Advertisement
Zalmoxis_1

graf

Nov 28th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include<cstdio>
  2. #include<vector>
  3. #include<deque>
  4. #include<cstrign>
  5. using namespace std;
  6. vector<int> a[100001];
  7. vector<int>::iterator it;
  8. deque<pair<int,int> > cd;
  9. int i, n, m, x, y, val[100001], nr, sol;
  10. pair<int,int> p;
  11. bool ok[100001], merge, viz[100001];
  12. int main(){
  13.     freopen("a.in","r",stdin);
  14.     freopen("a.out","w",stdout);
  15.     scanf("%d%d", &n, &m);
  16.     for (i=1;i<=m;i++) {
  17.         scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x);
  18.     }
  19.     scanf("%d", &val[0]); for (i=1;i<=val[0];i++) scanf("%d", &val[i]);
  20.     scanf("%d", &nr); for (i=1;i<=nr;i++) {scanf("%d", &x); ok[x]=true;}
  21.     for (i=1;i<=val[0];i++) {
  22.         cd.clear(); merge=true; memset(viz, false, sizeof(viz));
  23.         viz[val[i]]=true;
  24.         for (it=a[val[i]].begin();it!=a[val[i]].end();it++)
  25.             cd.push_back(make_pair(*it, 1));
  26.         while (merge==true) {
  27.             p=cd.front();
  28.             for (it=a[x].begin();it!=a[x].end();it++)
  29.                 if (!viz[*it])
  30.                 cd.push_back(make_pair(*it, p.second+1));
  31.             if (ok[p.first]==true) {merge=true; sol=p.second;}
  32.         }
  33.         printf("%d\n", sol);
  34.     }
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement