Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define mx 1000000
  4. bool vis[mx];
  5. int n,e,cnt;
  6. vector<int>v[mx];
  7. int dis[mx];
  8. map<int,int>m;
  9. map<int,int>::iterator it;
  10. int  bfs(int sn,int z)
  11. {
  12.     int i,j;
  13.     for(it=m.begin();it!=m.end();it++){
  14.         vis[it->first]=false;
  15.         dis[it->first]=5000000;
  16.     }
  17.     dis[sn]=0;
  18.     vis[sn]=true;
  19.     queue<int >q;
  20.     q.push(sn);
  21.     while(!q.empty()){
  22.         int x=q.front();
  23.         q.pop();
  24.         for(i=0;i<v[x].size();i++){
  25.             if(vis[v[x][i]]==false){
  26.                 dis[v[x][i]]=dis[x]+1;
  27.                 vis[v[x][i]]=true;
  28.                 q.push(v[x][i]);
  29.             }
  30.         }
  31.     }
  32.     return cnt;
  33. }
  34. int main()
  35. {
  36.     int i,j,a,b,p,k=0,l=0,c=0;
  37.     while(1){
  38.         p=0;
  39.         cin>>e;
  40.         if(e==0) break;
  41.         for(i=0;i<e;i++){
  42.             int n1,n2;
  43.             cin>>n1>>n2;
  44.             l=m[n1]++;
  45.             if(l==0) p++;
  46.             k=m[n2]++;
  47.             if(k==0) p++;
  48.             v[n1].push_back(n2);
  49.             v[n2].push_back(n1);
  50.         }
  51.  
  52.         while(1){
  53.             c++;
  54.             cnt=0;
  55.             scanf("%d%d",&a,&b);
  56.             if(a==0 && b==0) break;
  57.             bfs(a,p);
  58.             for(it=m.begin();it!=m.end();it++){
  59.                 if(dis[it->first]>b) cnt++;
  60.             }
  61.             cout<<"Case "<<c<<": "<<cnt<<" nodes not reachable from node "<<a<<" with TTL = "<<b<<"."<<endl;
  62.         }
  63.         for(it=m.begin();it!=m.end();it++){
  64.             v[it->first].clear();
  65.         }
  66.          m.clear();
  67.          cout<<endl;
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement