#include "bits/stdc++.h" #define convert(a, b) {min(a, b), max(a, b)} using namespace std; int const N = 3e4+5, M = 5e5+5; int n, ms, md, d[N]; bool vis[N]; pair <int, int> qs[M], qd[M]; map <pair<int, int>, int> gs, gd; vector <pair<int, int>> add, del; vector <int> v[N], g[N]; void construct(int x){ vis[x] = true; for(int u : v[x]){ if(vis[u]) continue; if(!gs[convert(1, u)]) add.push_back(convert(1, u)); gs[convert(1, u)] = 1; construct(u); } } void bfs(int x){ queue <pair<int, int>> q; q.push({x, 0}); while(!q.empty()){ auto [x, u] = q.front(); q.pop(); d[x] = d[u] + 1; vis[x] = true; for(auto w : g[x]){ if(vis[w]) continue; q.push({w, x}); } } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> ms; for(int i = 0, a, b; i < ms; ++i){ cin >> a >> b; qs[i] = convert(a, b); gs[qs[i]] = true; v[a].push_back(b); v[b].push_back(a); } cin >> md; for(int i = 0, a, b; i < md; ++i){ cin >> a >> b; qd[i] = convert(a, b); gd[qd[i]] = true; } construct(1); for(int i = 0; i < md; ++i){ if(!gs[qd[i]]){ add.push_back(qd[i]); } auto [a, b] = qd[i]; g[a].push_back(b); g[b].push_back(a); } for(int i = 0; i < ms; ++i){ if(!gd[qs[i]] && (qs[i].first != 1 && qs[i].second != 1)) del.push_back(qs[i]); } for(int i = 0; i <= n; ++i) vis[i] = false; bfs(1); vector <pair<int, int>> order; for(int i = 2; i <= n; ++i) if(!gd[{1, i}]) order.push_back({d[i], i}); sort(order.begin(), order.end()); reverse(order.begin(), order.end()); for(auto x : order) del.push_back({1, x.second}); cout << add.size() + del.size() << '\n'; for(auto x : add) cout << "+ " << x.first << ' ' << x.second << '\n'; for(auto x : del) cout << "- " << x.first << ' ' << x.second << '\n'; }
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 | #include "bits/stdc++.h" #define convert(a, b) {min(a, b), max(a, b)} using namespace std; int const N = 3e4+5, M = 5e5+5; int n, ms, md, d[N]; bool vis[N]; pair <int, int> qs[M], qd[M]; map <pair<int, int>, int> gs, gd; vector <pair<int, int>> add, del; vector <int> v[N], g[N]; void construct(int x){ vis[x] = true; for(int u : v[x]){ if(vis[u]) continue; if(!gs[convert(1, u)]) add.push_back(convert(1, u)); gs[convert(1, u)] = 1; construct(u); } } void bfs(int x){ queue <pair<int, int>> q; q.push({x, 0}); while(!q.empty()){ auto [x, u] = q.front(); q.pop(); d[x] = d[u] + 1; vis[x] = true; for(auto w : g[x]){ if(vis[w]) continue; q.push({w, x}); } } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> ms; for(int i = 0, a, b; i < ms; ++i){ cin >> a >> b; qs[i] = convert(a, b); gs[qs[i]] = true; v[a].push_back(b); v[b].push_back(a); } cin >> md; for(int i = 0, a, b; i < md; ++i){ cin >> a >> b; qd[i] = convert(a, b); gd[qd[i]] = true; } construct(1); for(int i = 0; i < md; ++i){ if(!gs[qd[i]]){ add.push_back(qd[i]); } auto [a, b] = qd[i]; g[a].push_back(b); g[b].push_back(a); } for(int i = 0; i < ms; ++i){ if(!gd[qs[i]] && (qs[i].first != 1 && qs[i].second != 1)) del.push_back(qs[i]); } for(int i = 0; i <= n; ++i) vis[i] = false; bfs(1); vector <pair<int, int>> order; for(int i = 2; i <= n; ++i) if(!gd[{1, i}]) order.push_back({d[i], i}); sort(order.begin(), order.end()); reverse(order.begin(), order.end()); for(auto x : order) del.push_back({1, x.second}); cout << add.size() + del.size() << '\n'; for(auto x : add) cout << "+ " << x.first << ' ' << x.second << '\n'; for(auto x : del) cout << "- " << x.first << ' ' << x.second << '\n'; } |