#include <iostream> #include <set> #include <vector> #include <map> using namespace std; const int stala=3e4+10; vector<int>first_graph[stala]; map<pair<int,int>,int>mapka_first_graph; set<pair<int,int> >all_first_graph; bool o_first_graph[stala]; vector<pair<int,int> >to_remove; vector<int>second_graph[stala]; bool o_second_graph[stala]; vector<pair<int,int> >final_graph; map<pair<int,int>,int>mapka_final_graph; vector<char>res; vector<pair<int,int> >res2; vector<int>neighbours; void dfs(int k) { o_first_graph[k]=1; if(k!=1){ if(mapka_first_graph[{1,k}]==0){ mapka_first_graph[{1,k}]=1; all_first_graph.insert({1,k}); res.push_back('+'); res2.push_back({1,k}); } } for(int i=0;i<(int)first_graph[k].size();i++){ if(o_first_graph[first_graph[k][i]]==0){ dfs(first_graph[k][i]); } } } void dfs2(int k) { o_second_graph[k]=1; for(int i=0;i<(int)second_graph[k].size();i++){ if(o_second_graph[second_graph[k][i]]==0){ dfs2(second_graph[k][i]); } } if(mapka_final_graph[{1,k}]==0){ res.push_back('-'); res2.push_back({1,k}); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ile; cin>>ile; int q; cin>>q; for(int i=1;i<=q;i++){ int a,b; cin>>a>>b; first_graph[a].push_back(b); first_graph[b].push_back(a); mapka_first_graph[{min(a,b),max(a,b)}]=1; all_first_graph.insert({min(a,b),max(a,b)}); } dfs(1); cin>>q; for(int i=1;i<=q;i++){ int a,b; cin>>a>>b; if(a==1){ neighbours.push_back(b); } else if(b==1){ neighbours.push_back(a); } final_graph.push_back({min(a,b),max(a,b)}); mapka_final_graph[{min(a,b),max(a,b)}]=1; } for(int i=0;i<q;i++){ if(mapka_first_graph[{final_graph[i].first,final_graph[i].second}]==0){ mapka_first_graph[{final_graph[i].first,final_graph[i].second}]=1; all_first_graph.insert({final_graph[i].first,final_graph[i].second}); res.push_back('+'); res2.push_back({final_graph[i].first,final_graph[i].second}); } } auto it=all_first_graph.begin(); while(it!=all_first_graph.end()){ int x=it->first; int y=it->second; if(x!=1&&y!=1&&mapka_final_graph[{x,y}]==0){ to_remove.push_back({x,y}); } it++; } for(int i=0;i<(int)to_remove.size();i++){ all_first_graph.erase({to_remove[i].first,to_remove[i].second}); mapka_first_graph[{to_remove[i].first,to_remove[i].second}]=0; res.push_back('-'); res2.push_back({to_remove[i].first,to_remove[i].second}); } while(!all_first_graph.empty()){ auto it=all_first_graph.begin(); if(it->first!=1&&it->second!=1){ second_graph[it->first].push_back(it->second); second_graph[it->second].push_back(it->first); } all_first_graph.erase(it); } for(int i=0;i<(int)neighbours.size();i++){ if(o_second_graph[neighbours[i]]==0){ dfs2(neighbours[i]); } } cout<<(int)res.size()<<"\n"; for(int i=0;i<(int)res.size();i++){ cout<<res[i]<<" "<<res2[i].first<<" "<<res2[i].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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | #include <iostream> #include <set> #include <vector> #include <map> using namespace std; const int stala=3e4+10; vector<int>first_graph[stala]; map<pair<int,int>,int>mapka_first_graph; set<pair<int,int> >all_first_graph; bool o_first_graph[stala]; vector<pair<int,int> >to_remove; vector<int>second_graph[stala]; bool o_second_graph[stala]; vector<pair<int,int> >final_graph; map<pair<int,int>,int>mapka_final_graph; vector<char>res; vector<pair<int,int> >res2; vector<int>neighbours; void dfs(int k) { o_first_graph[k]=1; if(k!=1){ if(mapka_first_graph[{1,k}]==0){ mapka_first_graph[{1,k}]=1; all_first_graph.insert({1,k}); res.push_back('+'); res2.push_back({1,k}); } } for(int i=0;i<(int)first_graph[k].size();i++){ if(o_first_graph[first_graph[k][i]]==0){ dfs(first_graph[k][i]); } } } void dfs2(int k) { o_second_graph[k]=1; for(int i=0;i<(int)second_graph[k].size();i++){ if(o_second_graph[second_graph[k][i]]==0){ dfs2(second_graph[k][i]); } } if(mapka_final_graph[{1,k}]==0){ res.push_back('-'); res2.push_back({1,k}); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ile; cin>>ile; int q; cin>>q; for(int i=1;i<=q;i++){ int a,b; cin>>a>>b; first_graph[a].push_back(b); first_graph[b].push_back(a); mapka_first_graph[{min(a,b),max(a,b)}]=1; all_first_graph.insert({min(a,b),max(a,b)}); } dfs(1); cin>>q; for(int i=1;i<=q;i++){ int a,b; cin>>a>>b; if(a==1){ neighbours.push_back(b); } else if(b==1){ neighbours.push_back(a); } final_graph.push_back({min(a,b),max(a,b)}); mapka_final_graph[{min(a,b),max(a,b)}]=1; } for(int i=0;i<q;i++){ if(mapka_first_graph[{final_graph[i].first,final_graph[i].second}]==0){ mapka_first_graph[{final_graph[i].first,final_graph[i].second}]=1; all_first_graph.insert({final_graph[i].first,final_graph[i].second}); res.push_back('+'); res2.push_back({final_graph[i].first,final_graph[i].second}); } } auto it=all_first_graph.begin(); while(it!=all_first_graph.end()){ int x=it->first; int y=it->second; if(x!=1&&y!=1&&mapka_final_graph[{x,y}]==0){ to_remove.push_back({x,y}); } it++; } for(int i=0;i<(int)to_remove.size();i++){ all_first_graph.erase({to_remove[i].first,to_remove[i].second}); mapka_first_graph[{to_remove[i].first,to_remove[i].second}]=0; res.push_back('-'); res2.push_back({to_remove[i].first,to_remove[i].second}); } while(!all_first_graph.empty()){ auto it=all_first_graph.begin(); if(it->first!=1&&it->second!=1){ second_graph[it->first].push_back(it->second); second_graph[it->second].push_back(it->first); } all_first_graph.erase(it); } for(int i=0;i<(int)neighbours.size();i++){ if(o_second_graph[neighbours[i]]==0){ dfs2(neighbours[i]); } } cout<<(int)res.size()<<"\n"; for(int i=0;i<(int)res.size();i++){ cout<<res[i]<<" "<<res2[i].first<<" "<<res2[i].second<<"\n"; } return 0; } |