Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <cstring>
- #include <queue>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- const int maxn = 1e5 + 10;
- int n, m;
- 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;
- }
- bool check(int a, int b) {
- if(find_root(a) == find_root(b)) {
- return true;
- }
- return false;
- }
- void unite(int a, int b) {
- int a_root = find_root(a);
- int b_root = find_root(b);
- if(a_root != b_root) {
- if(sz[a_root] < sz[b_root]) {
- idx[a_root] = idx[b_root];
- sz[b_root] += sz[a_root];
- }
- else {
- idx[b_root] = idx[a_root];
- sz[a_root] += sz[b_root];
- }
- }
- }
- int main() {
- for(int i = 0; i < maxn; i++) {
- idx[i] = i;
- sz[i] = 1;
- }
- 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);
- unite(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);
- }
- for(int i = 0; i < n; i++) {
- int result = sz[find_root(i)] - 1;
- result -= (int) dangerous_graph[i].size();
- // cout << i << " " << sz[find_root(i)] << " " << dangerous_graph[i].size() << endl;
- for(int j = 0; j < safe_graph[i].size(); j++) {
- int neighbour = safe_graph[i][j];
- if(check(neighbour, i)) {
- result--;
- }
- }
- cout << result << endl;
- }
- return 0;
- }
- /*
- 10 9
- 1 6
- 1 9
- 1 10
- 2 6
- 2 7
- 3 7
- 4 8
- 4 9
- 5 10
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement