#include <bits/stdc++.h> using namespace std; map <pair<int , int> , bool> czy; map <pair<int , int> , bool> ost; vector<int> pol[30005]; int n , m , a , b , wyn , wyn2; vector <pair<int , int>> w; vector <pair<int , int>> w2; bool odw[30005]; void dfs(int k) { odw[k] = 1; if(czy[{1 , k}] == 0) { pol[1].push_back(k); pol[k].push_back(1); czy[{1 , k}] = 1; czy[{k , 1}] = 1; w.push_back({1 , k}); wyn ++; } for(int i = 0 ; i < pol[k].size() ; i ++) if(odw[pol[k][i]] == 0) dfs(pol[k][i]); } void dfs2(int k) { odw[k] = 1; for(int i = 0 ; i < pol[k].size() ; i ++) if(odw[pol[k][i]] == 0) dfs2(pol[k][i]); if(ost[{1 , k}] == 0 && czy[{1 , k}] == 1) { czy[{1 , k}] = 0; czy[{k , 1}] = 0; w2.push_back({1 , k}); wyn2 ++; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; cin >> m; for(int i = 1 ; i <= m ; i ++) { cin >> a >> b; pol[a].push_back(b); pol[b].push_back(a); czy[{a , b}] = 1; czy[{b , a}] = 1; } odw[1] = 1; for(int i = 0 ; i < pol[1].size() ; i ++) { if(odw[pol[1][i]] == 0) dfs(pol[1][i]); } cin >> m; for(int i = 1 ; i <= m ; i ++) { cin >> a >> b; ost[{a , b}] = 1; ost[{b , a}] = 1; if(czy[{a , b}] == 0) { pol[a].push_back(b); pol[b].push_back(a); czy[{a , b}] = 1; czy[{b , a}] = 1; w.push_back({a , b}); //cout << "+" << " " << a << " " << b << "\n"; wyn ++; } } for(int i = 2 ; i <= n ; i ++) { for(int j = 0 ; j < pol[i].size() ; j ++) { if(pol[i][j] != 1 && ost[{i , pol[i][j]}] == 0 && czy[{i , pol[i][j]}] == 1) { czy[{i , pol[i][j]}] = 0; czy[{pol[i][j] , i}] = 0; w2.push_back({i , pol[i][j]}); wyn2 ++; } } } for(int i = 1 ; i <= n ; i ++) odw[i] = 0; dfs2(1); cout << wyn + wyn2 << "\n"; for(int i = 0 ; i < wyn ; i ++) { cout << "+" << " " << w[i].first << " " << w[i].second << "\n"; } for(int i = 0 ; i < wyn2 ; i ++) { cout << "-" << " " << w2[i].first << " " << w2[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 | #include <bits/stdc++.h> using namespace std; map <pair<int , int> , bool> czy; map <pair<int , int> , bool> ost; vector<int> pol[30005]; int n , m , a , b , wyn , wyn2; vector <pair<int , int>> w; vector <pair<int , int>> w2; bool odw[30005]; void dfs(int k) { odw[k] = 1; if(czy[{1 , k}] == 0) { pol[1].push_back(k); pol[k].push_back(1); czy[{1 , k}] = 1; czy[{k , 1}] = 1; w.push_back({1 , k}); wyn ++; } for(int i = 0 ; i < pol[k].size() ; i ++) if(odw[pol[k][i]] == 0) dfs(pol[k][i]); } void dfs2(int k) { odw[k] = 1; for(int i = 0 ; i < pol[k].size() ; i ++) if(odw[pol[k][i]] == 0) dfs2(pol[k][i]); if(ost[{1 , k}] == 0 && czy[{1 , k}] == 1) { czy[{1 , k}] = 0; czy[{k , 1}] = 0; w2.push_back({1 , k}); wyn2 ++; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; cin >> m; for(int i = 1 ; i <= m ; i ++) { cin >> a >> b; pol[a].push_back(b); pol[b].push_back(a); czy[{a , b}] = 1; czy[{b , a}] = 1; } odw[1] = 1; for(int i = 0 ; i < pol[1].size() ; i ++) { if(odw[pol[1][i]] == 0) dfs(pol[1][i]); } cin >> m; for(int i = 1 ; i <= m ; i ++) { cin >> a >> b; ost[{a , b}] = 1; ost[{b , a}] = 1; if(czy[{a , b}] == 0) { pol[a].push_back(b); pol[b].push_back(a); czy[{a , b}] = 1; czy[{b , a}] = 1; w.push_back({a , b}); //cout << "+" << " " << a << " " << b << "\n"; wyn ++; } } for(int i = 2 ; i <= n ; i ++) { for(int j = 0 ; j < pol[i].size() ; j ++) { if(pol[i][j] != 1 && ost[{i , pol[i][j]}] == 0 && czy[{i , pol[i][j]}] == 1) { czy[{i , pol[i][j]}] = 0; czy[{pol[i][j] , i}] = 0; w2.push_back({i , pol[i][j]}); wyn2 ++; } } } for(int i = 1 ; i <= n ; i ++) odw[i] = 0; dfs2(1); cout << wyn + wyn2 << "\n"; for(int i = 0 ; i < wyn ; i ++) { cout << "+" << " " << w[i].first << " " << w[i].second << "\n"; } for(int i = 0 ; i < wyn2 ; i ++) { cout << "-" << " " << w2[i].first << " " << w2[i].second << "\n"; } return 0; } |