#include<bits/stdc++.h> using namespace std; const int maksn = 3e4+5; int n; int ms,md; map<pair<int,int>,bool> edges1,edges2,distinct1,distinct2; //dist1 = są w 1 a nie w 2 //dist2 = są w 2 a nie w 1 //edges z duplikatami dist bez duplikatow vector<pair<int,int> > wek1,wek2; vector<int> graf1[maksn],graf2[maksn]; void wczytaj() { cin >> n; cin >> ms; for(int i = 0; i < ms; i++) { int a,b; cin >> a >> b; edges1[{a,b}] = 1; edges1[{b,a}] = 1; graf1[a].push_back(b); graf1[b].push_back(a); } cin >> md; for(int i = 0; i < md; i++) { int a,b; cin >> a >> b; edges2[{a,b}] = 1; edges2[{b,a}] = 1; graf2[a].push_back(b); graf2[b].push_back(a); } } void licz_dist() { for(auto i : edges1) { if(edges2.find(i.first) == edges2.end()) //i.first nie ma w edges 2 { if(distinct1.find({i.first.second,i.first.first}) == distinct1.end()) { distinct1[i.first] = 1; //cout << i.first.first << " " << i.first.second << '\n'; } } } //cout << '\n'; for(auto i : edges2) { if(edges1.find(i.first) == edges1.end()) //i.first nie ma w edges1 { if(distinct2.find({i.first.second,i.first.first}) == distinct2.end()) { distinct2[i.first] = 1; //cout << i.first.first << " " << i.first.second << '\n'; } } } } bool odw1[maksn]; void dfs1(int v)//dopelniam graf1 do wek1 { if(v != 1) { if(edges1.find({1,v}) == edges1.end()) { wek1.push_back({1,v}); edges1[{1,v}] = 1; edges1[{v,1}] = 1; } } odw1[v] = 1; for(int i : graf1[v]) if(!odw1[i]) { dfs1(i); } } bool odw2[maksn]; void dfs2(int v) { if(v != 1) { if(edges2.find({1,v}) == edges2.end()) { wek2.push_back({1,v}); edges2[{1,v}] = edges2[{v,1}] = 1; } } odw2[v] = 1; for(int i : graf2[v]) if(!odw2[i]) { dfs2(i); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); wczytaj(); licz_dist(); dfs1(1); dfs2(1); for(auto i : distinct2) //te ktore sa w 2 dokladamy do dopelnionego grafu1 { if(edges1.find(i.first) == edges1.end()) //nie ma w dopelnionym grafie 1 { wek1.push_back(i.first); } } for(auto i : distinct1) //te ktore sa w 2 dokladamy do dopelnionego grafu1 { if(edges2.find(i.first) == edges2.end()) //nie ma w dopelnionym grafie 1 { wek2.push_back(i.first); } } cout << (int)(wek1.size() + wek2.size()) << '\n'; for(auto i : wek1) { cout << "+ " << i.first << " " << i.second << '\n'; } for(auto i : wek2) { cout << "- " << i.first << " " << i.second << '\n'; } }
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 127 128 129 130 131 132 133 134 135 136 | #include<bits/stdc++.h> using namespace std; const int maksn = 3e4+5; int n; int ms,md; map<pair<int,int>,bool> edges1,edges2,distinct1,distinct2; //dist1 = są w 1 a nie w 2 //dist2 = są w 2 a nie w 1 //edges z duplikatami dist bez duplikatow vector<pair<int,int> > wek1,wek2; vector<int> graf1[maksn],graf2[maksn]; void wczytaj() { cin >> n; cin >> ms; for(int i = 0; i < ms; i++) { int a,b; cin >> a >> b; edges1[{a,b}] = 1; edges1[{b,a}] = 1; graf1[a].push_back(b); graf1[b].push_back(a); } cin >> md; for(int i = 0; i < md; i++) { int a,b; cin >> a >> b; edges2[{a,b}] = 1; edges2[{b,a}] = 1; graf2[a].push_back(b); graf2[b].push_back(a); } } void licz_dist() { for(auto i : edges1) { if(edges2.find(i.first) == edges2.end()) //i.first nie ma w edges 2 { if(distinct1.find({i.first.second,i.first.first}) == distinct1.end()) { distinct1[i.first] = 1; //cout << i.first.first << " " << i.first.second << '\n'; } } } //cout << '\n'; for(auto i : edges2) { if(edges1.find(i.first) == edges1.end()) //i.first nie ma w edges1 { if(distinct2.find({i.first.second,i.first.first}) == distinct2.end()) { distinct2[i.first] = 1; //cout << i.first.first << " " << i.first.second << '\n'; } } } } bool odw1[maksn]; void dfs1(int v)//dopelniam graf1 do wek1 { if(v != 1) { if(edges1.find({1,v}) == edges1.end()) { wek1.push_back({1,v}); edges1[{1,v}] = 1; edges1[{v,1}] = 1; } } odw1[v] = 1; for(int i : graf1[v]) if(!odw1[i]) { dfs1(i); } } bool odw2[maksn]; void dfs2(int v) { if(v != 1) { if(edges2.find({1,v}) == edges2.end()) { wek2.push_back({1,v}); edges2[{1,v}] = edges2[{v,1}] = 1; } } odw2[v] = 1; for(int i : graf2[v]) if(!odw2[i]) { dfs2(i); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); wczytaj(); licz_dist(); dfs1(1); dfs2(1); for(auto i : distinct2) //te ktore sa w 2 dokladamy do dopelnionego grafu1 { if(edges1.find(i.first) == edges1.end()) //nie ma w dopelnionym grafie 1 { wek1.push_back(i.first); } } for(auto i : distinct1) //te ktore sa w 2 dokladamy do dopelnionego grafu1 { if(edges2.find(i.first) == edges2.end()) //nie ma w dopelnionym grafie 1 { wek2.push_back(i.first); } } cout << (int)(wek1.size() + wek2.size()) << '\n'; for(auto i : wek1) { cout << "+ " << i.first << " " << i.second << '\n'; } for(auto i : wek2) { cout << "- " << i.first << " " << i.second << '\n'; } } |