// #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> using namespace std; #define PB emplace_back #define int long long #define ll long long #define vi vector<int> #define siz(a) ((int) ((a).size())) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; } const int N = 3e4; int a, b; struct pii { int o, x, y; }; vector<pii> s, t; vi st[N + 5]; bool vis[N + 5]; void bfs(vector<pii> &s) { queue<int> qu; vis[1] = 1; for(int x : st[1]) { vis[x] = 1, qu.push(x); for(int y : st[x]) if(vis[y] && y != 1) { s.PB(pii {1, x, y}); } } while(siz(qu)) { int u = qu.front(); qu.pop(); for(int v : st[u]) { if(vis[v]) continue; vis[v] = 1; qu.push(v); s.PB(pii {0, 1, v}); for(int x : st[v]) if(vis[x]) { assert(x != 1); s.PB(pii {1, v, x}); } } } } signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> a; cin >> b; int x, y; rep(i, 1, b) { cin >> x >> y; st[x].PB(y), st[y].PB(x); } bfs(s); rep(i, 1, a) st[i].clear(), vis[i] = 0; cin >> b; rep(i, 1, b) { cin >> x >> y; st[x].PB(y), st[y].PB(x); } bfs(t); // cerr << siz(s) << ' ' << siz(t) << endl; cout << siz(s) + siz(t) << '\n'; for(auto [o, x, y] : s) cout << (o ? "- " : "+ ") << x << ' ' << y << '\n'; reverse(t.begin(), t.end()); for(auto [o, x, y] : t) cout << (o ? "+ " : "- ") << x << ' ' << y << '\n'; return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 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 | // #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> using namespace std; #define PB emplace_back #define int long long #define ll long long #define vi vector<int> #define siz(a) ((int) ((a).size())) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; } const int N = 3e4; int a, b; struct pii { int o, x, y; }; vector<pii> s, t; vi st[N + 5]; bool vis[N + 5]; void bfs(vector<pii> &s) { queue<int> qu; vis[1] = 1; for(int x : st[1]) { vis[x] = 1, qu.push(x); for(int y : st[x]) if(vis[y] && y != 1) { s.PB(pii {1, x, y}); } } while(siz(qu)) { int u = qu.front(); qu.pop(); for(int v : st[u]) { if(vis[v]) continue; vis[v] = 1; qu.push(v); s.PB(pii {0, 1, v}); for(int x : st[v]) if(vis[x]) { assert(x != 1); s.PB(pii {1, v, x}); } } } } signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> a; cin >> b; int x, y; rep(i, 1, b) { cin >> x >> y; st[x].PB(y), st[y].PB(x); } bfs(s); rep(i, 1, a) st[i].clear(), vis[i] = 0; cin >> b; rep(i, 1, b) { cin >> x >> y; st[x].PB(y), st[y].PB(x); } bfs(t); // cerr << siz(s) << ' ' << siz(t) << endl; cout << siz(s) + siz(t) << '\n'; for(auto [o, x, y] : s) cout << (o ? "- " : "+ ") << x << ' ' << y << '\n'; reverse(t.begin(), t.end()); for(auto [o, x, y] : t) cout << (o ? "+ " : "- ") << x << ' ' << y << '\n'; return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 0; } |