#include <bits/stdc++.h> using namespace std; const int maxn = 50009; vector <int> t1[maxn],t2[maxn]; vector <int> k1 , k2; int czy1[maxn] , czy2[maxn]; int vis[maxn]; unordered_map<int,bool> M1 , M2; vector <pair<int,int>> kon , pocz; vector <pair<bool,pair<int,int>>> res; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n , m1,m2 , a , b; cin >> n; cin >> m1; for(int i = 0 ; i < m1 ; i++) { cin >> a >> b; if(a > b)swap(a,b); pocz.push_back({a,b}); M1[a*maxn+b] = 1; if(a == 1) { czy1[b] = 1; } t1[a].push_back(b); t1[b].push_back(a); } cin >> m2; for(int i = 0 ; i < m2 ; i++) { cin >> a >> b; if(a > b)swap(a,b); kon.push_back({a,b}); M2[a*maxn+b] = 1; if(a == 1) { czy2[b] = 1; } t2[a].push_back(b); t2[b].push_back(a); } queue <int> q; q.push(1); vis[1] = 1; while(!q.empty()) { a = q.front(); q.pop(); for(int i = 0 ; i < t1[a].size() ; i++) { b = t1[a][i]; if(vis[b] == 0) { q.push(b); vis[b] = 1; k1.push_back(b); } } } for(int i = 0 ; i <= n ; i++)vis[i] = 0; q.push(1); vis[1] = 1; while(!q.empty()) { a = q.front(); q.pop(); for(int i = 0 ; i < t2[a].size() ; i++) { b = t2[a][i]; if(vis[b] == 0) { q.push(b); vis[b] = 1; k2.push_back(b); } } } reverse(k2.begin() , k2.end()); for(int i = 0; i < k1.size() ; i++) { if(czy1[k1[i]] == 1)continue; res.push_back({1,{1,k1[i]}}); } for(int i = 0; i < kon.size() ; i++) { if(kon[i].first == 1)continue; if(M1[kon[i].first *maxn + kon[i].second] == 0) { res.push_back({1,{kon[i].first,kon[i].second}}); } } for(int i = 0; i < pocz.size() ; i++) { if(pocz[i].first == 1)continue; if(M2[pocz[i].first*maxn +pocz[i].second] == 0) { res.push_back({0,{pocz[i].first,pocz[i].second}}); } } for(int i = 0; i < k2.size() ; i++) { if(czy2[k2[i]] == 1)continue; res.push_back({0,{1,k2[i]}}); } cout << res.size() << '\n'; for(int i = 0 ; i < res.size() ; i++) { if(res[i].first == 1)cout << "+ "; else cout << "- "; cout << res[i].second.first << ' ' << res[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; const int maxn = 50009; vector <int> t1[maxn],t2[maxn]; vector <int> k1 , k2; int czy1[maxn] , czy2[maxn]; int vis[maxn]; unordered_map<int,bool> M1 , M2; vector <pair<int,int>> kon , pocz; vector <pair<bool,pair<int,int>>> res; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n , m1,m2 , a , b; cin >> n; cin >> m1; for(int i = 0 ; i < m1 ; i++) { cin >> a >> b; if(a > b)swap(a,b); pocz.push_back({a,b}); M1[a*maxn+b] = 1; if(a == 1) { czy1[b] = 1; } t1[a].push_back(b); t1[b].push_back(a); } cin >> m2; for(int i = 0 ; i < m2 ; i++) { cin >> a >> b; if(a > b)swap(a,b); kon.push_back({a,b}); M2[a*maxn+b] = 1; if(a == 1) { czy2[b] = 1; } t2[a].push_back(b); t2[b].push_back(a); } queue <int> q; q.push(1); vis[1] = 1; while(!q.empty()) { a = q.front(); q.pop(); for(int i = 0 ; i < t1[a].size() ; i++) { b = t1[a][i]; if(vis[b] == 0) { q.push(b); vis[b] = 1; k1.push_back(b); } } } for(int i = 0 ; i <= n ; i++)vis[i] = 0; q.push(1); vis[1] = 1; while(!q.empty()) { a = q.front(); q.pop(); for(int i = 0 ; i < t2[a].size() ; i++) { b = t2[a][i]; if(vis[b] == 0) { q.push(b); vis[b] = 1; k2.push_back(b); } } } reverse(k2.begin() , k2.end()); for(int i = 0; i < k1.size() ; i++) { if(czy1[k1[i]] == 1)continue; res.push_back({1,{1,k1[i]}}); } for(int i = 0; i < kon.size() ; i++) { if(kon[i].first == 1)continue; if(M1[kon[i].first *maxn + kon[i].second] == 0) { res.push_back({1,{kon[i].first,kon[i].second}}); } } for(int i = 0; i < pocz.size() ; i++) { if(pocz[i].first == 1)continue; if(M2[pocz[i].first*maxn +pocz[i].second] == 0) { res.push_back({0,{pocz[i].first,pocz[i].second}}); } } for(int i = 0; i < k2.size() ; i++) { if(czy2[k2[i]] == 1)continue; res.push_back({0,{1,k2[i]}}); } cout << res.size() << '\n'; for(int i = 0 ; i < res.size() ; i++) { if(res[i].first == 1)cout << "+ "; else cout << "- "; cout << res[i].second.first << ' ' << res[i].second.second << '\n'; } return 0; } |