#include <bits/stdc++.h> #define FOR(i, a, b) for(int i =(a); i < (b); ++i) #define re(i, n) FOR(i, 1, n) #define ford(i, a, b) for(int i = (a); i >= (b); --i) #define rep(i, n) for(int i = 0;i <(n); ++i) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pll> vpll; typedef vector<pii> vpii; const ll inf = 1e18; const int N = 5005; const int mod = 1e9 + 7; int n, m_start, m_end; vector<vi> Adj1; vector<vi> Adj2; set<pii> e1; set<pii> e2; vector<pii> o1; vector<pii> o2; vector<bool> vis; void bfs(int s) { vis[s] = true; queue<int> q; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (int u : Adj1[v]) { if (!vis[u]) { if (!e1.count({s, u}) && !e1.count({u, s})) { o1.push_back({s, u}); } vis[u] = true; q.push(u); } } } } void solve() { vis.clear(); vis.resize(n); bfs(0); for (auto& e : e2) { if (min(e.first, e.second) > 0 && !e1.count(e) && !e1.count({e.second, e.first})) { o1.push_back(e); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m_start; Adj1.resize(n); Adj2.resize(n); rep(i, m_start) { int a, b; cin >> a >> b; a--; b--; Adj1[a].push_back(b); Adj1[b].push_back(a); e1.insert({a, b}); } cin >> m_end; rep(i, m_end) { int a, b; cin >> a >> b; a--; b--; Adj2[a].push_back(b); Adj2[b].push_back(a); e2.insert({a, b}); } solve(); swap(Adj1, Adj2); swap(e1, e2); swap(o1, o2); solve(); swap(o1, o2); cout << sz(o1) + sz(o2) << '\n'; for (auto& e : o1) { cout << "+ " << e.first + 1 << " " << e.second + 1 << '\n'; } reverse(all(o2)); for (auto& e : o2) { cout << "- " << e.first + 1 << " " << e.second + 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #include <bits/stdc++.h> #define FOR(i, a, b) for(int i =(a); i < (b); ++i) #define re(i, n) FOR(i, 1, n) #define ford(i, a, b) for(int i = (a); i >= (b); --i) #define rep(i, n) for(int i = 0;i <(n); ++i) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pll> vpll; typedef vector<pii> vpii; const ll inf = 1e18; const int N = 5005; const int mod = 1e9 + 7; int n, m_start, m_end; vector<vi> Adj1; vector<vi> Adj2; set<pii> e1; set<pii> e2; vector<pii> o1; vector<pii> o2; vector<bool> vis; void bfs(int s) { vis[s] = true; queue<int> q; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (int u : Adj1[v]) { if (!vis[u]) { if (!e1.count({s, u}) && !e1.count({u, s})) { o1.push_back({s, u}); } vis[u] = true; q.push(u); } } } } void solve() { vis.clear(); vis.resize(n); bfs(0); for (auto& e : e2) { if (min(e.first, e.second) > 0 && !e1.count(e) && !e1.count({e.second, e.first})) { o1.push_back(e); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m_start; Adj1.resize(n); Adj2.resize(n); rep(i, m_start) { int a, b; cin >> a >> b; a--; b--; Adj1[a].push_back(b); Adj1[b].push_back(a); e1.insert({a, b}); } cin >> m_end; rep(i, m_end) { int a, b; cin >> a >> b; a--; b--; Adj2[a].push_back(b); Adj2[b].push_back(a); e2.insert({a, b}); } solve(); swap(Adj1, Adj2); swap(e1, e2); swap(o1, o2); solve(); swap(o1, o2); cout << sz(o1) + sz(o2) << '\n'; for (auto& e : o1) { cout << "+ " << e.first + 1 << " " << e.second + 1 << '\n'; } reverse(all(o2)); for (auto& e : o2) { cout << "- " << e.first + 1 << " " << e.second + 1 << '\n'; } } |