Josif_tepe

Untitled

Feb 1st, 2026
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <map>
  4. const int maxn = 105;
  5. using namespace std;
  6. int n;
  7. vector<int> dangerous[maxn];
  8. vector<int> safe_graph[maxn];
  9. map<pair<int, int>, bool> danger, safe;
  10. int main() {
  11.     ios_base::sync_with_stdio(false);
  12.     cin >> n;
  13.    
  14.     int m;
  15.     cin >> m;
  16.     for(int i = 0; i < m; i++) {
  17.         int a, b;
  18.         cin >> a >> b;
  19.         dangerous[a].push_back(b);
  20.         dangerous[b].push_back(a);
  21.        
  22.         danger[{a, b}] = true;
  23.         danger[{b, a}] = true;
  24.     }
  25.    
  26.     int o;
  27.     cin >> o;
  28.     for(int i = 0; i < o; i++) {
  29.         int a, b;
  30.         cin >> a >> b;
  31.        
  32.         safe_graph[a].push_back(b);
  33.         safe_graph[b].push_back(a);
  34.        
  35.         safe[{a, b}] = true;
  36.         safe[{b, a}] = true;
  37.     }
  38.  
  39.     for(int i = 1; i <= n; i++) {
  40.         int res = 0;
  41.         queue<int> q;
  42.         q.push(i);
  43.         q.push(0);
  44.        
  45.         vector<bool> visited(n + 1);
  46.         visited[i] = true;
  47.        
  48.         while(!q.empty()) {
  49.             int node = q.front();
  50.             q.pop();
  51.            
  52.             int dist = q.front();
  53.             q.pop();
  54.            
  55.             if(dist > 1) {
  56.                 if(!safe[{i, node}] and !danger[{i, node}]) {
  57.                     res++;
  58.                 }
  59.             }
  60.             for(int j = 0; j < (int) dangerous[node].size(); j++) {
  61.                 int neighbour = dangerous[node][j];
  62.                
  63.                 if(!visited[neighbour]) {
  64.                     visited[neighbour] = true;
  65.                     q.push(neighbour);
  66.                     q.push(dist + 1);
  67.                 }
  68.             }
  69.         }
  70. cout << res << "\n";
  71.        
  72.     }
  73.     return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment