#include <bits/stdc++.h> using namespace std; #define f first #define s second struct Pyt { char op; int a, b; }; const int MAX = 30'007; vector<int> g1[MAX]; vector<int> g2[MAX]; bool odw1[MAX]; vector<int> bfs1(int v) { vector<int> ret; queue<int> Q; Q.push(v); while (!Q.empty()) { int curr = Q.front(); Q.pop(); odw1[curr] = true; for (int x : g1[curr]) { if (odw1[x]) continue; if (curr != v) ret.push_back(x); odw1[x] = true; Q.push(x); } } return ret; } bool odw2[MAX]; vector<int> bfs2(int v) { vector<int> ret; queue<int> Q; Q.push(v); while (!Q.empty()) { int curr = Q.front(); Q.pop(); odw2[curr] = true; for (int x : g2[curr]) { if (odw2[x]) continue; if (curr != v) ret.push_back(x); odw2[x] = true; Q.push(x); } } reverse(ret.begin(), ret.end()); return ret; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; // graf 1 int m1; cin >> m1; vector<pair<int,int>> kraw1(m1); for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; if (a > b) swap(a, b); kraw1[i] = {a, b}; g1[a].push_back(b); g1[b].push_back(a); } sort(kraw1.begin(), kraw1.end()); // graf 2 int m2; cin >> m2; vector<pair<int,int>> kraw2(m2); for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; if (a > b) swap(a, b); kraw2[i] = {a, b}; g2[a].push_back(b); g2[b].push_back(a); } sort(kraw2.begin(), kraw2.end()); vector<Pyt> odp; // dodaj wszystkie krawedzie do jedynki for (int x : bfs1(1)) odp.push_back({'+', 1, x}); // usun wszystkie stare krawedzie for (int i = 0; i < m1; i++) { if (kraw1[i].f == 1) continue; odp.push_back({'-', kraw1[i].f, kraw1[i].s}); } // dodaj (prawie) wszystkie nowe krawedzie for (int i = 0; i < m2; i++) { if (kraw2[i].f == 1) continue; odp.push_back({'+', kraw2[i].f, kraw2[i].s}); } // usun wszystkie zbedne krawedzie do jedynki for (int x : bfs2(1)) odp.push_back({'-', 1, x}); cout << odp.size() << '\n'; for (Pyt p : odp) cout << p.op << ' ' << p.a << ' ' << p.b << '\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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | #include <bits/stdc++.h> using namespace std; #define f first #define s second struct Pyt { char op; int a, b; }; const int MAX = 30'007; vector<int> g1[MAX]; vector<int> g2[MAX]; bool odw1[MAX]; vector<int> bfs1(int v) { vector<int> ret; queue<int> Q; Q.push(v); while (!Q.empty()) { int curr = Q.front(); Q.pop(); odw1[curr] = true; for (int x : g1[curr]) { if (odw1[x]) continue; if (curr != v) ret.push_back(x); odw1[x] = true; Q.push(x); } } return ret; } bool odw2[MAX]; vector<int> bfs2(int v) { vector<int> ret; queue<int> Q; Q.push(v); while (!Q.empty()) { int curr = Q.front(); Q.pop(); odw2[curr] = true; for (int x : g2[curr]) { if (odw2[x]) continue; if (curr != v) ret.push_back(x); odw2[x] = true; Q.push(x); } } reverse(ret.begin(), ret.end()); return ret; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; // graf 1 int m1; cin >> m1; vector<pair<int,int>> kraw1(m1); for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; if (a > b) swap(a, b); kraw1[i] = {a, b}; g1[a].push_back(b); g1[b].push_back(a); } sort(kraw1.begin(), kraw1.end()); // graf 2 int m2; cin >> m2; vector<pair<int,int>> kraw2(m2); for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; if (a > b) swap(a, b); kraw2[i] = {a, b}; g2[a].push_back(b); g2[b].push_back(a); } sort(kraw2.begin(), kraw2.end()); vector<Pyt> odp; // dodaj wszystkie krawedzie do jedynki for (int x : bfs1(1)) odp.push_back({'+', 1, x}); // usun wszystkie stare krawedzie for (int i = 0; i < m1; i++) { if (kraw1[i].f == 1) continue; odp.push_back({'-', kraw1[i].f, kraw1[i].s}); } // dodaj (prawie) wszystkie nowe krawedzie for (int i = 0; i < m2; i++) { if (kraw2[i].f == 1) continue; odp.push_back({'+', kraw2[i].f, kraw2[i].s}); } // usun wszystkie zbedne krawedzie do jedynki for (int x : bfs2(1)) odp.push_back({'-', 1, x}); cout << odp.size() << '\n'; for (Pyt p : odp) cout << p.op << ' ' << p.a << ' ' << p.b << '\n'; return 0; } |