#include <bits/stdc++.h>
using namespace std;
vector<set<int>> read_graph(int n) {
int m;
cin >> m;
vector<set<int>> graph(n);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a; --b;
graph[a].insert(b);
graph[b].insert(a);
}
return graph;
}
bool is_edge(vector<set<int>>& graph, int v, int u) {
return graph[v].find(u) != graph[v].end();
}
list<int> bfs(vector<set<int>>& graph, int s) {
list<int> ans;
vector<bool> visited(graph.size(), false);
visited[s] = true;
queue<int> q;
for (auto v : graph[s]) {
visited[v] = true;
q.push(v);
}
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : graph[u]) {
if (!visited[v]) {
visited[v] = true;
q.push(v);
ans.push_back(v);
}
}
}
return ans;
}
void print_ans(list<pair<char, pair<int, int>>>& ans) {
cout << ans.size() << "\n";
for (auto [op, vu] : ans) {
cout << op << " " << vu.first + 1 << " " << vu.second + 1 << "\n";
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<set<int>> graph_s = read_graph(n);
vector<set<int>> graph_d = read_graph(n);
list<pair<char, pair<int, int>>> ans;
list<int> span_s = bfs(graph_s, 0);
for (auto v : span_s) {
ans.push_back({'+', {0, v}});
}
for (int v = 1; v < n; ++v) {
for (auto u : graph_d[v]) {
if (v < u && !is_edge(graph_s, v, u)) {
ans.push_back({'+', {v, u}});
}
}
}
for (int v = 1; v < n; ++v) {
for (auto u : graph_s[v]) {
if (v < u && !is_edge(graph_d, v, u)) {
ans.push_back({'-', {v, u}});
}
}
}
list<int> span_d = bfs(graph_d, 0);
span_d.reverse();
for (auto v : span_d) {
ans.push_back({'-', {0, v}});
}
print_ans(ans);
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; vector<set<int>> read_graph(int n) { int m; cin >> m; vector<set<int>> graph(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; graph[a].insert(b); graph[b].insert(a); } return graph; } bool is_edge(vector<set<int>>& graph, int v, int u) { return graph[v].find(u) != graph[v].end(); } list<int> bfs(vector<set<int>>& graph, int s) { list<int> ans; vector<bool> visited(graph.size(), false); visited[s] = true; queue<int> q; for (auto v : graph[s]) { visited[v] = true; q.push(v); } while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : graph[u]) { if (!visited[v]) { visited[v] = true; q.push(v); ans.push_back(v); } } } return ans; } void print_ans(list<pair<char, pair<int, int>>>& ans) { cout << ans.size() << "\n"; for (auto [op, vu] : ans) { cout << op << " " << vu.first + 1 << " " << vu.second + 1 << "\n"; } } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<set<int>> graph_s = read_graph(n); vector<set<int>> graph_d = read_graph(n); list<pair<char, pair<int, int>>> ans; list<int> span_s = bfs(graph_s, 0); for (auto v : span_s) { ans.push_back({'+', {0, v}}); } for (int v = 1; v < n; ++v) { for (auto u : graph_d[v]) { if (v < u && !is_edge(graph_s, v, u)) { ans.push_back({'+', {v, u}}); } } } for (int v = 1; v < n; ++v) { for (auto u : graph_s[v]) { if (v < u && !is_edge(graph_d, v, u)) { ans.push_back({'-', {v, u}}); } } } list<int> span_d = bfs(graph_d, 0); span_d.reverse(); for (auto v : span_d) { ans.push_back({'-', {0, v}}); } print_ans(ans); return 0; } |
English