#include <bits/stdc++.h> #define pb push_back #define inf 999999999 #define int long long #define turbo ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; const int N = 30015; vector<int> graph[N]; vector<int> new_graph[N]; vector<pair<char, pair<int, int>>> result; bool visited[N]; bool new_visited[N]; int dist[N]; int new_dist[N]; vector <int> new_bfs_order; void bfs(int u) { queue<int> q; q.push(u); visited[u] = true; dist[u] = 0; while(!q.empty()) { int v = q.front(); q.pop(); if (dist[v] > 1) { result.pb({'+', {1, v}}); } for(int i = 0; i < graph[v].size(); i++) { int to = graph[v][i]; if(!visited[to]) { visited[to] = true; dist[to] = dist[v] + 1; q.push(to); } } } } void new_bfs(int u) { queue<int> q; q.push(u); new_visited[u] = true; new_dist[u] = 0; while(!q.empty()) { int v = q.front(); q.pop(); new_bfs_order.pb(v); for(int i = 0; i < new_graph[v].size(); i++) { int to = new_graph[v][i]; if(!new_visited[to]) { new_visited[to] = true; new_dist[to] = new_dist[v] + 1; q.push(to); } } } } signed main() { turbo; int n, m; cin >> n >> m; set <pair<int, int>> edges; set <pair<int, int>> new_edges; for(int i = 0; i < m; i++) { int u, v; cin >> u >> v; graph[u].pb(v); graph[v].pb(u); edges.insert({min(u, v), max(u, v)}); } int new_m; cin >> new_m; for(int i = 0; i < new_m; i++) { int u, v; cin >> u >> v; new_graph[u].pb(v); new_graph[v].pb(u); new_edges.insert({min(u, v), max(u, v)}); } bfs(1); for (auto i : new_edges) { if (i.first != 1 && edges.find(i) == edges.end()) { result.pb({'+', i}); } } for (auto i : edges) { if (i.first != 1 && new_edges.find(i) == new_edges.end()) { result.pb({'-', i}); } } new_bfs(1); reverse(new_bfs_order.begin(), new_bfs_order.end()); for (auto i : new_bfs_order) { if (i != 1 && new_edges.find({1, i}) == new_edges.end()) { result.pb({'-', {1, i}}); } } cout << result.size() << endl; for (auto i : result) { cout << i.first << " " << i.second.first << " " << i.second.second << endl; } 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 | #include <bits/stdc++.h> #define pb push_back #define inf 999999999 #define int long long #define turbo ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; const int N = 30015; vector<int> graph[N]; vector<int> new_graph[N]; vector<pair<char, pair<int, int>>> result; bool visited[N]; bool new_visited[N]; int dist[N]; int new_dist[N]; vector <int> new_bfs_order; void bfs(int u) { queue<int> q; q.push(u); visited[u] = true; dist[u] = 0; while(!q.empty()) { int v = q.front(); q.pop(); if (dist[v] > 1) { result.pb({'+', {1, v}}); } for(int i = 0; i < graph[v].size(); i++) { int to = graph[v][i]; if(!visited[to]) { visited[to] = true; dist[to] = dist[v] + 1; q.push(to); } } } } void new_bfs(int u) { queue<int> q; q.push(u); new_visited[u] = true; new_dist[u] = 0; while(!q.empty()) { int v = q.front(); q.pop(); new_bfs_order.pb(v); for(int i = 0; i < new_graph[v].size(); i++) { int to = new_graph[v][i]; if(!new_visited[to]) { new_visited[to] = true; new_dist[to] = new_dist[v] + 1; q.push(to); } } } } signed main() { turbo; int n, m; cin >> n >> m; set <pair<int, int>> edges; set <pair<int, int>> new_edges; for(int i = 0; i < m; i++) { int u, v; cin >> u >> v; graph[u].pb(v); graph[v].pb(u); edges.insert({min(u, v), max(u, v)}); } int new_m; cin >> new_m; for(int i = 0; i < new_m; i++) { int u, v; cin >> u >> v; new_graph[u].pb(v); new_graph[v].pb(u); new_edges.insert({min(u, v), max(u, v)}); } bfs(1); for (auto i : new_edges) { if (i.first != 1 && edges.find(i) == edges.end()) { result.pb({'+', i}); } } for (auto i : edges) { if (i.first != 1 && new_edges.find(i) == new_edges.end()) { result.pb({'-', i}); } } new_bfs(1); reverse(new_bfs_order.begin(), new_bfs_order.end()); for (auto i : new_bfs_order) { if (i != 1 && new_edges.find({1, i}) == new_edges.end()) { result.pb({'-', {1, i}}); } } cout << result.size() << endl; for (auto i : result) { cout << i.first << " " << i.second.first << " " << i.second.second << endl; } return 0; } |