#include<bits/stdc++.h> #define MAXN 30311 using namespace std; struct ans { char s; int a; int b; }; ans newAns(char s, int a, int b) { ans e; e.s = s; e.a = a; e.b = b; return e; } int n, ms, md, r, a, b; vector<int> gs[MAXN]; vector<int> gd[MAXN]; bool visitedS[MAXN]; bool visitedD[MAXN]; queue<ans> w; set<pair<int, int> > initial; set<pair<int, int> > target; set<pair<int, int> > added; void dfsS(int x) { if (x != 1 && initial.find({1, x}) == initial.end()) { added.insert({1, x}); w.push(newAns('+', 1, x)); } visitedS[x] = true; for (int i = 0; i < gs[x].size(); i++) { if (!visitedS[gs[x][i]]) { dfsS(gs[x][i]); } } } void dfsD(int x) { visitedD[x] = true; for (int i = 0; i < gd[x].size(); i++) { if (!visitedD[gd[x][i]]) { dfsD(gd[x][i]); } } if (x != 1 && target.find({1, x}) == target.end()) { w.push(newAns('-', 1, x)); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); r = 0; cin >> n >> ms; for (int i = 0; i < n; i++) { visitedS[i] = false; visitedD[i] = false; } for (int i = 0; i < ms; i++) { cin >> a >> b; gs[a].push_back(b); gs[b].push_back(a); if (a > b) { swap(a, b); } initial.insert({a, b}); } cin >> md; for (int i = 0; i < md; i++) { cin >> a >> b; gd[a].push_back(b); gd[b].push_back(a); if (a > b) { swap(a, b); } target.insert({a, b}); } dfsS(1); for (pair<int, int> e : target) { if (initial.find(e) == initial.end() && added.find(e) == added.end()) { added.insert(e); w.push(newAns('+', e.first, e.second)); } } for (pair<int, int> e : initial) { if (e.first != 1 && target.find(e) == target.end()) { w.push(newAns('-', e.first, e.second)); } } dfsD(1); cout<<w.size()<<"\n"; while (!w.empty()) { ans e = w.front(); w.pop(); cout<<e.s<<" "<<e.a<<" "<<e.b<<"\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 | #include<bits/stdc++.h> #define MAXN 30311 using namespace std; struct ans { char s; int a; int b; }; ans newAns(char s, int a, int b) { ans e; e.s = s; e.a = a; e.b = b; return e; } int n, ms, md, r, a, b; vector<int> gs[MAXN]; vector<int> gd[MAXN]; bool visitedS[MAXN]; bool visitedD[MAXN]; queue<ans> w; set<pair<int, int> > initial; set<pair<int, int> > target; set<pair<int, int> > added; void dfsS(int x) { if (x != 1 && initial.find({1, x}) == initial.end()) { added.insert({1, x}); w.push(newAns('+', 1, x)); } visitedS[x] = true; for (int i = 0; i < gs[x].size(); i++) { if (!visitedS[gs[x][i]]) { dfsS(gs[x][i]); } } } void dfsD(int x) { visitedD[x] = true; for (int i = 0; i < gd[x].size(); i++) { if (!visitedD[gd[x][i]]) { dfsD(gd[x][i]); } } if (x != 1 && target.find({1, x}) == target.end()) { w.push(newAns('-', 1, x)); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); r = 0; cin >> n >> ms; for (int i = 0; i < n; i++) { visitedS[i] = false; visitedD[i] = false; } for (int i = 0; i < ms; i++) { cin >> a >> b; gs[a].push_back(b); gs[b].push_back(a); if (a > b) { swap(a, b); } initial.insert({a, b}); } cin >> md; for (int i = 0; i < md; i++) { cin >> a >> b; gd[a].push_back(b); gd[b].push_back(a); if (a > b) { swap(a, b); } target.insert({a, b}); } dfsS(1); for (pair<int, int> e : target) { if (initial.find(e) == initial.end() && added.find(e) == added.end()) { added.insert(e); w.push(newAns('+', e.first, e.second)); } } for (pair<int, int> e : initial) { if (e.first != 1 && target.find(e) == target.end()) { w.push(newAns('-', e.first, e.second)); } } dfsD(1); cout<<w.size()<<"\n"; while (!w.empty()) { ans e = w.front(); w.pop(); cout<<e.s<<" "<<e.a<<" "<<e.b<<"\n"; } return 0; } |