Untitled

 avatar
unknown
c_cpp
3 years ago
1.7 kB
4
Indexable
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 1e5 + 10;
vector<int> dangerous_graph[maxn];
vector<int> safe_graph[maxn];
int idx[maxn], sz[maxn];
int find_root(int x) {
    while(x != idx[x]) {
        idx[x] = idx[idx[x]];
        x = idx[x];
    }
    return x;
}
void unite_two_elements(int A, int B) {
    int root_a = find_root(A);
    int root_b = find_root(B);
    if(root_a != root_b) {
        if(sz[root_a] < sz[root_b]) {
            idx[root_a] = idx[root_b];
            sz[root_b] += sz[root_a];
        }
        else {
            idx[root_b] = idx[root_a];
            sz[root_a] += sz[root_b];
        }
    }
}
int main() {
    for(int i = 0; i < maxn; i++) {
        idx[i] = i;
        sz[i] = 1;
    }
    int n;
    cin >> n;
    
    int m;
    cin >> m;
    for(int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        dangerous_graph[a].push_back(b);
        dangerous_graph[b].push_back(a);
        unite_two_elements(a, b);
    }
    int k;
    cin >> k;
    for(int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        safe_graph[a].push_back(b);
        safe_graph[b].push_back(a);
    }
    vector<int> result(n);
    for(int i = 0; i < n; i++) {
        result[i] = sz[find_root(i)] - 1;
        result[i] -= (int) dangerous_graph[i].size();
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < (int) safe_graph[i].size(); j++) {
            int neighbour = safe_graph[i][j];
            if(find_root(i) == find_root(neighbour)) {
                result[i]--;
            }
        }
    }
    for(int i = 0; i < n; i++) {
        cout << result[i] << endl;
    }
    
    return 0;
}
Editor is loading...