#include <bits/stdc++.h> using namespace std; unordered_set<int> nbhss[30300]; unordered_set<int> nbhsd[30300]; bool visited1[30300]; bool visited2[30300]; vector<tuple<bool, int, int>> ans; void dfs1(int v) { if(visited1[v]) { return; } visited1[v] = true; if(v != 1 && nbhss[1].find(v) == nbhss[1].end()) { ans.push_back({true, 1, v}); } for(auto u: nbhss[v]) { dfs1(u); } } void dfs2(int v) { if(visited2[v]) { return; } visited2[v] = true; for(auto u: nbhsd[v]) { dfs2(u); } if(v != 1 && nbhsd[1].find(v) == nbhsd[1].end()) { ans.push_back({false, 1, v}); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int ms; cin >> ms; for(int i = 0; i < ms; i++) { int v, u; cin >> v >> u; nbhss[v].insert(u); nbhss[u].insert(v); } dfs1(1); int md; cin >> md; for(int i = 0; i < md; i++) { int v, u; cin >> v >> u; nbhsd[v].insert(u); nbhsd[u].insert(v); if(v != 1 && u != 1 && nbhss[v].find(u) == nbhss[v].end()) { ans.push_back({true, v, u}); } } for(int i = 2; i <= n; i++) { for(auto v: nbhss[i]) { if(i < v && nbhsd[i].find(v) == nbhsd[i].end()) { ans.push_back({false, i, v}); } } } dfs2(1); cout << ans.size() << "\n"; for(auto s: ans) { cout << (get<0>(s) ? "+" : "-") << " " << get<1>(s) << " " << get<2>(s) << "\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 | #include <bits/stdc++.h> using namespace std; unordered_set<int> nbhss[30300]; unordered_set<int> nbhsd[30300]; bool visited1[30300]; bool visited2[30300]; vector<tuple<bool, int, int>> ans; void dfs1(int v) { if(visited1[v]) { return; } visited1[v] = true; if(v != 1 && nbhss[1].find(v) == nbhss[1].end()) { ans.push_back({true, 1, v}); } for(auto u: nbhss[v]) { dfs1(u); } } void dfs2(int v) { if(visited2[v]) { return; } visited2[v] = true; for(auto u: nbhsd[v]) { dfs2(u); } if(v != 1 && nbhsd[1].find(v) == nbhsd[1].end()) { ans.push_back({false, 1, v}); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int ms; cin >> ms; for(int i = 0; i < ms; i++) { int v, u; cin >> v >> u; nbhss[v].insert(u); nbhss[u].insert(v); } dfs1(1); int md; cin >> md; for(int i = 0; i < md; i++) { int v, u; cin >> v >> u; nbhsd[v].insert(u); nbhsd[u].insert(v); if(v != 1 && u != 1 && nbhss[v].find(u) == nbhss[v].end()) { ans.push_back({true, v, u}); } } for(int i = 2; i <= n; i++) { for(auto v: nbhss[i]) { if(i < v && nbhsd[i].find(v) == nbhsd[i].end()) { ans.push_back({false, i, v}); } } } dfs2(1); cout << ans.size() << "\n"; for(auto s: ans) { cout << (get<0>(s) ? "+" : "-") << " " << get<1>(s) << " " << get<2>(s) << "\n"; } return 0; } |