#include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define pb push_back #define nd second #define st first #define sz size #define forr(i, n) for(int i=1;i<=n;i++) const ll infl=1e18+90; const int inf=1e9+90; const int roz=1e5; int odw[roz], odw2[roz]; set<pair<int,int> > M, M2; vector<pair<int,int> > wek; vector<int> graf[roz], graf2[roz]; vector<pair<char, pair<int,int> > > oper; pair<int,int> pop(pair<int,int> a) { if(a.st>a.nd) swap(a.st, a.nd); return a; } void dfs(int u, int o) { odw[u]=1; if(u!=1) { if(!M.count({1, u})) { M.insert({1, u}); oper.pb({'+' ,{1, u}}); wek.pb({1, u}); } } for(auto v:graf[u]) { if(odw[v]==1) continue; dfs(v, u); } } void dfs2(int u, int o) { odw2[u]=1; for(auto v:graf2[u]) { if(odw2[v]==1) continue; dfs2(v, u); } for(auto it:graf[u]) { if(it==1) continue; if(!M2.count(pop({u, it}))) { if(M.count(pop({u, it}))) { oper.pb({'-', {u, it}}); M.erase(pop({u, it})); } } } if(M.count({1, u})) { if(!M2.count({1, u})) { oper.pb({'-', {1, u}}); M.erase(pop({1, u})); } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, a, b; cin>>n; int m; cin>>m; for(int i=1;i<=m;i++) { cin>>a>>b; graf[a].pb(b); graf[b].pb(a); pair<int,int> c={a, b}; c=pop(c); wek.pb(c); M.insert(c); } dfs(1, 0); int m2; cin>>m2; for(int i=1;i<=m2;i++) { cin>>a>>b; pair<int,int> c={a, b}; c=pop(c); graf2[c.st].pb(c.nd); graf2[c.nd].pb(c.st); M2.insert(c); if(!M.count(c)) { oper.pb({'+', c}); } } dfs2(1, 0); cout<<oper.size()<<"\n"; for(auto [a, p]:oper) { cout<<a<<" "<<p.st<<" "<<p.nd<<"\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 112 113 114 115 116 117 118 119 | #include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define pb push_back #define nd second #define st first #define sz size #define forr(i, n) for(int i=1;i<=n;i++) const ll infl=1e18+90; const int inf=1e9+90; const int roz=1e5; int odw[roz], odw2[roz]; set<pair<int,int> > M, M2; vector<pair<int,int> > wek; vector<int> graf[roz], graf2[roz]; vector<pair<char, pair<int,int> > > oper; pair<int,int> pop(pair<int,int> a) { if(a.st>a.nd) swap(a.st, a.nd); return a; } void dfs(int u, int o) { odw[u]=1; if(u!=1) { if(!M.count({1, u})) { M.insert({1, u}); oper.pb({'+' ,{1, u}}); wek.pb({1, u}); } } for(auto v:graf[u]) { if(odw[v]==1) continue; dfs(v, u); } } void dfs2(int u, int o) { odw2[u]=1; for(auto v:graf2[u]) { if(odw2[v]==1) continue; dfs2(v, u); } for(auto it:graf[u]) { if(it==1) continue; if(!M2.count(pop({u, it}))) { if(M.count(pop({u, it}))) { oper.pb({'-', {u, it}}); M.erase(pop({u, it})); } } } if(M.count({1, u})) { if(!M2.count({1, u})) { oper.pb({'-', {1, u}}); M.erase(pop({1, u})); } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, a, b; cin>>n; int m; cin>>m; for(int i=1;i<=m;i++) { cin>>a>>b; graf[a].pb(b); graf[b].pb(a); pair<int,int> c={a, b}; c=pop(c); wek.pb(c); M.insert(c); } dfs(1, 0); int m2; cin>>m2; for(int i=1;i<=m2;i++) { cin>>a>>b; pair<int,int> c={a, b}; c=pop(c); graf2[c.st].pb(c.nd); graf2[c.nd].pb(c.st); M2.insert(c); if(!M.count(c)) { oper.pb({'+', c}); } } dfs2(1, 0); cout<<oper.size()<<"\n"; for(auto [a, p]:oper) { cout<<a<<" "<<p.st<<" "<<p.nd<<"\n"; } } |