#include <bits/stdc++.h> using namespace std; const int MAXN = 1e4 * 3 + 10, MAXE = 1e4 * 5 + 10, MAXR = 1e5 * 2 + 10; set<int>adj[MAXN]; set<int>adj2[MAXN]; bool visited[MAXN]; pair<int,int>Ei[MAXE], Eo[MAXE], ans[MAXR]; bool linkone[MAXN], linked[MAXN]; int cur; char op[MAXR]; vector<int>S; void addones(int v){ visited[v] = true; if (v != 1 && !linked[v]){ op[cur] = '+'; ans[cur] = {1,v}; cur++; } for (int u: adj[v]){ if (!visited[u]){ addones(u); } } if (!linkone[v]){ S.push_back(v); } } void solve(int n, int m1, int m2){ linkone[1] = true; addones(1); for (int i = 0; i < m2; i++){ int v = Eo[i].first, u = Eo[i].second; if (v != 1 && u != 1 && adj[v].find(u) == adj[v].end()){ op[cur] = '+'; ans[cur].first = v; ans[cur].second = u; cur++; adj[v].insert(u); adj[u].insert(v); } } for (int i = 2; i <= n; i++){ for (int x: adj[i]){ if (x != 1 && adj2[i].find(x) == adj2[i].end()){ adj2[x].insert(i); op[cur] = '-'; ans[cur].first = i; ans[cur].second = x; cur++; } } } for (int x: S){ op[cur] = '-'; ans[cur].first = 1; ans[cur].second = x; cur++; } cout << cur << "\n"; for (int i = 0; i < cur; i++){ cout << op[i] << " " << ans[i].first << " " << ans[i].second << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int m1, m2; cin >> m1; for (int i = 0; i < m1; i++){ int u, v; cin >> u >> v; Ei[i] = {u,v}; adj[u].insert(v); adj[v].insert(u); if (v == 1){ linked[u] = true; } if (u == 1){ linked[v] = true; } } cin >> m2; for (int i = 0; i < m2; i++){ int u, v; cin >> u >> v; Eo[i] = {u,v}; adj2[u].insert(v); adj2[v].insert(u); if (v == 1){ linkone[u] = true; } if (u == 1){ linkone[v] = true; } } solve(n,m1,m2); }
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 | #include <bits/stdc++.h> using namespace std; const int MAXN = 1e4 * 3 + 10, MAXE = 1e4 * 5 + 10, MAXR = 1e5 * 2 + 10; set<int>adj[MAXN]; set<int>adj2[MAXN]; bool visited[MAXN]; pair<int,int>Ei[MAXE], Eo[MAXE], ans[MAXR]; bool linkone[MAXN], linked[MAXN]; int cur; char op[MAXR]; vector<int>S; void addones(int v){ visited[v] = true; if (v != 1 && !linked[v]){ op[cur] = '+'; ans[cur] = {1,v}; cur++; } for (int u: adj[v]){ if (!visited[u]){ addones(u); } } if (!linkone[v]){ S.push_back(v); } } void solve(int n, int m1, int m2){ linkone[1] = true; addones(1); for (int i = 0; i < m2; i++){ int v = Eo[i].first, u = Eo[i].second; if (v != 1 && u != 1 && adj[v].find(u) == adj[v].end()){ op[cur] = '+'; ans[cur].first = v; ans[cur].second = u; cur++; adj[v].insert(u); adj[u].insert(v); } } for (int i = 2; i <= n; i++){ for (int x: adj[i]){ if (x != 1 && adj2[i].find(x) == adj2[i].end()){ adj2[x].insert(i); op[cur] = '-'; ans[cur].first = i; ans[cur].second = x; cur++; } } } for (int x: S){ op[cur] = '-'; ans[cur].first = 1; ans[cur].second = x; cur++; } cout << cur << "\n"; for (int i = 0; i < cur; i++){ cout << op[i] << " " << ans[i].first << " " << ans[i].second << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int m1, m2; cin >> m1; for (int i = 0; i < m1; i++){ int u, v; cin >> u >> v; Ei[i] = {u,v}; adj[u].insert(v); adj[v].insert(u); if (v == 1){ linked[u] = true; } if (u == 1){ linked[v] = true; } } cin >> m2; for (int i = 0; i < m2; i++){ int u, v; cin >> u >> v; Eo[i] = {u,v}; adj2[u].insert(v); adj2[v].insert(u); if (v == 1){ linkone[u] = true; } if (u == 1){ linkone[v] = true; } } solve(n,m1,m2); } |