Advertisement
Vengadora

aNodeTooFar-OLVeredictDX

Mar 26th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. //#define NDEBUG
  2.  
  3. #include<string>
  4. #include<queue>
  5. #include<math.h>
  6. #include<iostream>
  7. #include<vector>
  8. #include<algorithm>
  9. #include<sstream>
  10. #include<fstream>
  11. #include<assert.h>
  12. #include<set>
  13. #include<map>
  14. #include<cstdio>
  15.  
  16. using namespace std;
  17.  
  18. typedef long long int ll;
  19. typedef vector<int> vi;
  20. typedef vector<ll> vll;
  21. typedef vector<string> vs;
  22. typedef vector<char> vc;
  23. typedef pair<int,int> ii;
  24. typedef pair<string,string> ss;
  25.  
  26. int main(){
  27.       int cases = 0;
  28.  
  29.       while(true) {
  30.         map<int, vi> graph;
  31.         int nc;
  32.         scanf("%d", &nc);
  33.  
  34.         if(!nc)
  35.           break;
  36.  
  37.         for(int i=0; i<nc; i++) {
  38.           int a, b;
  39.           scanf("%d%d", &a, &b);
  40.           graph[a].push_back(b);
  41.           graph[b].push_back(a);
  42.         }
  43.        
  44.         while(true) {
  45.           ll start, ttl;
  46.           scanf("%d%d", &start, &ttl);
  47.  
  48.           if(!start && !ttl)
  49.             break;
  50.           cases++;
  51.          
  52.           //BFS MOTHAFUCKAH!
  53.           map<int, int> dist;
  54.           map<int, bool> visited;
  55.           queue<int> explore;
  56.           explore.push(start);
  57.           visited[explore.front()] = true;
  58.           dist[explore.front()] = 0;
  59.  
  60.           while(!explore.empty()) {
  61.            
  62.             if(dist[explore.front()] != ttl) {
  63.  
  64.                   for(int i=0; i<graph[explore.front()].size(); i++) {
  65.  
  66.                     if(!visited[graph[explore.front()][i]]) {
  67.                       dist[graph[explore.front()][i]] =
  68.                         dist[explore.front()] + 1;
  69.                       explore.push(graph[explore.front()][i]);
  70.                       visited[graph[explore.front()][i]] = true;
  71.                     }
  72.  
  73.                   }
  74.  
  75.             }
  76.            
  77.             explore.pop();
  78.           }
  79.           /* by now you know that the vector dist has
  80.              the optimus distance from the start to all
  81.              the nodes he can go */
  82.  
  83.           int notReachable = graph.size() - dist.size();
  84.           printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n", cases, notReachable, start, ttl);
  85.         }
  86.  
  87.       }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement