//2024-03-12 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; const int maxN = 3e4+2; int n, ms, md; vector <int> Gs [maxN], Gd [maxN]; bool vis [maxN]; set <pair<int, int>> Es, Ed; vector <pair<int, int>> dodaj; vector <pair<int, int>> odejmij; void dfs1 (int u) { vis[u] = 1; if (u != 1 && !Es.count({1, u})) dodaj.push_back({1, u}); for (int v : Gs[u]) if (!vis[v]) dfs1(v); } void dfs2 (int u) { vis[u] = 1; for (int v : Gd[u]) if (!vis[v]) dfs2(v); if (u != 1 && !Ed.count({1, u})) odejmij.push_back({1, u}); } int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> ms; for (int i = 1; i <= ms; i++) { int a, b; cin >> a >> b; Gs[a].push_back(b); Gs[b].push_back(a); if (a > b) swap(a, b); Es.insert({a, b}); } cin >> md; for (int i = 1; i <= md; i++) { int a, b; cin >> a >> b; Gd[a].push_back(b); Gd[b].push_back(a); if (a > b) swap(a, b); Ed.insert({a, b}); } dfs1(1); for (auto p : Ed) if (p.first != 1 && !Es.count(p)) dodaj.push_back(p); for (auto p : Es) if (p.first != 1 && !Ed.count(p)) odejmij.push_back(p); fill (vis+1, vis+1+n, 0); dfs2(1); cout << dodaj.size() + odejmij.size() << '\n'; for (auto p : dodaj) cout << "+ " << p.first << ' ' << p.second << '\n'; for (auto p : odejmij) cout << "- " << p.first << ' ' << p.second << '\n'; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | //2024-03-12 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; const int maxN = 3e4+2; int n, ms, md; vector <int> Gs [maxN], Gd [maxN]; bool vis [maxN]; set <pair<int, int>> Es, Ed; vector <pair<int, int>> dodaj; vector <pair<int, int>> odejmij; void dfs1 (int u) { vis[u] = 1; if (u != 1 && !Es.count({1, u})) dodaj.push_back({1, u}); for (int v : Gs[u]) if (!vis[v]) dfs1(v); } void dfs2 (int u) { vis[u] = 1; for (int v : Gd[u]) if (!vis[v]) dfs2(v); if (u != 1 && !Ed.count({1, u})) odejmij.push_back({1, u}); } int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> ms; for (int i = 1; i <= ms; i++) { int a, b; cin >> a >> b; Gs[a].push_back(b); Gs[b].push_back(a); if (a > b) swap(a, b); Es.insert({a, b}); } cin >> md; for (int i = 1; i <= md; i++) { int a, b; cin >> a >> b; Gd[a].push_back(b); Gd[b].push_back(a); if (a > b) swap(a, b); Ed.insert({a, b}); } dfs1(1); for (auto p : Ed) if (p.first != 1 && !Es.count(p)) dodaj.push_back(p); for (auto p : Es) if (p.first != 1 && !Ed.count(p)) odejmij.push_back(p); fill (vis+1, vis+1+n, 0); dfs2(1); cout << dodaj.size() + odejmij.size() << '\n'; for (auto p : dodaj) cout << "+ " << p.first << ' ' << p.second << '\n'; for (auto p : odejmij) cout << "- " << p.first << ' ' << p.second << '\n'; return 0; } |