#include <bits/stdc++.h> using namespace std; void input(int &n, int &ms, int &md, vector <vector <int>> &v, vector <bool> &r1, vector <bool> &r2, vector <pair <int, int>> &g1, vector <pair <int, int>> &g2){ cin >> n; r1.resize(n); r2.resize(n); for(int i = 0; i < n; i++){ r1[i] = 0; r2[i] = 0; } cin >> ms; v.resize(n); for(int i = 0; i < ms; i++){ int a, b; cin >> a >> b; a--; b--; if(a == 0){ r1[b] = 1; } else if(b == 0){ r1[a] = 1; } else{ g1.push_back(make_pair(a, b)); } v[a].push_back(b); v[b].push_back(a); } cin >> md; for(int i = 0; i < md; i++){ int a, b; cin >> a >> b; a--; b--; if(a == 0){ r2[b] = 1; } else if(b == 0){ r2[a] = 1; } else{ g2.push_back(make_pair(a, b)); } } return; } void bfs(queue <int> &q, vector <vector <int>> &v, vector <bool> &r1){ int x = q.front(); q.pop(); for(int i = 0; i < v[x].size(); i++){ if(!r1[v[x][i]]){ cout << "+ 1 " << (v[x][i] + 1) << '\n'; q.push(v[x][i]); r1[v[x][i]] = 1; } } return; } void solve(int n, int ms, int md, vector <vector <int>> &v, vector <bool> &r1, vector <bool> &r2, vector <pair <int, int>> &g1, vector <pair <int, int>> &g2){ int ans = 0; for(int i = 0; i < n; i++){ if(r2[i]){ ans++; } } cout << (int) (g1.size() + g2.size() + (n - 1 - v[0].size()) + (n - 1 - ans)) << '\n'; r1[0] = 1; queue <int> q; for(int i = 0; i < v[0].size(); i++){ q.push(v[0][i]); } while(!q.empty()){ bfs(q, v, r1); } for(int i = 0; i < g1.size(); i++){ cout << "- " << (g1[i].first + 1) << " " << (g1[i].second + 1) << '\n'; } for(int i = 0; i < g2.size(); i++){ cout << "+ " << (g2[i].first + 1) << " " << (g2[i].second + 1) << '\n'; } for(int i = 1; i < n; i++){ if(!r2[i]){ cout << "- 1 " << (i + 1) << '\n'; } } return; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, ms, md; vector <vector <int>> v; vector <bool> r1, r2; vector <pair <int, int>> g1, g2; input(n, ms, md, v, r1, r2, g1, g2); solve(n, ms, md, v, r1, r2, g1, g2); 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 131 132 133 134 135 136 137 138 139 140 141 | #include <bits/stdc++.h> using namespace std; void input(int &n, int &ms, int &md, vector <vector <int>> &v, vector <bool> &r1, vector <bool> &r2, vector <pair <int, int>> &g1, vector <pair <int, int>> &g2){ cin >> n; r1.resize(n); r2.resize(n); for(int i = 0; i < n; i++){ r1[i] = 0; r2[i] = 0; } cin >> ms; v.resize(n); for(int i = 0; i < ms; i++){ int a, b; cin >> a >> b; a--; b--; if(a == 0){ r1[b] = 1; } else if(b == 0){ r1[a] = 1; } else{ g1.push_back(make_pair(a, b)); } v[a].push_back(b); v[b].push_back(a); } cin >> md; for(int i = 0; i < md; i++){ int a, b; cin >> a >> b; a--; b--; if(a == 0){ r2[b] = 1; } else if(b == 0){ r2[a] = 1; } else{ g2.push_back(make_pair(a, b)); } } return; } void bfs(queue <int> &q, vector <vector <int>> &v, vector <bool> &r1){ int x = q.front(); q.pop(); for(int i = 0; i < v[x].size(); i++){ if(!r1[v[x][i]]){ cout << "+ 1 " << (v[x][i] + 1) << '\n'; q.push(v[x][i]); r1[v[x][i]] = 1; } } return; } void solve(int n, int ms, int md, vector <vector <int>> &v, vector <bool> &r1, vector <bool> &r2, vector <pair <int, int>> &g1, vector <pair <int, int>> &g2){ int ans = 0; for(int i = 0; i < n; i++){ if(r2[i]){ ans++; } } cout << (int) (g1.size() + g2.size() + (n - 1 - v[0].size()) + (n - 1 - ans)) << '\n'; r1[0] = 1; queue <int> q; for(int i = 0; i < v[0].size(); i++){ q.push(v[0][i]); } while(!q.empty()){ bfs(q, v, r1); } for(int i = 0; i < g1.size(); i++){ cout << "- " << (g1[i].first + 1) << " " << (g1[i].second + 1) << '\n'; } for(int i = 0; i < g2.size(); i++){ cout << "+ " << (g2[i].first + 1) << " " << (g2[i].second + 1) << '\n'; } for(int i = 1; i < n; i++){ if(!r2[i]){ cout << "- 1 " << (i + 1) << '\n'; } } return; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, ms, md; vector <vector <int>> v; vector <bool> r1, r2; vector <pair <int, int>> g1, g2; input(n, ms, md, v, r1, r2, g1, g2); solve(n, ms, md, v, r1, r2, g1, g2); return 0; } |