#include <bits/stdc++.h> using namespace std; #define maxn 30010 int n; vector<int> graph[maxn], mid_graph[maxn], output_graph[maxn]; vector<pair<char, pair<int,int>>> output; bool ommit[maxn]; int odw[maxn]; int time_cnt = 0; void read() { int a, b, m; cin >> n; cin >> m; for (int i = 0; i < m; ++i) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } cin >> m; for (int i = 0; i < m; ++i) { cin >> a >> b; output_graph[a].push_back(b); output_graph[b].push_back(a); } } void dfs_add_all_edges(int basic, int now) { odw[now] = time_cnt; if (!ommit[now]) { output.push_back({'+', {basic, now}}); graph[basic].push_back(now); graph[now].push_back(basic); } for (int i = 0; i < (int)graph[now].size(); ++i) { int x =graph[now][i]; if (odw[x] != time_cnt) { dfs_add_all_edges(basic, x); } } } void set_ommit(int x, vector<int> &edges) { for (int i = 1; i <= n; ++i) { ommit[i] = false; } ommit[x] = true; for (auto el : edges) { ommit[el] = true; } } void add_all_edges(int x) { time_cnt++; set_ommit(x, graph[x]); dfs_add_all_edges(x, x); // for (int i = 2; i <= n; ++i) { // for (int j = 0; j <= (int)output_graph[i].size(); ++j) { // int y = output_graph[i][j]; // cout << y << " "; // } // cout << endl; // } for (int i = 2; i <= n; ++i) { set<int> s(graph[i].begin(), graph[i].end()); for (int j = 0; j < (int)output_graph[i].size(); ++j) { int y = output_graph[i][j]; if (s.find(y) == s.end()) { output.push_back({'+', {i, y}}); graph[i].push_back(y); graph[y].push_back(i); } } } } void dfs_remove_all_edges(int basic, int now) { odw[now] = time_cnt; for (int i = 0; i < (int)mid_graph[now].size(); ++i) { int x = mid_graph[now][i]; if (odw[x] != time_cnt) { dfs_remove_all_edges(basic, x); } } if(!ommit[now]) { output.push_back({'-', {now, basic}}); } } void non_dfs_remove_all_edges() { for (int i = 1; i <= n; ++i) { set<int> s(output_graph[i].begin(), output_graph[i].end()); for (int j = 0; j < (int)graph[i].size(); ++j) { int x = graph[i][j]; bool ok = i == 1 || x == 1 || s.find(x) != s.end(); if (ok) { mid_graph[i].push_back(x); } else if (i < x) { output.push_back({'-', {i, x}}); } } } } void remove_all_edges() { time_cnt++; odw[1] = time_cnt; set_ommit(1, output_graph[1]); non_dfs_remove_all_edges(); for (int i = 0; i < (int)output_graph[1].size(); ++i) { int x = output_graph[1][i]; if (odw[x] != time_cnt) { //cout << x << endl; dfs_remove_all_edges(1, x); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); add_all_edges(1); remove_all_edges(); cout << (int)output.size() << endl; for (auto el : output) { cout << el.first << " " << el.second.first << " " << el.second.second << endl; } }
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <bits/stdc++.h> using namespace std; #define maxn 30010 int n; vector<int> graph[maxn], mid_graph[maxn], output_graph[maxn]; vector<pair<char, pair<int,int>>> output; bool ommit[maxn]; int odw[maxn]; int time_cnt = 0; void read() { int a, b, m; cin >> n; cin >> m; for (int i = 0; i < m; ++i) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } cin >> m; for (int i = 0; i < m; ++i) { cin >> a >> b; output_graph[a].push_back(b); output_graph[b].push_back(a); } } void dfs_add_all_edges(int basic, int now) { odw[now] = time_cnt; if (!ommit[now]) { output.push_back({'+', {basic, now}}); graph[basic].push_back(now); graph[now].push_back(basic); } for (int i = 0; i < (int)graph[now].size(); ++i) { int x =graph[now][i]; if (odw[x] != time_cnt) { dfs_add_all_edges(basic, x); } } } void set_ommit(int x, vector<int> &edges) { for (int i = 1; i <= n; ++i) { ommit[i] = false; } ommit[x] = true; for (auto el : edges) { ommit[el] = true; } } void add_all_edges(int x) { time_cnt++; set_ommit(x, graph[x]); dfs_add_all_edges(x, x); // for (int i = 2; i <= n; ++i) { // for (int j = 0; j <= (int)output_graph[i].size(); ++j) { // int y = output_graph[i][j]; // cout << y << " "; // } // cout << endl; // } for (int i = 2; i <= n; ++i) { set<int> s(graph[i].begin(), graph[i].end()); for (int j = 0; j < (int)output_graph[i].size(); ++j) { int y = output_graph[i][j]; if (s.find(y) == s.end()) { output.push_back({'+', {i, y}}); graph[i].push_back(y); graph[y].push_back(i); } } } } void dfs_remove_all_edges(int basic, int now) { odw[now] = time_cnt; for (int i = 0; i < (int)mid_graph[now].size(); ++i) { int x = mid_graph[now][i]; if (odw[x] != time_cnt) { dfs_remove_all_edges(basic, x); } } if(!ommit[now]) { output.push_back({'-', {now, basic}}); } } void non_dfs_remove_all_edges() { for (int i = 1; i <= n; ++i) { set<int> s(output_graph[i].begin(), output_graph[i].end()); for (int j = 0; j < (int)graph[i].size(); ++j) { int x = graph[i][j]; bool ok = i == 1 || x == 1 || s.find(x) != s.end(); if (ok) { mid_graph[i].push_back(x); } else if (i < x) { output.push_back({'-', {i, x}}); } } } } void remove_all_edges() { time_cnt++; odw[1] = time_cnt; set_ommit(1, output_graph[1]); non_dfs_remove_all_edges(); for (int i = 0; i < (int)output_graph[1].size(); ++i) { int x = output_graph[1][i]; if (odw[x] != time_cnt) { //cout << x << endl; dfs_remove_all_edges(1, x); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); add_all_edges(1); remove_all_edges(); cout << (int)output.size() << endl; for (auto el : output) { cout << el.first << " " << el.second.first << " " << el.second.second << endl; } } |