#include <bits/stdc++.h> using namespace std; const int N = 30007; vector<pair<int,int> > G[N]; vector<pair<int,pair<int,int> > > ops; set<pair<int,int> > S; void add(int a,int b){ if (a>b){ swap(a,b); } if (!S.count({a,b})){ ops.push_back({0,{a,b}}); S.insert({a,b}); } } void del(int a,int b){ if (a>b){ swap(a,b); } assert(S.count({a,b})); ops.push_back({1,{a,b}}); S.erase({a,b}); } bool vis[N]; void hull(int v){ vis[v] = 1; bool r = (v==1); for(auto [to,col]:G[v]){ if (to==1){ r = true; } } if (!r){ add(1,v); } for(auto [to,col]:G[v]){ if (!vis[to]){ hull(to); } } } void clr(int v){ vis[v] = 1; bool r = (v==1); for(auto [to,col]:G[v]){ if (col==0){ continue; } if (to==1){ r = true; } if (!vis[to]){ clr(to); } } if (!r){ del(1,v); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin>>n>>m; vector<pair<int,int> > E; for(int i = 1;i<=m;i+=1){ int a,b; cin>>a>>b; if (a>b){ swap(a,b); } S.insert({a,b}); E.push_back({a,b}); G[a].push_back({b,0}); G[b].push_back({a,0}); } hull(1); cin>>m; set<pair<int,int> > O; for(int i = 1;i<=m;i+=1){ int a,b; cin>>a>>b; if (a>b){ swap(a,b); } O.insert({a,b}); if (a!=1 and b!=1){ add(a,b); } G[a].push_back({b,2}); G[b].push_back({a,2}); } for(auto [a,b]:E){ if (a!=1 and b!=1 and !O.count({a,b})){ del(a,b); } } for(int i = 1;i<=n;i+=1){ vis[i] = false; } clr(1); cout<<ops.size()<<'\n'; for(auto [sgn,e]:ops){ cout<<(sgn?'-':'+')<<' '<<e.first<<' '<<e.second<<'\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 | #include <bits/stdc++.h> using namespace std; const int N = 30007; vector<pair<int,int> > G[N]; vector<pair<int,pair<int,int> > > ops; set<pair<int,int> > S; void add(int a,int b){ if (a>b){ swap(a,b); } if (!S.count({a,b})){ ops.push_back({0,{a,b}}); S.insert({a,b}); } } void del(int a,int b){ if (a>b){ swap(a,b); } assert(S.count({a,b})); ops.push_back({1,{a,b}}); S.erase({a,b}); } bool vis[N]; void hull(int v){ vis[v] = 1; bool r = (v==1); for(auto [to,col]:G[v]){ if (to==1){ r = true; } } if (!r){ add(1,v); } for(auto [to,col]:G[v]){ if (!vis[to]){ hull(to); } } } void clr(int v){ vis[v] = 1; bool r = (v==1); for(auto [to,col]:G[v]){ if (col==0){ continue; } if (to==1){ r = true; } if (!vis[to]){ clr(to); } } if (!r){ del(1,v); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin>>n>>m; vector<pair<int,int> > E; for(int i = 1;i<=m;i+=1){ int a,b; cin>>a>>b; if (a>b){ swap(a,b); } S.insert({a,b}); E.push_back({a,b}); G[a].push_back({b,0}); G[b].push_back({a,0}); } hull(1); cin>>m; set<pair<int,int> > O; for(int i = 1;i<=m;i+=1){ int a,b; cin>>a>>b; if (a>b){ swap(a,b); } O.insert({a,b}); if (a!=1 and b!=1){ add(a,b); } G[a].push_back({b,2}); G[b].push_back({a,2}); } for(auto [a,b]:E){ if (a!=1 and b!=1 and !O.count({a,b})){ del(a,b); } } for(int i = 1;i<=n;i+=1){ vis[i] = false; } clr(1); cout<<ops.size()<<'\n'; for(auto [sgn,e]:ops){ cout<<(sgn?'-':'+')<<' '<<e.first<<' '<<e.second<<'\n'; } return 0; } |