#include <bits/stdc++.h> using namespace std; const int MAXN = 3e4+7; const int MAXA = 2e5+7; struct str{ int fi,sc,th; }; str ans[MAXA]; bool visited[MAXN]; int dis[MAXN]; vector<int>kolejnosc; map<pair<int,int>,bool> e,newe; vector<int>g[MAXN]; vector<pair<int,int>> v; queue<int>q; void BFS(){ while(!q.empty()){ int x=q.front(); q.pop(); kolejnosc.push_back(x); for(auto s:g[x]){ if(visited[s]) continue; dis[s]=dis[x]+1; visited[s]=1; q.push(s); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m1,m2,a,b,ra=0,x; cin>>n>>m1; //cout<<n<<'\n'<<m1<<'\n'; for(int i=1;i<=m1;i++){ cin>>a>>b; //cout<<a<<' '<<b<<'\n'; e[{a,b}]=e[{b,a}]=1; v.push_back({a,b}); g[a].push_back(b); g[b].push_back(a); } visited[1]=1; dis[1]=0; q.push(1); BFS(); for(auto x:kolejnosc){ //cout<<x<<' '<<dis[x]<<' '<<e[{1,x}]<<'\n'; if(dis[x]>1){ if(e[{1,x}]==0){ ans[++ra]={1,1,x}; e[{1,x}]=e[{x,1}]=1; } } } cin>>m2; //cout<<m2<<'\n'; for(int i=1;i<=m2;i++){ cin>>a>>b; //cout<<a<<' '<<b<<'\n'; newe[{a,b}]=newe[{b,a}]=1; if(e[{a,b}]==0){ ans[++ra]={1,a,b}; } } for(int i=0;i<v.size();i++){ a=v[i].first; b=v[i].second; if(newe[{a,b}]==0){ e[{a,b}]=e[{b,a}]=0; ans[++ra]={-1,a,b}; } } for(int i=kolejnosc.size()-1;i>=0;i--){ x=kolejnosc[i]; if(e[{1,x}]==1 and newe[{1,x}]==0){ ans[++ra]={-1,1,x}; } } cout<<ra<<'\n'; for(int i=1;i<=ra;i++){ if(ans[i].fi==1) cout<<"+ "; else cout<<"- "; cout<<ans[i].sc<<' '<<ans[i].th<<'\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 | #include <bits/stdc++.h> using namespace std; const int MAXN = 3e4+7; const int MAXA = 2e5+7; struct str{ int fi,sc,th; }; str ans[MAXA]; bool visited[MAXN]; int dis[MAXN]; vector<int>kolejnosc; map<pair<int,int>,bool> e,newe; vector<int>g[MAXN]; vector<pair<int,int>> v; queue<int>q; void BFS(){ while(!q.empty()){ int x=q.front(); q.pop(); kolejnosc.push_back(x); for(auto s:g[x]){ if(visited[s]) continue; dis[s]=dis[x]+1; visited[s]=1; q.push(s); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m1,m2,a,b,ra=0,x; cin>>n>>m1; //cout<<n<<'\n'<<m1<<'\n'; for(int i=1;i<=m1;i++){ cin>>a>>b; //cout<<a<<' '<<b<<'\n'; e[{a,b}]=e[{b,a}]=1; v.push_back({a,b}); g[a].push_back(b); g[b].push_back(a); } visited[1]=1; dis[1]=0; q.push(1); BFS(); for(auto x:kolejnosc){ //cout<<x<<' '<<dis[x]<<' '<<e[{1,x}]<<'\n'; if(dis[x]>1){ if(e[{1,x}]==0){ ans[++ra]={1,1,x}; e[{1,x}]=e[{x,1}]=1; } } } cin>>m2; //cout<<m2<<'\n'; for(int i=1;i<=m2;i++){ cin>>a>>b; //cout<<a<<' '<<b<<'\n'; newe[{a,b}]=newe[{b,a}]=1; if(e[{a,b}]==0){ ans[++ra]={1,a,b}; } } for(int i=0;i<v.size();i++){ a=v[i].first; b=v[i].second; if(newe[{a,b}]==0){ e[{a,b}]=e[{b,a}]=0; ans[++ra]={-1,a,b}; } } for(int i=kolejnosc.size()-1;i>=0;i--){ x=kolejnosc[i]; if(e[{1,x}]==1 and newe[{1,x}]==0){ ans[++ra]={-1,1,x}; } } cout<<ra<<'\n'; for(int i=1;i<=ra;i++){ if(ans[i].fi==1) cout<<"+ "; else cout<<"- "; cout<<ans[i].sc<<' '<<ans[i].th<<'\n'; } return 0; } |