#include <bits/stdc++.h> using namespace std; set<int> srcV[399990]; set<int> dstV[399990]; bool visited1[399990]; bool visited2[399990]; vector<pair<char,pair<int,int> > > result; void dfsAddAllEdges(const int &start, const int &v){ if(srcV[v].count(start) == 0){ // cout<<"addDFS: "<<start<<' '<<v<<'\n'; result.push_back({'+',{start, v}}); } visited1[v] = true; for(auto i = srcV[v].begin(); i != srcV[v].end(); ++i){ if(!visited1[*i]){ dfsAddAllEdges(start, *i); } } } void dfsDeleteUselesEdges(const int &start, const int &v){ visited2[v] = true; for(auto i = dstV[v].begin(); i != dstV[v].end(); ++i){ if(!visited2[*i]){ dfsDeleteUselesEdges(start, *i); } } if(dstV[v].count(start) == 0){ // cout<<"deleteDFS: "<<start<<' '<<v<<'\n'; result.push_back({'-',{start, v}}); } } void deleteUselesEdges(const int &n){ for(int i = 2; i <= n; ++i){ for(auto j = srcV[i].begin(); j != srcV[i].end(); ++j){ if(*j > i){ if(dstV[i].count(*j) == 0){ // cout<<"delete: "<<i<<' '<<*j<<'\n'; result.push_back({'-',{i, *j}}); } } } } } void addNecessaryEdges(const int &n){ for(int i = 2; i <= n; ++i){ for(auto j = dstV[i].begin(); j != dstV[i].end(); ++j){ if(*j > i){ if(srcV[i].count(*j) == 0){ // cout<<"add: "<<i<<' '<<*j<<'\n'; result.push_back({'+',{i, *j}}); } } } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k, a, b, start; cin >> n; cin >> k; for(int i = 0; i < k; ++i){ cin >> a >> b; srcV[a].insert(b); srcV[b].insert(a); } cin >> k; for(int i = 0; i < k; ++i){ cin >> a >> b; dstV[a].insert(b); dstV[b].insert(a); } for(int i = 1; i <= n; ++i){ visited1[i] = false; visited2[i] = false; } start = 1; visited1[start] = true; visited2[start] = true; for(auto i = srcV[start].begin(); i != srcV[start].end(); ++i){ if(!visited1[*i]){ // cout<<"ASD"<<*i<<'\n'; dfsAddAllEdges(start, *i); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// deleteUselesEdges(n); addNecessaryEdges(n); for(auto i = dstV[start].begin(); i != dstV[start].end(); ++i){ if(!visited2[*i]){ dfsDeleteUselesEdges(start, *i); } } cout<<result.size()<<'\n'; for(int i = 0; i < result.size(); ++i){ cout << result[i].first << ' ' << result[i].second.first << ' ' << result[i].second.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 126 | #include <bits/stdc++.h> using namespace std; set<int> srcV[399990]; set<int> dstV[399990]; bool visited1[399990]; bool visited2[399990]; vector<pair<char,pair<int,int> > > result; void dfsAddAllEdges(const int &start, const int &v){ if(srcV[v].count(start) == 0){ // cout<<"addDFS: "<<start<<' '<<v<<'\n'; result.push_back({'+',{start, v}}); } visited1[v] = true; for(auto i = srcV[v].begin(); i != srcV[v].end(); ++i){ if(!visited1[*i]){ dfsAddAllEdges(start, *i); } } } void dfsDeleteUselesEdges(const int &start, const int &v){ visited2[v] = true; for(auto i = dstV[v].begin(); i != dstV[v].end(); ++i){ if(!visited2[*i]){ dfsDeleteUselesEdges(start, *i); } } if(dstV[v].count(start) == 0){ // cout<<"deleteDFS: "<<start<<' '<<v<<'\n'; result.push_back({'-',{start, v}}); } } void deleteUselesEdges(const int &n){ for(int i = 2; i <= n; ++i){ for(auto j = srcV[i].begin(); j != srcV[i].end(); ++j){ if(*j > i){ if(dstV[i].count(*j) == 0){ // cout<<"delete: "<<i<<' '<<*j<<'\n'; result.push_back({'-',{i, *j}}); } } } } } void addNecessaryEdges(const int &n){ for(int i = 2; i <= n; ++i){ for(auto j = dstV[i].begin(); j != dstV[i].end(); ++j){ if(*j > i){ if(srcV[i].count(*j) == 0){ // cout<<"add: "<<i<<' '<<*j<<'\n'; result.push_back({'+',{i, *j}}); } } } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k, a, b, start; cin >> n; cin >> k; for(int i = 0; i < k; ++i){ cin >> a >> b; srcV[a].insert(b); srcV[b].insert(a); } cin >> k; for(int i = 0; i < k; ++i){ cin >> a >> b; dstV[a].insert(b); dstV[b].insert(a); } for(int i = 1; i <= n; ++i){ visited1[i] = false; visited2[i] = false; } start = 1; visited1[start] = true; visited2[start] = true; for(auto i = srcV[start].begin(); i != srcV[start].end(); ++i){ if(!visited1[*i]){ // cout<<"ASD"<<*i<<'\n'; dfsAddAllEdges(start, *i); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// deleteUselesEdges(n); addNecessaryEdges(n); for(auto i = dstV[start].begin(); i != dstV[start].end(); ++i){ if(!visited2[*i]){ dfsDeleteUselesEdges(start, *i); } } cout<<result.size()<<'\n'; for(int i = 0; i < result.size(); ++i){ cout << result[i].first << ' ' << result[i].second.first << ' ' << result[i].second.second << '\n'; } return 0; } |