Josif_tepe

Untitled

Feb 1st, 2026
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <map>
  4. const int maxn = 1e5 + 10;
  5. using namespace std;
  6.  
  7. int idx[maxn], sz[maxn];
  8. void init() {
  9.     for(int i = 0; i < maxn; i++) {
  10.         idx[i] = i;
  11.         sz[i] = 1;
  12.     }
  13. }
  14.  
  15. int find_root(int x) {
  16.     while(x != idx[x]) {
  17.         idx[x] = idx[idx[x]];
  18.         x = idx[x];
  19.     }
  20.     return x;
  21. }
  22.  
  23. void unite(int A, int B) {
  24.     int rootA = find_root(A);
  25.     int rootB = find_root(B);
  26.    
  27.     if(rootA != rootB) {
  28.         if(sz[rootA] < sz[rootB]) {
  29.             idx[rootA] = idx[rootB];
  30.             sz[rootB] += sz[rootA];
  31.         }
  32.         else {
  33.             idx[rootB] = idx[rootA];
  34.             sz[rootA] += sz[rootB];
  35.         }
  36.     }
  37. }
  38. bool check(int A, int B) {
  39.     return find_root(A) == find_root(B);
  40. }
  41.  
  42. vector<int> danger_graph[maxn], safe_graph[maxn];
  43. int main() {
  44.     ios_base::sync_with_stdio(false);
  45.     init();
  46.    
  47.     int n;
  48.     cin >> n;
  49.    
  50.     int m;
  51.     cin >> m;
  52.    
  53.     for(int i = 0; i < m; i++) {
  54.         int a, b;
  55.         cin >> a >> b;
  56.        
  57.         danger_graph[a].push_back(b);
  58.         danger_graph[b].push_back(a);
  59.        
  60.         unite(a, b);
  61.     }
  62.    
  63.     int o;
  64.     cin >> o;
  65.     for(int i = 0; i < o; i++) {
  66.         int a, b;
  67.         cin >> a >> b;
  68.        
  69.         safe_graph[a].push_back(b);
  70.         safe_graph[b].push_back(a);
  71.     }
  72.    
  73.     for(int i = 1; i <= n; i++) {
  74.         int res = sz[find_root(i)] - 1;
  75.         res -= (int) danger_graph[i].size();
  76.        
  77.         for(int j = 0; j < (int) safe_graph[i].size(); j++) {
  78.             int neighbour = safe_graph[i][j];
  79.            
  80.             if(check(i, neighbour)) {
  81.                 res--;
  82.             }
  83.         }
  84.        
  85.         cout << res << endl;
  86.     }
  87.    
  88.     return 0;
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment