#include <bits/stdc++.h> using namespace std; constexpr int MAXN = 3e4 + 10; int n, m1, m2; set<int> adj1[MAXN], adj2[MAXN]; vector<int> odl; vector<string> res; void addFirstLayer() { queue<int> q; odl.assign(n+10, -1); q.push(1); odl[1] = 0; while (!q.empty()) { int v = q.front(); q.pop(); //cout << v << ' '; for (auto u : adj1[v]) { if (odl[u] != -1) continue; odl[u] = odl[v]+1; if (odl[u] != 1) { string tmp = "+ 1 "; tmp += to_string(u); res.push_back(tmp); } q.push(u); } } } void bfs() { queue<int> q; odl.assign(n+10, -1); q.push(1); odl[1] = 0; stack<string> tmpRes; while (!q.empty()) { int v = q.front(); q.pop(); //cout << v << ' '; for (auto u : adj2[v]) { if (odl[u] != -1) continue; odl[u] = odl[v]+1; if (odl[u] != 1) { string tmp = "- 1 "; tmp += to_string(u); tmpRes.push(tmp); } q.push(u); } } while (!tmpRes.empty()) { res.push_back(tmpRes.top()); tmpRes.pop(); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m1; for (int i = 1; i <= m1; i++) { int a, b; cin >> a >> b; adj1[a].insert(b); adj1[b].insert(a); } cin >> m2; for (int i = 1; i <= m2; i++) { int a, b; cin >> a >> b; adj2[a].insert(b); adj2[b].insert(a); } addFirstLayer(); for (int v = 2; v <= n; v++) { for (auto u : adj2[v]) { if (u < v || adj1[v].find(u) != adj1[v].end()) continue; string tmp = "+ "; tmp += to_string(v); tmp += ' '; tmp += to_string(u); res.push_back(tmp); } } for (int v = 2; v <= n; v++) { for (auto u : adj1[v]) { if (u < v || adj2[v].find(u) != adj2[v].end()) continue; string tmp = "- "; tmp += to_string(v); tmp += ' '; tmp += to_string(u); res.push_back(tmp); } } bfs(); cout << res.size() << '\n'; for (auto i : res) cout << i << '\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 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 | #include <bits/stdc++.h> using namespace std; constexpr int MAXN = 3e4 + 10; int n, m1, m2; set<int> adj1[MAXN], adj2[MAXN]; vector<int> odl; vector<string> res; void addFirstLayer() { queue<int> q; odl.assign(n+10, -1); q.push(1); odl[1] = 0; while (!q.empty()) { int v = q.front(); q.pop(); //cout << v << ' '; for (auto u : adj1[v]) { if (odl[u] != -1) continue; odl[u] = odl[v]+1; if (odl[u] != 1) { string tmp = "+ 1 "; tmp += to_string(u); res.push_back(tmp); } q.push(u); } } } void bfs() { queue<int> q; odl.assign(n+10, -1); q.push(1); odl[1] = 0; stack<string> tmpRes; while (!q.empty()) { int v = q.front(); q.pop(); //cout << v << ' '; for (auto u : adj2[v]) { if (odl[u] != -1) continue; odl[u] = odl[v]+1; if (odl[u] != 1) { string tmp = "- 1 "; tmp += to_string(u); tmpRes.push(tmp); } q.push(u); } } while (!tmpRes.empty()) { res.push_back(tmpRes.top()); tmpRes.pop(); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m1; for (int i = 1; i <= m1; i++) { int a, b; cin >> a >> b; adj1[a].insert(b); adj1[b].insert(a); } cin >> m2; for (int i = 1; i <= m2; i++) { int a, b; cin >> a >> b; adj2[a].insert(b); adj2[b].insert(a); } addFirstLayer(); for (int v = 2; v <= n; v++) { for (auto u : adj2[v]) { if (u < v || adj1[v].find(u) != adj1[v].end()) continue; string tmp = "+ "; tmp += to_string(v); tmp += ' '; tmp += to_string(u); res.push_back(tmp); } } for (int v = 2; v <= n; v++) { for (auto u : adj1[v]) { if (u < v || adj2[v].find(u) != adj2[v].end()) continue; string tmp = "- "; tmp += to_string(v); tmp += ' '; tmp += to_string(u); res.push_back(tmp); } } bfs(); cout << res.size() << '\n'; for (auto i : res) cout << i << '\n'; } |