#include <bits/stdc++.h> #define pb push_back using namespace std; typedef vector<int> vi; typedef vector<pair<int, int>> vii; typedef pair<int, int> pii; const int N = 30003; vi adj[N]; int s_tab[N]; vi cel[N]; int k_tab[N]; bool visited[N]; set<pair<int, int>> s_edges; set<pair<int, int>> k_edges; vii add; vii del; vi kolejnosc; queue<int> q; void dfs(int x) { if(visited[x]) return; visited[x] = true; if(s_tab[x] == 0) { add.pb({1, x}); s_tab[x] = 1; } for(auto u: adj[x]) dfs(u); } void bfs() { while(!q.empty()){ int s = q.front(); q.pop(); for(auto u : cel[s]) { if(visited[u]) continue; visited[u] = true; q.push(u); if(k_tab[u] == 0) kolejnosc.pb(u); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, ms, mk; cin >> n >> ms; for(int i = 0; i <= n; i++) { s_tab[i] = 0; k_tab[i] = 0; visited[i] = false; } s_tab[1] = 1; for(int i = 0; i < ms; i++) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); if(a == 1) s_tab[b] = 1; else if(b == 1) s_tab[a] = 1; else { if(a > b) swap(a, b); s_edges.insert({a, b}); } } cin >> mk; for(int i = 0; i < mk; i++) { int a, b; cin >> a >> b; cel[a].pb(b); cel[b].pb(a); if(a == 1) k_tab[b] = 1; else if(b == 1) k_tab[a] = 1; else { if(a > b) swap(a, b); k_edges.insert({a, b}); } } dfs(1); for(auto it = k_edges.begin(); it != k_edges.end(); it++) { pii p = *it; if(s_edges.count(p) == 0) add.pb(p); } for(auto it = s_edges.begin(); it != s_edges.end(); it++) { pii p = *it; if(k_edges.count(p) == 0) del.pb(p); } for(int i = 0; i <= n; i++) visited[i] = false; visited[1] = true; q.push(1); bfs(); reverse(kolejnosc.begin(), kolejnosc.end()); int sum = add.size() + del.size() + kolejnosc.size(); cout << sum << "\n"; for(auto p : add) cout << "+ " << p.first << " " << p.second << "\n"; for(auto p : del) cout << "- " << p.first << " " << p.second << "\n"; for(auto p : kolejnosc) cout << "- " << 1 << " " << p << "\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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | #include <bits/stdc++.h> #define pb push_back using namespace std; typedef vector<int> vi; typedef vector<pair<int, int>> vii; typedef pair<int, int> pii; const int N = 30003; vi adj[N]; int s_tab[N]; vi cel[N]; int k_tab[N]; bool visited[N]; set<pair<int, int>> s_edges; set<pair<int, int>> k_edges; vii add; vii del; vi kolejnosc; queue<int> q; void dfs(int x) { if(visited[x]) return; visited[x] = true; if(s_tab[x] == 0) { add.pb({1, x}); s_tab[x] = 1; } for(auto u: adj[x]) dfs(u); } void bfs() { while(!q.empty()){ int s = q.front(); q.pop(); for(auto u : cel[s]) { if(visited[u]) continue; visited[u] = true; q.push(u); if(k_tab[u] == 0) kolejnosc.pb(u); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, ms, mk; cin >> n >> ms; for(int i = 0; i <= n; i++) { s_tab[i] = 0; k_tab[i] = 0; visited[i] = false; } s_tab[1] = 1; for(int i = 0; i < ms; i++) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); if(a == 1) s_tab[b] = 1; else if(b == 1) s_tab[a] = 1; else { if(a > b) swap(a, b); s_edges.insert({a, b}); } } cin >> mk; for(int i = 0; i < mk; i++) { int a, b; cin >> a >> b; cel[a].pb(b); cel[b].pb(a); if(a == 1) k_tab[b] = 1; else if(b == 1) k_tab[a] = 1; else { if(a > b) swap(a, b); k_edges.insert({a, b}); } } dfs(1); for(auto it = k_edges.begin(); it != k_edges.end(); it++) { pii p = *it; if(s_edges.count(p) == 0) add.pb(p); } for(auto it = s_edges.begin(); it != s_edges.end(); it++) { pii p = *it; if(k_edges.count(p) == 0) del.pb(p); } for(int i = 0; i <= n; i++) visited[i] = false; visited[1] = true; q.push(1); bfs(); reverse(kolejnosc.begin(), kolejnosc.end()); int sum = add.size() + del.size() + kolejnosc.size(); cout << sum << "\n"; for(auto p : add) cout << "+ " << p.first << " " << p.second << "\n"; for(auto p : del) cout << "- " << p.first << " " << p.second << "\n"; for(auto p : kolejnosc) cout << "- " << 1 << " " << p << "\n"; return 0; } |