#include <bits/stdc++.h> using namespace std; #define fi first #define se second int n, m1, m2; vector<pair<int, int>> a; vector<pair<int, int>> b; const int MAXN = 3e4 + 7; vector<int> g[MAXN]; vector<int> naOdl[MAXN]; int dist[MAXN]; const int INF = 1e9; vector<int> g2[MAXN]; vector<int> naOdl2[MAXN]; int dist2[MAXN]; void bfs(){ for(int i = 1; i <= n; i++){ dist[i] = INF; } dist[1] = 0; queue<int> q; q.push(1); while(q.size()){ int v = q.front(); q.pop(); naOdl[dist[v]].push_back(v); for(auto u : g[v]){ if(dist[u] > dist[v] + 1){ dist[u] = dist[v] + 1; q.push(u); } } } } void bfs2(){ for(int i = 1; i <= n; i++){ dist2[i] = INF; } dist2[1] = 0; queue<int> q; q.push(1); while(q.size()){ int v = q.front(); q.pop(); naOdl2[dist2[v]].push_back(v); for(auto u : g2[v]){ if(dist2[u] > dist2[v] + 1){ dist2[u] = dist2[v] + 1; q.push(u); } } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n; //cout << n << '\n'; cin >> m1; //cout << m1 << '\n'; for(int i = 1; i <= m1; i++){ int x, y; cin >> x >> y; //cout << x << ' ' << y << '\n'; if(x > y){ swap(x, y); } g[x].push_back(y); g[y].push_back(x); a.push_back({x, y}); } cin >> m2; int kolega; //cout << m2 << '\n'; for(int i = 1; i <= m2; i++){ int x, y; cin >> x >> y; //cout << x << ' ' << y << '\n'; if(x > y){ swap(x, y); } if(x == 1){ kolega = y; } g2[x].push_back(y); g2[y].push_back(x); b.push_back({x, y}); } map<pair<int, int>, bool> in; for(auto ele : a){ in[ele] = true; } bfs(); bfs2(); vector<pair<char, pair<int, int>>> op; for(int i = 1; i <= n; i++){ for(auto u : naOdl[i]){ if(in[{1, u}]){ continue; } a.push_back({1, u}); op.push_back({'+', {1, u}}); in[{1, u}] = true; } } for(auto ele : b){ if(in[ele] == false){ in[ele] = true; a.push_back(ele); op.push_back({'+', ele}); } } map<pair<int, int>, bool> in2; for(auto ele : b){ in2[ele] = true; } for(auto ele : a){ if(in2[ele] == false && ele.first != 1){ op.push_back({'-', ele}); in2[ele] = true; } } //usun jedynki for(int i = n; i >= 1; i--){ for(auto u : naOdl2[i]){ if(in2[{1, u}] == false && in[{1, u}]){ op.push_back({'-', {1, u}}); } } } cout << op.size() << '\n'; for(auto ele : op){ cout << ele.fi << ' ' << ele.se.fi << ' ' << ele.se.se << '\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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <bits/stdc++.h> using namespace std; #define fi first #define se second int n, m1, m2; vector<pair<int, int>> a; vector<pair<int, int>> b; const int MAXN = 3e4 + 7; vector<int> g[MAXN]; vector<int> naOdl[MAXN]; int dist[MAXN]; const int INF = 1e9; vector<int> g2[MAXN]; vector<int> naOdl2[MAXN]; int dist2[MAXN]; void bfs(){ for(int i = 1; i <= n; i++){ dist[i] = INF; } dist[1] = 0; queue<int> q; q.push(1); while(q.size()){ int v = q.front(); q.pop(); naOdl[dist[v]].push_back(v); for(auto u : g[v]){ if(dist[u] > dist[v] + 1){ dist[u] = dist[v] + 1; q.push(u); } } } } void bfs2(){ for(int i = 1; i <= n; i++){ dist2[i] = INF; } dist2[1] = 0; queue<int> q; q.push(1); while(q.size()){ int v = q.front(); q.pop(); naOdl2[dist2[v]].push_back(v); for(auto u : g2[v]){ if(dist2[u] > dist2[v] + 1){ dist2[u] = dist2[v] + 1; q.push(u); } } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n; //cout << n << '\n'; cin >> m1; //cout << m1 << '\n'; for(int i = 1; i <= m1; i++){ int x, y; cin >> x >> y; //cout << x << ' ' << y << '\n'; if(x > y){ swap(x, y); } g[x].push_back(y); g[y].push_back(x); a.push_back({x, y}); } cin >> m2; int kolega; //cout << m2 << '\n'; for(int i = 1; i <= m2; i++){ int x, y; cin >> x >> y; //cout << x << ' ' << y << '\n'; if(x > y){ swap(x, y); } if(x == 1){ kolega = y; } g2[x].push_back(y); g2[y].push_back(x); b.push_back({x, y}); } map<pair<int, int>, bool> in; for(auto ele : a){ in[ele] = true; } bfs(); bfs2(); vector<pair<char, pair<int, int>>> op; for(int i = 1; i <= n; i++){ for(auto u : naOdl[i]){ if(in[{1, u}]){ continue; } a.push_back({1, u}); op.push_back({'+', {1, u}}); in[{1, u}] = true; } } for(auto ele : b){ if(in[ele] == false){ in[ele] = true; a.push_back(ele); op.push_back({'+', ele}); } } map<pair<int, int>, bool> in2; for(auto ele : b){ in2[ele] = true; } for(auto ele : a){ if(in2[ele] == false && ele.first != 1){ op.push_back({'-', ele}); in2[ele] = true; } } //usun jedynki for(int i = n; i >= 1; i--){ for(auto u : naOdl2[i]){ if(in2[{1, u}] == false && in[{1, u}]){ op.push_back({'-', {1, u}}); } } } cout << op.size() << '\n'; for(auto ele : op){ cout << ele.fi << ' ' << ele.se.fi << ' ' << ele.se.se << '\n'; } return 0; } |