#include <bits/stdc++.h> using namespace std; set <int> graf[30007]; vector <pair <int, int> > wypisz; vector <pair <int, int> > dodaj; vector <int> do_usuniecia; map <pair <int, int>, bool> k_ist; map <pair <int, int>, bool> docelowe; int wsk = 1; int odw[30007]; void utworz_centrum (int x){ odw[x] = wsk; if ((x != 1) && (!k_ist[{1, x}])){ wypisz.push_back(make_pair(1, x)); graf[1].insert(x); graf[x].insert(1); k_ist[{1, x}] = true; k_ist[{x, 1}] = true; } for (auto iter = graf[x].begin(); iter != graf[x].end(); ++iter){ int v = *iter; if (odw[v] == wsk){ continue; } utworz_centrum(v); } } void DFS (int x){ odw[x] = wsk; // cout << "WCHODZE: " << x << "\n"; for (auto iter = graf[x].begin(); iter != graf[x].end(); ++iter){ int v = *iter; if (odw[v] != wsk){ DFS(v); } } if (docelowe[{1, x}] == false && k_ist[{1, x}]){ k_ist[{1, x}] = false; k_ist[{x, 1}] = false; wypisz.push_back(make_pair(-1, -x)); graf[x].erase(1); graf[1].erase(x); } // cout << "WYCHODZE: " << x << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m_akt, m_cel, a, b; cin >> n >> m_akt; for (int i = 0; i < m_akt; ++i){ cin >> a >> b; graf[a].insert(b); graf[b].insert(a); k_ist[{a, b}] = true; k_ist[{b, a}] = true; } cin >> m_cel; for (int i = 0; i < m_cel; ++i){ cin >> a >> b; docelowe[{a, b}] = true; docelowe[{b, a}] = true; if (k_ist[{a, b}] == false){ dodaj.push_back(make_pair(a, b)); } } wsk++; utworz_centrum(1); wsk++; for (int i = 0; i < (int)dodaj.size(); ++i){ if (k_ist[{dodaj[i].first, dodaj[i].second}] == true){ continue; } wypisz.push_back(make_pair(dodaj[i].first, dodaj[i].second)); graf[dodaj[i].first].insert(dodaj[i].second); graf[dodaj[i].second].insert(dodaj[i].first); k_ist[{dodaj[i].first, dodaj[i].second}] = true; k_ist[{dodaj[i].second, dodaj[i].first}] = true; } for (int i = 2; i <= n; ++i){ do_usuniecia.clear(); for (auto iter = graf[i].begin(); iter != graf[i].end(); ++iter){ int v = *iter; if (v != 1){ if (docelowe[{i, v}] == false){ do_usuniecia.push_back(v); } } } for (int j = 0; j < (int)do_usuniecia.size(); ++j){ graf[i].erase(do_usuniecia[j]); graf[do_usuniecia[j]].erase(i); // cout << "USUWAM: " << i << " " << do_usuniecia[j] << "\n"; // cout << "USUWAM: " << do_usuniecia[j] << " " << i << "\n"; wypisz.push_back(make_pair(-i, -do_usuniecia[j])); } } odw[1] = wsk; for (int i = 2; i <= n; ++i){ if (docelowe[{1, i}] == true && odw[i] != wsk){ DFS(i); } } // cout << "\n"; cout << (int)wypisz.size() << "\n"; for (int i = 0; i < (int)wypisz.size(); ++i){ if (wypisz[i].first > 0){ cout << '+' << " " << wypisz[i].first << " " << wypisz[i].second << "\n"; } else{ cout << '-' << " " << -wypisz[i].first << " " << -wypisz[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 | #include <bits/stdc++.h> using namespace std; set <int> graf[30007]; vector <pair <int, int> > wypisz; vector <pair <int, int> > dodaj; vector <int> do_usuniecia; map <pair <int, int>, bool> k_ist; map <pair <int, int>, bool> docelowe; int wsk = 1; int odw[30007]; void utworz_centrum (int x){ odw[x] = wsk; if ((x != 1) && (!k_ist[{1, x}])){ wypisz.push_back(make_pair(1, x)); graf[1].insert(x); graf[x].insert(1); k_ist[{1, x}] = true; k_ist[{x, 1}] = true; } for (auto iter = graf[x].begin(); iter != graf[x].end(); ++iter){ int v = *iter; if (odw[v] == wsk){ continue; } utworz_centrum(v); } } void DFS (int x){ odw[x] = wsk; // cout << "WCHODZE: " << x << "\n"; for (auto iter = graf[x].begin(); iter != graf[x].end(); ++iter){ int v = *iter; if (odw[v] != wsk){ DFS(v); } } if (docelowe[{1, x}] == false && k_ist[{1, x}]){ k_ist[{1, x}] = false; k_ist[{x, 1}] = false; wypisz.push_back(make_pair(-1, -x)); graf[x].erase(1); graf[1].erase(x); } // cout << "WYCHODZE: " << x << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m_akt, m_cel, a, b; cin >> n >> m_akt; for (int i = 0; i < m_akt; ++i){ cin >> a >> b; graf[a].insert(b); graf[b].insert(a); k_ist[{a, b}] = true; k_ist[{b, a}] = true; } cin >> m_cel; for (int i = 0; i < m_cel; ++i){ cin >> a >> b; docelowe[{a, b}] = true; docelowe[{b, a}] = true; if (k_ist[{a, b}] == false){ dodaj.push_back(make_pair(a, b)); } } wsk++; utworz_centrum(1); wsk++; for (int i = 0; i < (int)dodaj.size(); ++i){ if (k_ist[{dodaj[i].first, dodaj[i].second}] == true){ continue; } wypisz.push_back(make_pair(dodaj[i].first, dodaj[i].second)); graf[dodaj[i].first].insert(dodaj[i].second); graf[dodaj[i].second].insert(dodaj[i].first); k_ist[{dodaj[i].first, dodaj[i].second}] = true; k_ist[{dodaj[i].second, dodaj[i].first}] = true; } for (int i = 2; i <= n; ++i){ do_usuniecia.clear(); for (auto iter = graf[i].begin(); iter != graf[i].end(); ++iter){ int v = *iter; if (v != 1){ if (docelowe[{i, v}] == false){ do_usuniecia.push_back(v); } } } for (int j = 0; j < (int)do_usuniecia.size(); ++j){ graf[i].erase(do_usuniecia[j]); graf[do_usuniecia[j]].erase(i); // cout << "USUWAM: " << i << " " << do_usuniecia[j] << "\n"; // cout << "USUWAM: " << do_usuniecia[j] << " " << i << "\n"; wypisz.push_back(make_pair(-i, -do_usuniecia[j])); } } odw[1] = wsk; for (int i = 2; i <= n; ++i){ if (docelowe[{1, i}] == true && odw[i] != wsk){ DFS(i); } } // cout << "\n"; cout << (int)wypisz.size() << "\n"; for (int i = 0; i < (int)wypisz.size(); ++i){ if (wypisz[i].first > 0){ cout << '+' << " " << wypisz[i].first << " " << wypisz[i].second << "\n"; } else{ cout << '-' << " " << -wypisz[i].first << " " << -wypisz[i].second << "\n"; } } } |