// Jan Zakrzewski #include <bits/stdc++.h> using namespace std; int constexpr N = 30'010; enum { PLUS, MINUS }; enum { C = 1 }; int n, m_1, m_2; vector<int> adj_1[N], adj_2[N]; queue<int> Queue; bool Visited[N]; vector<tuple<int,int,int>> op_1, op_2; vector<tuple<int,int,int>> answer; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int u = 1; u <= n; ++u) { adj_1[u].clear(); adj_2[u].clear(); } cin >> m_1; for(int i = 1; i <= m_1; ++i) { int u, v; cin >> u >> v; adj_1[u].push_back(v); adj_1[v].push_back(u); } cin >> m_2; for(int i = 1; i <= m_2; ++i) { int u, v; cin >> u >> v; adj_2[u].push_back(v); adj_2[v].push_back(u); } op_1.clear(); op_2.clear(); while(!Queue.empty()) Queue.pop(); for(int u = 1; u <= n; ++u) Visited[u] = false; Visited[C] = true; for(int u : adj_1[C]) { Queue.push(u); Visited[u] = true; } while(!Queue.empty()) { int u = Queue.front(); Queue.pop(); for(int v : adj_1[u]) { if(Visited[v]) continue; Visited[v] = true; Queue.push(v); op_1.push_back(make_tuple(PLUS, C, v)); } } for(int u = 1; u <= n; ++u) { for(int v : adj_1[u]) { if(u == C) continue; if(v == C) continue; if(u >= v) continue; op_1.push_back(make_tuple(MINUS, u, v)); } } while(!Queue.empty()) Queue.pop(); for(int u = 1; u <= n; ++u) Visited[u] = false; Visited[C] = true; for(int u : adj_2[C]) { Queue.push(u); Visited[u] = true; } while(!Queue.empty()) { int u = Queue.front(); Queue.pop(); for(int v : adj_2[u]) { if(Visited[v]) continue; Visited[v] = true; Queue.push(v); op_2.push_back(make_tuple(PLUS, C, v)); } } for(int u = 1; u <= n; ++u) { for(int v : adj_2[u]) { if(u == C) continue; if(v == C) continue; if(u >= v) continue; op_2.push_back(make_tuple(MINUS, u, v)); } } answer.clear(); for(auto it = op_1.begin(); it != op_1.end(); ++it) { tuple<int,int,int> elm = *it; answer.push_back(elm); } for(auto it = op_2.rbegin(); it != op_2.rend(); ++it) { tuple<int,int,int> elm = *it; switch(get<0>(elm)) { case PLUS: get<0>(elm) = MINUS; break; case MINUS: get<0>(elm) = PLUS; break; } answer.push_back(elm); } cout << (int) answer.size() << "\n"; for(tuple<int,int,int> elm : answer) { switch(get<0>(elm)) { case PLUS: cout << "+ "; break; case MINUS: cout << "- "; break; } cout << get<1>(elm) << " " << get<2>(elm) << "\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 | // Jan Zakrzewski #include <bits/stdc++.h> using namespace std; int constexpr N = 30'010; enum { PLUS, MINUS }; enum { C = 1 }; int n, m_1, m_2; vector<int> adj_1[N], adj_2[N]; queue<int> Queue; bool Visited[N]; vector<tuple<int,int,int>> op_1, op_2; vector<tuple<int,int,int>> answer; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int u = 1; u <= n; ++u) { adj_1[u].clear(); adj_2[u].clear(); } cin >> m_1; for(int i = 1; i <= m_1; ++i) { int u, v; cin >> u >> v; adj_1[u].push_back(v); adj_1[v].push_back(u); } cin >> m_2; for(int i = 1; i <= m_2; ++i) { int u, v; cin >> u >> v; adj_2[u].push_back(v); adj_2[v].push_back(u); } op_1.clear(); op_2.clear(); while(!Queue.empty()) Queue.pop(); for(int u = 1; u <= n; ++u) Visited[u] = false; Visited[C] = true; for(int u : adj_1[C]) { Queue.push(u); Visited[u] = true; } while(!Queue.empty()) { int u = Queue.front(); Queue.pop(); for(int v : adj_1[u]) { if(Visited[v]) continue; Visited[v] = true; Queue.push(v); op_1.push_back(make_tuple(PLUS, C, v)); } } for(int u = 1; u <= n; ++u) { for(int v : adj_1[u]) { if(u == C) continue; if(v == C) continue; if(u >= v) continue; op_1.push_back(make_tuple(MINUS, u, v)); } } while(!Queue.empty()) Queue.pop(); for(int u = 1; u <= n; ++u) Visited[u] = false; Visited[C] = true; for(int u : adj_2[C]) { Queue.push(u); Visited[u] = true; } while(!Queue.empty()) { int u = Queue.front(); Queue.pop(); for(int v : adj_2[u]) { if(Visited[v]) continue; Visited[v] = true; Queue.push(v); op_2.push_back(make_tuple(PLUS, C, v)); } } for(int u = 1; u <= n; ++u) { for(int v : adj_2[u]) { if(u == C) continue; if(v == C) continue; if(u >= v) continue; op_2.push_back(make_tuple(MINUS, u, v)); } } answer.clear(); for(auto it = op_1.begin(); it != op_1.end(); ++it) { tuple<int,int,int> elm = *it; answer.push_back(elm); } for(auto it = op_2.rbegin(); it != op_2.rend(); ++it) { tuple<int,int,int> elm = *it; switch(get<0>(elm)) { case PLUS: get<0>(elm) = MINUS; break; case MINUS: get<0>(elm) = PLUS; break; } answer.push_back(elm); } cout << (int) answer.size() << "\n"; for(tuple<int,int,int> elm : answer) { switch(get<0>(elm)) { case PLUS: cout << "+ "; break; case MINUS: cout << "- "; break; } cout << get<1>(elm) << " " << get<2>(elm) << "\n"; } return 0; } |