//Solution by Mikołaj Kołek
#include "bits/stdc++.h"
#define intin *istream_iterator<int>(cin)
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n = intin, ms = intin;
vector<unordered_set<int>> startingG(n), finalG(n);
for(int i = 0; i < ms; i++) {
int a = intin - 1, b = intin - 1;
startingG[a].insert(b);
startingG[b].insert(a);
}
int md = intin;
for(int i = 0; i < md; i++) {
int a = intin - 1, b = intin - 1;
finalG[a].insert(b);
finalG[b].insert(a);
}
vector<string> result;
unordered_set<int> vis;
function<void(int, int)> DFS = [&] (int v, int p) {
vis.insert(v);
if(!startingG[v].count(0) and v != 0)
result.push_back("+ " + to_string(1) + " " + to_string(v + 1));
for(const auto &el : startingG[v]) {
if(vis.count(el))
continue;
DFS(el, v);
}
};
DFS(0, -1);
//unordered_map<int, unordered_set<int>> done;
for(int i = 1; i < n; i++) {
for(const auto &v : startingG[i]) {
if(v == 0)
continue;
if(!finalG[i].count(v)) {
result.push_back("- " + to_string(i + 1) + " " + to_string(v + 1));
startingG[v].erase(i);
}
}
for(const auto &v : finalG[i]) {
if(v == 0)
continue;
if(!startingG[i].count(v)) {
result.push_back("+ " + to_string(i + 1) + " " + to_string(v + 1));
finalG[v].erase(i);
}
}
}
vis = unordered_set<int>();
function<void(int, int)> DFS2 = [&] (int v, int p) {
vis.insert(v);
for(const auto &el : finalG[v]) {
if(vis.count(el))
continue;
DFS2(el, v);
}
if(v != 0 and !finalG[v].count(0))
result.push_back("- " + to_string(1) + " " + to_string(v + 1));
};
DFS2(0, -1);
cout << result.size() << "\n";
for(const auto &line : result)
cout << line << "\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 | //Solution by Mikołaj Kołek #include "bits/stdc++.h" #define intin *istream_iterator<int>(cin) using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n = intin, ms = intin; vector<unordered_set<int>> startingG(n), finalG(n); for(int i = 0; i < ms; i++) { int a = intin - 1, b = intin - 1; startingG[a].insert(b); startingG[b].insert(a); } int md = intin; for(int i = 0; i < md; i++) { int a = intin - 1, b = intin - 1; finalG[a].insert(b); finalG[b].insert(a); } vector<string> result; unordered_set<int> vis; function<void(int, int)> DFS = [&] (int v, int p) { vis.insert(v); if(!startingG[v].count(0) and v != 0) result.push_back("+ " + to_string(1) + " " + to_string(v + 1)); for(const auto &el : startingG[v]) { if(vis.count(el)) continue; DFS(el, v); } }; DFS(0, -1); //unordered_map<int, unordered_set<int>> done; for(int i = 1; i < n; i++) { for(const auto &v : startingG[i]) { if(v == 0) continue; if(!finalG[i].count(v)) { result.push_back("- " + to_string(i + 1) + " " + to_string(v + 1)); startingG[v].erase(i); } } for(const auto &v : finalG[i]) { if(v == 0) continue; if(!startingG[i].count(v)) { result.push_back("+ " + to_string(i + 1) + " " + to_string(v + 1)); finalG[v].erase(i); } } } vis = unordered_set<int>(); function<void(int, int)> DFS2 = [&] (int v, int p) { vis.insert(v); for(const auto &el : finalG[v]) { if(vis.count(el)) continue; DFS2(el, v); } if(v != 0 and !finalG[v].count(0)) result.push_back("- " + to_string(1) + " " + to_string(v + 1)); }; DFS2(0, -1); cout << result.size() << "\n"; for(const auto &line : result) cout << line << "\n"; return 0; } |
English