#include <iostream> #include <vector> #include <set> using namespace std; struct s1{ char pm; int a,b; }; void dfs(int w, vector<bool>&vi, vector<vector<int>>&g, vector<vector<bool>>&lg, vector<s1>&o){ vi[w]=true; if(w!=1 && lg[1][w]==false){ lg[1][w]=true; lg[w][1]=true; o.push_back({'+',1,w}); } for(auto&i:g[w]){ if(!vi[i]){ dfs(i,vi,g,lg,o); } } } void dfs2(int w, vector<bool>&vi, vector<vector<int>>&ta, vector<vector<bool>>&lg, vector<vector<bool>><a, vector<s1>&o){ vi[w]=true; //cout<<"i"<<w<<endl; for(auto&i:ta[w]){ if(!vi[i]){ dfs2(i,vi,ta,lg,lta,o); } } //cout<<"o"<<w<<endl; if(w!=1 && lg[1][w]==true && lta[1][w]==false){ o.push_back({'-',1,w}); lg[1][w]=false; lg[w][1]=false; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int n,m; cin >> n >> m; //cout<<n<<" "<<m<<endl; vector<vector<bool>>lg(n+1,vector<bool>(n+1)); vector<vector<int>>g(n+1); //set<int>c1; for(int i=0;i<m;i++){ int a,b; cin >> a >> b; //cout<<a<<" "<<b<<endl; lg[a][b]=true; lg[b][a]=true; //if(a==1){c1.insert(b);} //else if(b==1){c1.insert(a);} g[a].push_back(b); g[b].push_back(a); } int k; cin >> k; //cout<<k<<endl; vector<vector<bool>>lta(n+1,vector<bool>(n+1)); vector<vector<int>>ta(n+1); for(int i=0;i<k;i++){ int a,b; cin >> a >> b; //cout<<a<<" "<<b<<endl; lta[a][b]=true; lta[b][a]=true; ta[a].push_back(b); ta[b].push_back(a); } vector<s1>o; vector<bool>vi1(n+1),vi2(n+1); dfs(1,vi1,g,lg,o); for(int i=2;i<n+1;i++){ for(int j=2;j<n+1;j++){ if(lg[i][j]==true && lta[i][j]==false){ o.push_back({'-',i,j}); lg[i][j]=false; lg[j][i]=false; } if(lg[i][j]==false && lta[i][j]==true){ o.push_back({'+',i,j}); lg[i][j]=true; lg[j][i]=true; } } } //cout<<endl; dfs2(1,vi2,ta,lg,lta,o); //cout<<endl; cout<<o.size()<<endl; for(auto&i:o){cout<<i.pm<<" "<<i.a<<" "<<i.b<<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 | #include <iostream> #include <vector> #include <set> using namespace std; struct s1{ char pm; int a,b; }; void dfs(int w, vector<bool>&vi, vector<vector<int>>&g, vector<vector<bool>>&lg, vector<s1>&o){ vi[w]=true; if(w!=1 && lg[1][w]==false){ lg[1][w]=true; lg[w][1]=true; o.push_back({'+',1,w}); } for(auto&i:g[w]){ if(!vi[i]){ dfs(i,vi,g,lg,o); } } } void dfs2(int w, vector<bool>&vi, vector<vector<int>>&ta, vector<vector<bool>>&lg, vector<vector<bool>><a, vector<s1>&o){ vi[w]=true; //cout<<"i"<<w<<endl; for(auto&i:ta[w]){ if(!vi[i]){ dfs2(i,vi,ta,lg,lta,o); } } //cout<<"o"<<w<<endl; if(w!=1 && lg[1][w]==true && lta[1][w]==false){ o.push_back({'-',1,w}); lg[1][w]=false; lg[w][1]=false; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int n,m; cin >> n >> m; //cout<<n<<" "<<m<<endl; vector<vector<bool>>lg(n+1,vector<bool>(n+1)); vector<vector<int>>g(n+1); //set<int>c1; for(int i=0;i<m;i++){ int a,b; cin >> a >> b; //cout<<a<<" "<<b<<endl; lg[a][b]=true; lg[b][a]=true; //if(a==1){c1.insert(b);} //else if(b==1){c1.insert(a);} g[a].push_back(b); g[b].push_back(a); } int k; cin >> k; //cout<<k<<endl; vector<vector<bool>>lta(n+1,vector<bool>(n+1)); vector<vector<int>>ta(n+1); for(int i=0;i<k;i++){ int a,b; cin >> a >> b; //cout<<a<<" "<<b<<endl; lta[a][b]=true; lta[b][a]=true; ta[a].push_back(b); ta[b].push_back(a); } vector<s1>o; vector<bool>vi1(n+1),vi2(n+1); dfs(1,vi1,g,lg,o); for(int i=2;i<n+1;i++){ for(int j=2;j<n+1;j++){ if(lg[i][j]==true && lta[i][j]==false){ o.push_back({'-',i,j}); lg[i][j]=false; lg[j][i]=false; } if(lg[i][j]==false && lta[i][j]==true){ o.push_back({'+',i,j}); lg[i][j]=true; lg[j][i]=true; } } } //cout<<endl; dfs2(1,vi2,ta,lg,lta,o); //cout<<endl; cout<<o.size()<<endl; for(auto&i:o){cout<<i.pm<<" "<<i.a<<" "<<i.b<<endl;} } |