#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define mid ((l+r)/2) #define siz(x) (int)(x).size() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) #define deb(x) cout << #x << ": " << x << "\n" typedef long long ll; typedef long double ld; typedef pair<int, int> ii; const int N = 30005; set<int> g[N]; set<int> h[N]; vector<bool> vis; struct out{ char c; int a; int b; }; vector<out> ans; int main(){ BOOST; int n, m, k; cin >> n >> m; for(int i=0; i<m; i++){ int a, b; cin >> a >> b; g[a].insert(b); g[b].insert(a); } cin >> k; for(int i=0; i<k; i++){ int a, b; cin >> a >> b; h[a].insert(b); h[b].insert(a); } queue<int> bfs; vis = vector<bool>(N); vis[1] = 1; for(auto it : g[1]){ vis[it] = 1; bfs.push(it); } while(siz(bfs)){ int x = bfs.front(); bfs.pop(); for(auto it : g[x]){ if(!vis[it]){ vis[it] = 1; bfs.push(it); g[1].insert(it); g[it].insert(1); ans.pb({'+', 1, it}); } } } for(int i=2; i<=n; i++){ for(auto it : h[i]){ if(!g[i].count(it)){ g[i].insert(it); g[it].insert(i); ans.pb({'+', i, it}); } } for(auto it = g[i].begin(); it != g[i].end();){ if(*it != 1 && !h[i].count(*it)){ // printf("i: %d, *it: %d\n", i, *it); ans.pb({'-', i, *it}); g[*it].erase(i); it = g[i].erase(it); } else it++; } } vector<int> v; vis = vector<bool>(N); vis[1] = 1; for(auto it : h[1]){ vis[it] = 1; bfs.push(it); } while(siz(bfs)){ int x = bfs.front(); bfs.pop(); if(!h[1].count(x)) v.pb(x); for(auto it : g[x]){ if(!vis[it]){ vis[it] = 1; bfs.push(it); } } } reverse(all(v)); for(auto it : v) ans.pb({'-', 1, it}); cout << siz(ans) << "\n"; for(auto it : ans){ cout << it.c << " " << it.a << " " << it.b << "\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 | #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define mid ((l+r)/2) #define siz(x) (int)(x).size() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) #define deb(x) cout << #x << ": " << x << "\n" typedef long long ll; typedef long double ld; typedef pair<int, int> ii; const int N = 30005; set<int> g[N]; set<int> h[N]; vector<bool> vis; struct out{ char c; int a; int b; }; vector<out> ans; int main(){ BOOST; int n, m, k; cin >> n >> m; for(int i=0; i<m; i++){ int a, b; cin >> a >> b; g[a].insert(b); g[b].insert(a); } cin >> k; for(int i=0; i<k; i++){ int a, b; cin >> a >> b; h[a].insert(b); h[b].insert(a); } queue<int> bfs; vis = vector<bool>(N); vis[1] = 1; for(auto it : g[1]){ vis[it] = 1; bfs.push(it); } while(siz(bfs)){ int x = bfs.front(); bfs.pop(); for(auto it : g[x]){ if(!vis[it]){ vis[it] = 1; bfs.push(it); g[1].insert(it); g[it].insert(1); ans.pb({'+', 1, it}); } } } for(int i=2; i<=n; i++){ for(auto it : h[i]){ if(!g[i].count(it)){ g[i].insert(it); g[it].insert(i); ans.pb({'+', i, it}); } } for(auto it = g[i].begin(); it != g[i].end();){ if(*it != 1 && !h[i].count(*it)){ // printf("i: %d, *it: %d\n", i, *it); ans.pb({'-', i, *it}); g[*it].erase(i); it = g[i].erase(it); } else it++; } } vector<int> v; vis = vector<bool>(N); vis[1] = 1; for(auto it : h[1]){ vis[it] = 1; bfs.push(it); } while(siz(bfs)){ int x = bfs.front(); bfs.pop(); if(!h[1].count(x)) v.pb(x); for(auto it : g[x]){ if(!vis[it]){ vis[it] = 1; bfs.push(it); } } } reverse(all(v)); for(auto it : v) ans.pb({'-', 1, it}); cout << siz(ans) << "\n"; for(auto it : ans){ cout << it.c << " " << it.a << " " << it.b << "\n"; } } |