#include <iostream> #include <vector> #include <queue> #include <set> #include <algorithm> using namespace std; const int MAXN = 30000; set<pair<int, int>>A; set<pair<int, int>>B; vector<set<int>>G(MAXN); vector<pair<int, int>>X, Y; void Dodaj(int a, int b) { if (G[a].find(b) == G[a].end()) { G[a].insert(b); G[b].insert(a); X.push_back({ a, b }); } } void Usun(int a, int b) { if (B.find({ a, b }) != B.end()) return; G[a].erase(b); G[b].erase(a); Y.push_back({ a, b }); } void BFS(int n) { vector<int>d(n + 1, -1); d[1] = 0; queue<int>Q; Q.push(1); while (Q.size()) { auto v = Q.front(); Q.pop(); for (auto w : G[v]) { if (d[w] == -1) { Q.push(w); d[w] = d[v] + 1; Dodaj(1, w); } } } } void SBFS(int n) { vector<int>d(n + 1, -1); d[1] = 0; queue<int>Q; Q.push(1); vector<pair<int, int>>Del; while (Q.size()) { auto v = Q.front(); Q.pop(); for (auto w : G[v]) { if (B.find({ v, w }) == B.end()) continue; if (d[w] == -1) { Q.push(w); d[w] = d[v] + 1; Del.push_back({ 1, w }); } } } reverse(Del.begin(), Del.end()); for (auto& p : Del) Usun(p.first, p.second); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n1, m1; cin >> n1 >> m1; G = vector<set<int>>(n1 + 1); for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; A.insert({ a, b }); A.insert({ b, a }); G[a].insert(b); G[b].insert(a); } int m2; cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; B.insert({ a, b }); B.insert({ b, a }); } BFS(n1); for (auto p : B) Dodaj(p.first, p.second); for (auto p : A) { if (p.first == 1 || p.second == 1) continue; Usun(p.first, p.second); } SBFS(n1); cout << X.size() + Y.size() << '\n'; for (auto p : X) cout << "+ " << p.first << ' ' << p.second << '\n'; for (auto p : Y) cout << "- " << p.first << ' ' << p.second << '\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 | #include <iostream> #include <vector> #include <queue> #include <set> #include <algorithm> using namespace std; const int MAXN = 30000; set<pair<int, int>>A; set<pair<int, int>>B; vector<set<int>>G(MAXN); vector<pair<int, int>>X, Y; void Dodaj(int a, int b) { if (G[a].find(b) == G[a].end()) { G[a].insert(b); G[b].insert(a); X.push_back({ a, b }); } } void Usun(int a, int b) { if (B.find({ a, b }) != B.end()) return; G[a].erase(b); G[b].erase(a); Y.push_back({ a, b }); } void BFS(int n) { vector<int>d(n + 1, -1); d[1] = 0; queue<int>Q; Q.push(1); while (Q.size()) { auto v = Q.front(); Q.pop(); for (auto w : G[v]) { if (d[w] == -1) { Q.push(w); d[w] = d[v] + 1; Dodaj(1, w); } } } } void SBFS(int n) { vector<int>d(n + 1, -1); d[1] = 0; queue<int>Q; Q.push(1); vector<pair<int, int>>Del; while (Q.size()) { auto v = Q.front(); Q.pop(); for (auto w : G[v]) { if (B.find({ v, w }) == B.end()) continue; if (d[w] == -1) { Q.push(w); d[w] = d[v] + 1; Del.push_back({ 1, w }); } } } reverse(Del.begin(), Del.end()); for (auto& p : Del) Usun(p.first, p.second); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n1, m1; cin >> n1 >> m1; G = vector<set<int>>(n1 + 1); for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; A.insert({ a, b }); A.insert({ b, a }); G[a].insert(b); G[b].insert(a); } int m2; cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; B.insert({ a, b }); B.insert({ b, a }); } BFS(n1); for (auto p : B) Dodaj(p.first, p.second); for (auto p : A) { if (p.first == 1 || p.second == 1) continue; Usun(p.first, p.second); } SBFS(n1); cout << X.size() + Y.size() << '\n'; for (auto p : X) cout << "+ " << p.first << ' ' << p.second << '\n'; for (auto p : Y) cout << "- " << p.first << ' ' << p.second << '\n'; return 0; } |