#include <bits/stdc++.h> #ifdef dbg #define D(...) fprintf(stderr, __VA_ARGS__) #define DD(...) D(#__VA_ARGS__ " = "), debug_helper::debug(__VA_ARGS__), D("\n") #include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp" #else #define D(...) ((void)0) #define DD(...) ((void)0) #endif #define rep(i, j, k) for (int i = (j); i <= (k); ++i) #define per(i, j, k) for (int i = (j); i >= (k); --i) #define SZ(v) int((v).size()) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using namespace std; const int maxn = 100010; int n, m, dep[maxn]; vector<int> e[maxn]; queue<int> q; pii ed[maxn]; vector<tuple<int, int, int>> ans1, ans2; int main() { cin.tie(nullptr) -> ios::sync_with_stdio(false); cin >> n; rep (i, 1, n) dep[i] = 0, e[i].clear(); cin >> m; rep (i, 1, m) { int u, v; cin >> u >> v; e[u].emplace_back(v); e[v].emplace_back(u); ed[i] = pii(u, v); } dep[1] = 1, q.emplace(1); while (!q.empty()) { int u = q.front(); q.pop(); if (dep[u] >= 3) ans1.emplace_back(1, 1, u); for (int v : e[u]) if (!dep[v]) dep[v] = dep[u] + 1, q.emplace(v); } rep (i, 1, m) if (ed[i].fi != 1 && ed[i].se != 1) ans1.emplace_back(-1, ed[i].fi, ed[i].se); rep (i, 1, n) dep[i] = 0, e[i].clear(); cin >> m; rep (i, 1, m) { int u, v; cin >> u >> v; e[u].emplace_back(v); e[v].emplace_back(u); ed[i] = pii(u, v); } dep[1] = 1, q.emplace(1); while (!q.empty()) { int u = q.front(); q.pop(); if (dep[u] >= 3) ans2.emplace_back(-1, 1, u); for (int v : e[u]) if (!dep[v]) dep[v] = dep[u] + 1, q.emplace(v); } rep (i, 1, m) if (ed[i].fi != 1 && ed[i].se != 1) ans2.emplace_back(1, ed[i].fi, ed[i].se); reverse(ALL(ans2)); cout << SZ(ans1) + SZ(ans2) << '\n'; for (auto [a, b, c] : ans1) { if (a == 1) cout << "+ " << b << " " << c << '\n'; else cout << "- " << b << " " << c << '\n'; } for (auto [a, b, c] : ans2) { if (a == 1) cout << "+ " << b << " " << c << '\n'; else cout << "- " << b << " " << c << '\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 | #include <bits/stdc++.h> #ifdef dbg #define D(...) fprintf(stderr, __VA_ARGS__) #define DD(...) D(#__VA_ARGS__ " = "), debug_helper::debug(__VA_ARGS__), D("\n") #include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp" #else #define D(...) ((void)0) #define DD(...) ((void)0) #endif #define rep(i, j, k) for (int i = (j); i <= (k); ++i) #define per(i, j, k) for (int i = (j); i >= (k); --i) #define SZ(v) int((v).size()) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using namespace std; const int maxn = 100010; int n, m, dep[maxn]; vector<int> e[maxn]; queue<int> q; pii ed[maxn]; vector<tuple<int, int, int>> ans1, ans2; int main() { cin.tie(nullptr) -> ios::sync_with_stdio(false); cin >> n; rep (i, 1, n) dep[i] = 0, e[i].clear(); cin >> m; rep (i, 1, m) { int u, v; cin >> u >> v; e[u].emplace_back(v); e[v].emplace_back(u); ed[i] = pii(u, v); } dep[1] = 1, q.emplace(1); while (!q.empty()) { int u = q.front(); q.pop(); if (dep[u] >= 3) ans1.emplace_back(1, 1, u); for (int v : e[u]) if (!dep[v]) dep[v] = dep[u] + 1, q.emplace(v); } rep (i, 1, m) if (ed[i].fi != 1 && ed[i].se != 1) ans1.emplace_back(-1, ed[i].fi, ed[i].se); rep (i, 1, n) dep[i] = 0, e[i].clear(); cin >> m; rep (i, 1, m) { int u, v; cin >> u >> v; e[u].emplace_back(v); e[v].emplace_back(u); ed[i] = pii(u, v); } dep[1] = 1, q.emplace(1); while (!q.empty()) { int u = q.front(); q.pop(); if (dep[u] >= 3) ans2.emplace_back(-1, 1, u); for (int v : e[u]) if (!dep[v]) dep[v] = dep[u] + 1, q.emplace(v); } rep (i, 1, m) if (ed[i].fi != 1 && ed[i].se != 1) ans2.emplace_back(1, ed[i].fi, ed[i].se); reverse(ALL(ans2)); cout << SZ(ans1) + SZ(ans2) << '\n'; for (auto [a, b, c] : ans1) { if (a == 1) cout << "+ " << b << " " << c << '\n'; else cout << "- " << b << " " << c << '\n'; } for (auto [a, b, c] : ans2) { if (a == 1) cout << "+ " << b << " " << c << '\n'; else cout << "- " << b << " " << c << '\n'; } } |