#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
struct op {
bool c;
int a, b;
};
set <pair <int, int> > ist;
set <pair <int, int> > ost;
vector <vector <int> > g1, g2;
vector <op> res;
void dfsdod(int v, vector <bool> &odw) {
odw[v] = 1;
pair <int, int> temp = {0, v};
if (v != 0 && ist.find(temp) == ist.end()) {
op x = {1, 0, v};
res.push_back(x);
}
for (auto w : g1[v]) {
if (!odw[w]) {
dfsdod(w, odw);
}
}
}
void dfsusu(int v, vector <bool> &odw) {
odw[v] = 1;
pair <int, int> temp = {0, v};
for (auto w : g2[v]) {
if (!odw[w]) {
dfsusu(w, odw);
}
}
if (ost.find(temp) == ost.end()) {
op x = {0, temp.ff, temp.ss};
res.push_back(x);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m1, m2;
cin >> n;
cin >> m1;
set <pair <int, int> > dousu;
set <pair <int, int> > dodod;
g1.resize(n);
g2.resize(n);
for (int i = 0; i < m1; i++) {
int a, b;
cin >> a >> b;
a--; b--;
if (a > b)
swap(a, b);
dousu.insert({a, b});
ist.insert({a, b});
g1[a].push_back(b);
g1[b].push_back(a);
}
cin >> m2;
for (int i = 0; i < m2; i++) {
int a, b;
cin >> a >> b;
a--; b--;
if (a > b)
swap(a, b);
pair <int, int> para = {a, b};
ost.insert({a, b});
if (dousu.find(para) == dousu.end())
dodod.insert(para);
else
dousu.erase(dousu.find(para));
g2[a].push_back(b);
g2[b].push_back(a);
}
vector <bool> odw(n);
dfsdod(0, odw);
for (auto it = dodod.begin(); it != dodod.end(); it++) {
if ((*it).ff == 0)
continue;
op x = {1, (*it).ff, (*it).ss};
res.push_back(x);
}
for (auto it = dousu.begin(); it != dousu.end(); it++) {
if ((*it).ff == 0)
continue;
op x = {0, (*it).ff, (*it).ss};
res.push_back(x);
}
odw.clear();
odw.resize(n);
odw[0] = 1;
for (auto i : g2[0])
if (!odw[i])
dfsusu(i, odw);
cout << res.size() << "\n";
for (auto i : res) {
if (i.c)
cout << "+ ";
else
cout << "- ";
cout << i.a+1 << " " << i.b+1 << "\n";
}
}
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 | #include <bits/stdc++.h> using namespace std; #define ff first #define ss second struct op { bool c; int a, b; }; set <pair <int, int> > ist; set <pair <int, int> > ost; vector <vector <int> > g1, g2; vector <op> res; void dfsdod(int v, vector <bool> &odw) { odw[v] = 1; pair <int, int> temp = {0, v}; if (v != 0 && ist.find(temp) == ist.end()) { op x = {1, 0, v}; res.push_back(x); } for (auto w : g1[v]) { if (!odw[w]) { dfsdod(w, odw); } } } void dfsusu(int v, vector <bool> &odw) { odw[v] = 1; pair <int, int> temp = {0, v}; for (auto w : g2[v]) { if (!odw[w]) { dfsusu(w, odw); } } if (ost.find(temp) == ost.end()) { op x = {0, temp.ff, temp.ss}; res.push_back(x); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m1, m2; cin >> n; cin >> m1; set <pair <int, int> > dousu; set <pair <int, int> > dodod; g1.resize(n); g2.resize(n); for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; a--; b--; if (a > b) swap(a, b); dousu.insert({a, b}); ist.insert({a, b}); g1[a].push_back(b); g1[b].push_back(a); } cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; a--; b--; if (a > b) swap(a, b); pair <int, int> para = {a, b}; ost.insert({a, b}); if (dousu.find(para) == dousu.end()) dodod.insert(para); else dousu.erase(dousu.find(para)); g2[a].push_back(b); g2[b].push_back(a); } vector <bool> odw(n); dfsdod(0, odw); for (auto it = dodod.begin(); it != dodod.end(); it++) { if ((*it).ff == 0) continue; op x = {1, (*it).ff, (*it).ss}; res.push_back(x); } for (auto it = dousu.begin(); it != dousu.end(); it++) { if ((*it).ff == 0) continue; op x = {0, (*it).ff, (*it).ss}; res.push_back(x); } odw.clear(); odw.resize(n); odw[0] = 1; for (auto i : g2[0]) if (!odw[i]) dfsusu(i, odw); cout << res.size() << "\n"; for (auto i : res) { if (i.c) cout << "+ "; else cout << "- "; cout << i.a+1 << " " << i.b+1 << "\n"; } } |
English