Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int maxn = 1e5 + 10;
- vector<int> dangerous_graph[maxn], safe_graph[maxn];
- int idx[maxn], size[maxn];
- int root(int x) {
- while(x != idx[x]) {
- idx[x] = idx[idx[x]];
- x = idx[x];
- }
- return x;
- }
- void unify(int a, int b) {
- int root_a = root(a);
- int root_b = root(b);
- if(root_a == root_b) {
- return;
- }
- if(size[root_a] < size[root_b]) {
- size[root_b] += size[root_a];
- idx[root_a] = idx[root_b];
- }
- else {
- size[root_a] += size[root_b];
- idx[root_b] = idx[root_a];
- }
- }
- int main() {
- for(int i = 0; i < maxn; i++) {
- idx[i] = i;
- size[i] = 1;
- }
- int n, m;
- cin >> n >> 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);
- unify(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> pairs(n);
- for(int i = 0; i < n; i++) {
- pairs[i] = size[root(i)];
- pairs[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(root(neighbour) == root(i)) {
- pairs[i]--;
- }
- }
- }
- for(int i = 0; i < n; i++) {
- cout << pairs[i] - 1 << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement