#include <cstdio> #include <vector> #include <algorithm> using namespace std; struct drz { int _n; int m; vector<vector<int>> v; vector<pair<int, int>> e; void wczyt(int n) { _n = n; v = vector<vector<int>>(n+1); scanf("%d" ,&m); int a, b; for (int i = 0; i < m; i++) { scanf("%d %d", &a, &b); v[a].push_back(b); v[b].push_back(a); if (a > b) swap(a, b); if (a != 1) e.push_back(make_pair(a, b)); } sort(e.begin(), e.end()); } vector<int> bfs() { vector<int> kol(1, 1), c(_n+1), ret; c[1] = 1; for(int x = 0; x < kol.size(); x++) { for(int i : v[kol[x]]) if(!c[i]) { c[i] = 1; kol.push_back(i); if (x) ret.push_back(i); } } return ret; } } a, b; int main() { int n; scanf("%d", &n); a.wczyt(n); b.wczyt(n); int w = 0; for (int i = 0, j = 0; i < a.e.size() && j < b.e.size(); ) { if (i == a.e.size()) { w++; j++; } else if (j == b.e.size() || a.e[i] < b.e[j]) { w++; i++; } else if (a.e[i] == b.e[j]) { i++; j++; } else { w++; j++; } } vector<int> x = a.bfs(), y = b.bfs(); printf("%d\n", w + (int)(x.size() + y.size())); for (int i : x) printf("+ 1 %d\n", i); for (int i = 0, j = 0; i < a.e.size() && j < b.e.size(); ) { if (i == a.e.size()) { printf("+ %d %d\n", b.e[j].first, b.e[j].second); j++; } else if (j == b.e.size() || a.e[i] < b.e[j]) { printf("- %d %d\n", a.e[i].first, a.e[i].second); i++; } else if (a.e[i] == b.e[j]) { i++; j++; } else { printf("+ %d %d\n", b.e[j].first, b.e[j].second); j++; } } reverse(y.begin(), y.end()); for (int i : y) printf("- 1 %d\n", i); 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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; struct drz { int _n; int m; vector<vector<int>> v; vector<pair<int, int>> e; void wczyt(int n) { _n = n; v = vector<vector<int>>(n+1); scanf("%d" ,&m); int a, b; for (int i = 0; i < m; i++) { scanf("%d %d", &a, &b); v[a].push_back(b); v[b].push_back(a); if (a > b) swap(a, b); if (a != 1) e.push_back(make_pair(a, b)); } sort(e.begin(), e.end()); } vector<int> bfs() { vector<int> kol(1, 1), c(_n+1), ret; c[1] = 1; for(int x = 0; x < kol.size(); x++) { for(int i : v[kol[x]]) if(!c[i]) { c[i] = 1; kol.push_back(i); if (x) ret.push_back(i); } } return ret; } } a, b; int main() { int n; scanf("%d", &n); a.wczyt(n); b.wczyt(n); int w = 0; for (int i = 0, j = 0; i < a.e.size() && j < b.e.size(); ) { if (i == a.e.size()) { w++; j++; } else if (j == b.e.size() || a.e[i] < b.e[j]) { w++; i++; } else if (a.e[i] == b.e[j]) { i++; j++; } else { w++; j++; } } vector<int> x = a.bfs(), y = b.bfs(); printf("%d\n", w + (int)(x.size() + y.size())); for (int i : x) printf("+ 1 %d\n", i); for (int i = 0, j = 0; i < a.e.size() && j < b.e.size(); ) { if (i == a.e.size()) { printf("+ %d %d\n", b.e[j].first, b.e[j].second); j++; } else if (j == b.e.size() || a.e[i] < b.e[j]) { printf("- %d %d\n", a.e[i].first, a.e[i].second); i++; } else if (a.e[i] == b.e[j]) { i++; j++; } else { printf("+ %d %d\n", b.e[j].first, b.e[j].second); j++; } } reverse(y.begin(), y.end()); for (int i : y) printf("- 1 %d\n", i); return 0; } |