#include <bits/stdc++.h> using namespace std; #define loop(i, a, b) for(int i = a; i <= b; i++) #define loop_rev(i, a, b) for(int i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define eb emplace_back #define pb push_back using ui = uint32_t; using ll = int64_t; struct Event { char type; int a, b; }; tuple<int, vector<vector<int>>, set<pair<int,int>>> read_graph(int n) { vector<vector<int>> graph(n+1); set<pair<int,int>> edge_set; int m; cin >> m; loop(i, 0, m-1) { int a, b; cin >> a >> b; graph[a].eb(b), graph[b].eb(a); edge_set.emplace(min(a, b), max(a, b)); } return { sz(edge_set), graph, edge_set }; } void connect(int v, vector<vector<int>> const& graph, set<pair<int,int>> const& edges, vector<int>& vis, vector<Event>& res) { if(vis[v]) return; else vis[v] = 1; if(v != 1 && !edges.count({ 1, v })) res.eb('+', 1, v); for(int s : graph[v]) connect(s, graph, edges, vis, res); } void disconnect(int v, vector<vector<int>> const& graph, set<pair<int,int>> const& edges, vector<int>& vis, vector<Event>& res) { if(vis[v]) return; else vis[v] = 1; for(int s : graph[v]) disconnect(s, graph, edges, vis, res); if(v != 1 && !edges.count({ 1, v })) res.eb('-', 1, v); } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; auto [ ms, graph_s, edge_set_s ] = read_graph(n); auto [ md, graph_d, edge_set_d ] = read_graph(n); vector<Event> res; { // removing the old graph vector<int> vis_s(n+1); connect(1, graph_s, edge_set_s, vis_s, res); for(auto const& e : edge_set_s) { if(e.first != 1) res.eb('-', e.first, e.second); } } { // turning into the new graph vector<int> vis_d(n+1); for(auto const& e : edge_set_d) { if(e.first != 1) res.eb('+', e.first, e.second); } disconnect(1, graph_d, edge_set_d, vis_d, res); } cout << sz(res) << '\n'; for(auto const& r : res) cout << r.type << ' ' << r.a << ' ' << r.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 | #include <bits/stdc++.h> using namespace std; #define loop(i, a, b) for(int i = a; i <= b; i++) #define loop_rev(i, a, b) for(int i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define eb emplace_back #define pb push_back using ui = uint32_t; using ll = int64_t; struct Event { char type; int a, b; }; tuple<int, vector<vector<int>>, set<pair<int,int>>> read_graph(int n) { vector<vector<int>> graph(n+1); set<pair<int,int>> edge_set; int m; cin >> m; loop(i, 0, m-1) { int a, b; cin >> a >> b; graph[a].eb(b), graph[b].eb(a); edge_set.emplace(min(a, b), max(a, b)); } return { sz(edge_set), graph, edge_set }; } void connect(int v, vector<vector<int>> const& graph, set<pair<int,int>> const& edges, vector<int>& vis, vector<Event>& res) { if(vis[v]) return; else vis[v] = 1; if(v != 1 && !edges.count({ 1, v })) res.eb('+', 1, v); for(int s : graph[v]) connect(s, graph, edges, vis, res); } void disconnect(int v, vector<vector<int>> const& graph, set<pair<int,int>> const& edges, vector<int>& vis, vector<Event>& res) { if(vis[v]) return; else vis[v] = 1; for(int s : graph[v]) disconnect(s, graph, edges, vis, res); if(v != 1 && !edges.count({ 1, v })) res.eb('-', 1, v); } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; auto [ ms, graph_s, edge_set_s ] = read_graph(n); auto [ md, graph_d, edge_set_d ] = read_graph(n); vector<Event> res; { // removing the old graph vector<int> vis_s(n+1); connect(1, graph_s, edge_set_s, vis_s, res); for(auto const& e : edge_set_s) { if(e.first != 1) res.eb('-', e.first, e.second); } } { // turning into the new graph vector<int> vis_d(n+1); for(auto const& e : edge_set_d) { if(e.first != 1) res.eb('+', e.first, e.second); } disconnect(1, graph_d, edge_set_d, vis_d, res); } cout << sz(res) << '\n'; for(auto const& r : res) cout << r.type << ' ' << r.a << ' ' << r.b << '\n'; } |