#include <bits/stdc++.h> using namespace std; int n; vector <int> g[30001],g2[30001], dousun; vector<pair<int,int> > dodane; int odw[30001]; vector<char> znaki; set<pair <int,int> > krawedzie; void wypisz(); void bfs(int v){//dodawanie krawedzi do 1 odw[v] = 1; queue<int> kol; for(unsigned int i = 0; i < g[v].size();i++){ int w = g[v][i]; odw[w] = 1; kol.push(w); } while(!kol.empty()){ int z = kol.front(); kol.pop(); for(unsigned int i = 0; i < g[z].size();i++){ int w = g[z][i]; if(odw[w]==0) { odw[w] = 1; kol.push(w); dodane.push_back(make_pair(min(v,w),max(v,w))); //cout << "dodaje "<<v <<" "<<w <<endl; znaki.push_back('+'); g[v].push_back(w); g[w].push_back(v); } } } } void usun(int a, int b){ int in,sb, w, v, ina; for(unsigned int i = 0; i < g2[b].size();i++){ w = g2[b][i]; if(w == a) { in = i;break;//polozenie 1 n liscie b } } for(int j = 0; j < g2[1].size();j++) if (g2[1][j] == b) { ina = j;break;//polozenie b n liscie 1 } for(unsigned int i = 0; i < g2[b].size();i++){ w = g2[b][i];//sasiad b if(w != a) {//jesli nie jest to a for(int j = 0; j < g2[1].size();j++) if (g2[1][j] == w){//i w jest jest na liscie sasiadow 1 dodane.push_back(make_pair(a,b)); znaki.push_back('-'); //cout <<"usuwam "<<a <<" "<<b << endl; swap(g2[1][ina],g2[1][g2[1].size()-1] ); g2[1].pop_back(); swap(g2[b][in],g2[b][g2[b].size()-1]); g2[b].pop_back(); return; } sb = w; } } dodane.push_back(make_pair(a,sb)); znaki.push_back('+'); //cout << "dodaje "<<a <<" "<<sb <<endl; g2[sb].push_back(a); g2[a].push_back(sb); usun(a,b); usun(a,sb); } int main() { ios_base::sync_with_stdio(0); cin.tie();cout.tie(); int i, j, m1, m2, x, y; cin >> n; cin >> m1; for(i = 0; i < m1; i++){ cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } bfs(1); cin >> m2; for(i = 0; i < m2; i++){ cin >> x >> y; g2[x].push_back(y); g2[y].push_back(x); } for(i = 1; i <= n; i++) { sort(g[i].begin(),g[i].end()); sort(g2[i].begin(),g2[i].end()); } //dodawanie krawedzi for(i = 2; i <= n; i++) { for(j = 0; j < g2[i].size();j++){ int w = g2[i][j]; if (!binary_search(g[i].begin(),g[i].end(),w) ) { krawedzie.insert(make_pair(min(i,w),max(i,w))); } } } for(set<pair <int,int> >::iterator it=krawedzie.begin();it!=krawedzie.end();it++){ dodane.push_back(*it); znaki.push_back('+'); } krawedzie.clear(); //usuwanie krawedzi bez 1 for(i = 2; i <= n; i++) { for(j = 0; j < g[i].size();j++){ int w = g[i][j]; if (w !=1 && !binary_search(g2[i].begin(),g2[i].end(),w) ) { krawedzie.insert(make_pair(min(i,w),max(i,w))); } } } for(set<pair <int,int> >::iterator it=krawedzie.begin();it!=krawedzie.end();it++){ dodane.push_back(*it); znaki.push_back('-'); } //usuwanie 1 for(j = 0; j < g[1].size();j++){ int w = g[1][j]; if (!binary_search(g2[1].begin(),g2[1].end(),w) ) { dousun.push_back(w); } } for(i = 0; i < dousun.size();i++){ g2[1].push_back(dousun[i]); g2[dousun[i]].push_back(1); //cout <<"musisz dodac 1 " <<dousun[i]<<endl; } //wypisz(); for(i = 0; i < dousun.size();i++) { usun(1,dousun[i]); //wypisz(); } //wypisanie wyniku cout << dodane.size() << endl; for(i = 0; i < dodane.size();i++) cout << znaki[i] <<" "<<dodane[i].first <<" "<<dodane[i].second << endl; return 0; } void wypisz(){ for(int i = 1; i <= n;i++){ for(int j = 0; j < g2[i].size();j++) cout << g2[i][j]<<" "; cout << 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 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <bits/stdc++.h> using namespace std; int n; vector <int> g[30001],g2[30001], dousun; vector<pair<int,int> > dodane; int odw[30001]; vector<char> znaki; set<pair <int,int> > krawedzie; void wypisz(); void bfs(int v){//dodawanie krawedzi do 1 odw[v] = 1; queue<int> kol; for(unsigned int i = 0; i < g[v].size();i++){ int w = g[v][i]; odw[w] = 1; kol.push(w); } while(!kol.empty()){ int z = kol.front(); kol.pop(); for(unsigned int i = 0; i < g[z].size();i++){ int w = g[z][i]; if(odw[w]==0) { odw[w] = 1; kol.push(w); dodane.push_back(make_pair(min(v,w),max(v,w))); //cout << "dodaje "<<v <<" "<<w <<endl; znaki.push_back('+'); g[v].push_back(w); g[w].push_back(v); } } } } void usun(int a, int b){ int in,sb, w, v, ina; for(unsigned int i = 0; i < g2[b].size();i++){ w = g2[b][i]; if(w == a) { in = i;break;//polozenie 1 n liscie b } } for(int j = 0; j < g2[1].size();j++) if (g2[1][j] == b) { ina = j;break;//polozenie b n liscie 1 } for(unsigned int i = 0; i < g2[b].size();i++){ w = g2[b][i];//sasiad b if(w != a) {//jesli nie jest to a for(int j = 0; j < g2[1].size();j++) if (g2[1][j] == w){//i w jest jest na liscie sasiadow 1 dodane.push_back(make_pair(a,b)); znaki.push_back('-'); //cout <<"usuwam "<<a <<" "<<b << endl; swap(g2[1][ina],g2[1][g2[1].size()-1] ); g2[1].pop_back(); swap(g2[b][in],g2[b][g2[b].size()-1]); g2[b].pop_back(); return; } sb = w; } } dodane.push_back(make_pair(a,sb)); znaki.push_back('+'); //cout << "dodaje "<<a <<" "<<sb <<endl; g2[sb].push_back(a); g2[a].push_back(sb); usun(a,b); usun(a,sb); } int main() { ios_base::sync_with_stdio(0); cin.tie();cout.tie(); int i, j, m1, m2, x, y; cin >> n; cin >> m1; for(i = 0; i < m1; i++){ cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } bfs(1); cin >> m2; for(i = 0; i < m2; i++){ cin >> x >> y; g2[x].push_back(y); g2[y].push_back(x); } for(i = 1; i <= n; i++) { sort(g[i].begin(),g[i].end()); sort(g2[i].begin(),g2[i].end()); } //dodawanie krawedzi for(i = 2; i <= n; i++) { for(j = 0; j < g2[i].size();j++){ int w = g2[i][j]; if (!binary_search(g[i].begin(),g[i].end(),w) ) { krawedzie.insert(make_pair(min(i,w),max(i,w))); } } } for(set<pair <int,int> >::iterator it=krawedzie.begin();it!=krawedzie.end();it++){ dodane.push_back(*it); znaki.push_back('+'); } krawedzie.clear(); //usuwanie krawedzi bez 1 for(i = 2; i <= n; i++) { for(j = 0; j < g[i].size();j++){ int w = g[i][j]; if (w !=1 && !binary_search(g2[i].begin(),g2[i].end(),w) ) { krawedzie.insert(make_pair(min(i,w),max(i,w))); } } } for(set<pair <int,int> >::iterator it=krawedzie.begin();it!=krawedzie.end();it++){ dodane.push_back(*it); znaki.push_back('-'); } //usuwanie 1 for(j = 0; j < g[1].size();j++){ int w = g[1][j]; if (!binary_search(g2[1].begin(),g2[1].end(),w) ) { dousun.push_back(w); } } for(i = 0; i < dousun.size();i++){ g2[1].push_back(dousun[i]); g2[dousun[i]].push_back(1); //cout <<"musisz dodac 1 " <<dousun[i]<<endl; } //wypisz(); for(i = 0; i < dousun.size();i++) { usun(1,dousun[i]); //wypisz(); } //wypisanie wyniku cout << dodane.size() << endl; for(i = 0; i < dodane.size();i++) cout << znaki[i] <<" "<<dodane[i].first <<" "<<dodane[i].second << endl; return 0; } void wypisz(){ for(int i = 1; i <= n;i++){ for(int j = 0; j < g2[i].size();j++) cout << g2[i][j]<<" "; cout << endl; } } |