#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 2137
#endif
const int N = 30030;
int n;
vector<int> adj[N];
bool vis[N];
vector<pair<int, int>> rozw(auto s, auto t) {
for (int i = 0; i < n; i++) {
adj[i].clear();
vis[i] = false;
}
set<pair<int, int>> e;
for (auto [x, y] : s) {
if (x > y) {
swap(x, y);
}
e.insert({x, y});
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<pair<int, int>> ops;
auto op = [&] (int x, int y) {
if (x > y) {
swap(x, y);
}
if (!e.count({x, y})) {
ops.push_back({x, y});
e.insert({x, y});
}
};
queue<int> q;
vis[0] = true;
q.push(0);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
if (!vis[v]) {
op(0, v);
vis[v] = true;
q.push(v);
}
}
}
for (auto [x, y] : t) {
op(x, y);
}
return ops;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ms;
cin >> n >> ms;
vector<pair<int, int>> s(ms);
for (int i = 0; i < ms; i++) {
int x, y;
cin >> x >> y;
x--; y--;
s[i] = {x, y};
}
int mt;
cin >> mt;
vector<pair<int, int>> t(mt);
for (int i = 0; i < mt; i++) {
int x, y;
cin >> x >> y;
x--; y--;
t[i] = {x, y};
}
auto a = rozw(s, t);
auto b = rozw(t, s);
reverse(b.begin(), b.end());
int sz = ssize(a) + ssize(b);
assert(sz <= 200000);
cout << sz << '\n';
for (auto [x, y] : a) {
cout << "+ " << x + 1 << ' ' << y + 1 << '\n';
}
for (auto [x, y] : b) {
cout << "- " << x + 1 << ' ' << y + 1 << '\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 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include "debug.h" #else #define debug(...) 2137 #endif const int N = 30030; int n; vector<int> adj[N]; bool vis[N]; vector<pair<int, int>> rozw(auto s, auto t) { for (int i = 0; i < n; i++) { adj[i].clear(); vis[i] = false; } set<pair<int, int>> e; for (auto [x, y] : s) { if (x > y) { swap(x, y); } e.insert({x, y}); adj[x].push_back(y); adj[y].push_back(x); } vector<pair<int, int>> ops; auto op = [&] (int x, int y) { if (x > y) { swap(x, y); } if (!e.count({x, y})) { ops.push_back({x, y}); e.insert({x, y}); } }; queue<int> q; vis[0] = true; q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (!vis[v]) { op(0, v); vis[v] = true; q.push(v); } } } for (auto [x, y] : t) { op(x, y); } return ops; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int ms; cin >> n >> ms; vector<pair<int, int>> s(ms); for (int i = 0; i < ms; i++) { int x, y; cin >> x >> y; x--; y--; s[i] = {x, y}; } int mt; cin >> mt; vector<pair<int, int>> t(mt); for (int i = 0; i < mt; i++) { int x, y; cin >> x >> y; x--; y--; t[i] = {x, y}; } auto a = rozw(s, t); auto b = rozw(t, s); reverse(b.begin(), b.end()); int sz = ssize(a) + ssize(b); assert(sz <= 200000); cout << sz << '\n'; for (auto [x, y] : a) { cout << "+ " << x + 1 << ' ' << y + 1 << '\n'; } for (auto [x, y] : b) { cout << "- " << x + 1 << ' ' << y + 1 << '\n'; } } |
English