#include <bits/stdc++.h> using namespace std; int main() { int n,m_s; cin>>n>>m_s; vector<vector<int>> G1(n); vector<vector<int>> G2(n); for(int i=0;i<m_s;i++) { int x,y; cin>>x>>y; x--;y--; G1[x].push_back(y); G1[y].push_back(x); } int m_d; cin>>m_d; for(int i=0;i<m_d;i++) { int x,y; cin>>x>>y; x--;y--; G2[x].push_back(y); G2[y].push_back(x); } vector<pair<char,pair<int,int>>> ans; vector<bool> b1(n,0); for(int i=0;i<G1[0].size();i++) b1[G1[0][i]]=1; queue<int> q; q.push(0); vector<bool> visited(n,0); visited[0]=1; while(q.empty()==false) { int a=q.front(); q.pop(); for(int i=0;i<G1[a].size();i++) { if(visited[G1[a][i]]==0) { if(b1[G1[a][i]]==0) ans.push_back({'+',{1,G1[a][i]+1}}); visited[G1[a][i]]=1; q.push(G1[a][i]); } } } for(int i=1;i<n;i++) for(int j=0;j<G1[i].size();j++) { if(i<G1[i][j]) ans.push_back({'-',{i+1,G1[i][j]+1}}); } for(int i=1;i<n;i++) for(int j=0;j<G2[i].size();j++) { if(i<G2[i][j]) ans.push_back({'+',{i+1,G2[i][j]+1}}); } vector<int> d(n,-1); for(int i=0;i<G2[0].size();i++) { q.push(G2[0][i]); d[G2[0][i]]=0; } vector<int> del; while(q.empty()==false) { int a=q.front(); q.pop(); for(int i=0;i<G2[a].size();i++) { if(G2[a][i]==0) continue; if(d[G2[a][i]]==-1) { d[G2[a][i]]=d[a]+1; q.push(G2[a][i]); del.push_back(G2[a][i]); } } } for(int i=del.size()-1;i>=0;i--) ans.push_back({'-',{1,del[i]+1}}); cout<<ans.size()<<endl; for(int i=0;i<ans.size();i++) cout<<ans[i].first<<" "<<ans[i].second.first<<" "<<ans[i].second.second<<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 | #include <bits/stdc++.h> using namespace std; int main() { int n,m_s; cin>>n>>m_s; vector<vector<int>> G1(n); vector<vector<int>> G2(n); for(int i=0;i<m_s;i++) { int x,y; cin>>x>>y; x--;y--; G1[x].push_back(y); G1[y].push_back(x); } int m_d; cin>>m_d; for(int i=0;i<m_d;i++) { int x,y; cin>>x>>y; x--;y--; G2[x].push_back(y); G2[y].push_back(x); } vector<pair<char,pair<int,int>>> ans; vector<bool> b1(n,0); for(int i=0;i<G1[0].size();i++) b1[G1[0][i]]=1; queue<int> q; q.push(0); vector<bool> visited(n,0); visited[0]=1; while(q.empty()==false) { int a=q.front(); q.pop(); for(int i=0;i<G1[a].size();i++) { if(visited[G1[a][i]]==0) { if(b1[G1[a][i]]==0) ans.push_back({'+',{1,G1[a][i]+1}}); visited[G1[a][i]]=1; q.push(G1[a][i]); } } } for(int i=1;i<n;i++) for(int j=0;j<G1[i].size();j++) { if(i<G1[i][j]) ans.push_back({'-',{i+1,G1[i][j]+1}}); } for(int i=1;i<n;i++) for(int j=0;j<G2[i].size();j++) { if(i<G2[i][j]) ans.push_back({'+',{i+1,G2[i][j]+1}}); } vector<int> d(n,-1); for(int i=0;i<G2[0].size();i++) { q.push(G2[0][i]); d[G2[0][i]]=0; } vector<int> del; while(q.empty()==false) { int a=q.front(); q.pop(); for(int i=0;i<G2[a].size();i++) { if(G2[a][i]==0) continue; if(d[G2[a][i]]==-1) { d[G2[a][i]]=d[a]+1; q.push(G2[a][i]); del.push_back(G2[a][i]); } } } for(int i=del.size()-1;i>=0;i--) ans.push_back({'-',{1,del[i]+1}}); cout<<ans.size()<<endl; for(int i=0;i<ans.size();i++) cout<<ans[i].first<<" "<<ans[i].second.first<<" "<<ans[i].second.second<<endl; } |