#include<bits/stdc++.h> using namespace std; #define st first #define nd second vector<string>wyn; void q(vector<vector<int>>&b , int i, vector<bool>&czy,map<pair<int,int>,bool>&ma1) { if(czy[i]) { return; } czy[i]=true; if(!ma1[{0,i}] && i!=0) { wyn.push_back("+ 1 " + to_string(i+1)); } for(int j=0;j<b[i].size();j++) { q(b,b[i][j],czy,ma1); } } void q2(vector<vector<int>>&d , int i, vector<bool>&cz,map<pair<int,int>,bool>&ma2) { if(cz[i]) { return; } cz[i]=true; for(int j=0;j<d[i].size();j++) { q2(d,d[i][j],cz,ma2); } if(!ma2[{0,i}] && i!=0) { wyn.push_back("- 1 " + to_string(i+1)); } } int main() { ios_base::sync_with_stdio(false);cin.tie(0); int n; cin>>n; int m1; cin>>m1; vector<pair<int,int>>a(m1); vector<vector<int>>b(n); map<pair<int,int>,bool>ma1; for(int i=0;i<m1;i++) { cin>>a[i].st>>a[i].nd; a[i].st--; a[i].nd--; if(a[i].st>a[i].nd) { swap(a[i].st,a[i].nd); } ma1[a[i]]=true; b[a[i].st].push_back(a[i].nd); b[a[i].nd].push_back(a[i].st); } int m2; cin>>m2; vector<pair<int,int>>c(m2); vector<vector<int>>d(n); map<pair<int,int>,bool>ma2; for(int i=0;i<m2;i++) { cin>>c[i].st>>c[i].nd; c[i].st--; c[i].nd--; if(c[i].st>c[i].nd) { swap(c[i].st,c[i].nd); } ma2[c[i]]=true; d[c[i].st].push_back(c[i].nd); d[c[i].nd].push_back(c[i].st); } vector<bool>czy(n); q(b,0,czy,ma1); for(int i=0;i<m1;i++) { if(ma2[a[i]]==false && a[i].st!=0) { wyn.push_back("- " + to_string(a[i].st+1)+" "+to_string(a[i].nd+1)); } } for(int i=0;i<m2;i++) { if(ma1[c[i]]==false && c[i].st!=0) { wyn.push_back("+ " + to_string(c[i].st+1)+" "+to_string(c[i].nd+1)); } } vector<bool>cz(n); q2(d,0,cz,ma2); cout<<wyn.size()<<endl; for(int i=0;i<wyn.size();i++) { cout<<wyn[i]<<endl; } }
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 | #include<bits/stdc++.h> using namespace std; #define st first #define nd second vector<string>wyn; void q(vector<vector<int>>&b , int i, vector<bool>&czy,map<pair<int,int>,bool>&ma1) { if(czy[i]) { return; } czy[i]=true; if(!ma1[{0,i}] && i!=0) { wyn.push_back("+ 1 " + to_string(i+1)); } for(int j=0;j<b[i].size();j++) { q(b,b[i][j],czy,ma1); } } void q2(vector<vector<int>>&d , int i, vector<bool>&cz,map<pair<int,int>,bool>&ma2) { if(cz[i]) { return; } cz[i]=true; for(int j=0;j<d[i].size();j++) { q2(d,d[i][j],cz,ma2); } if(!ma2[{0,i}] && i!=0) { wyn.push_back("- 1 " + to_string(i+1)); } } int main() { ios_base::sync_with_stdio(false);cin.tie(0); int n; cin>>n; int m1; cin>>m1; vector<pair<int,int>>a(m1); vector<vector<int>>b(n); map<pair<int,int>,bool>ma1; for(int i=0;i<m1;i++) { cin>>a[i].st>>a[i].nd; a[i].st--; a[i].nd--; if(a[i].st>a[i].nd) { swap(a[i].st,a[i].nd); } ma1[a[i]]=true; b[a[i].st].push_back(a[i].nd); b[a[i].nd].push_back(a[i].st); } int m2; cin>>m2; vector<pair<int,int>>c(m2); vector<vector<int>>d(n); map<pair<int,int>,bool>ma2; for(int i=0;i<m2;i++) { cin>>c[i].st>>c[i].nd; c[i].st--; c[i].nd--; if(c[i].st>c[i].nd) { swap(c[i].st,c[i].nd); } ma2[c[i]]=true; d[c[i].st].push_back(c[i].nd); d[c[i].nd].push_back(c[i].st); } vector<bool>czy(n); q(b,0,czy,ma1); for(int i=0;i<m1;i++) { if(ma2[a[i]]==false && a[i].st!=0) { wyn.push_back("- " + to_string(a[i].st+1)+" "+to_string(a[i].nd+1)); } } for(int i=0;i<m2;i++) { if(ma1[c[i]]==false && c[i].st!=0) { wyn.push_back("+ " + to_string(c[i].st+1)+" "+to_string(c[i].nd+1)); } } vector<bool>cz(n); q2(d,0,cz,ma2); cout<<wyn.size()<<endl; for(int i=0;i<wyn.size();i++) { cout<<wyn[i]<<endl; } } |