#include <iostream> #include <vector> #include <algorithm> #include <set> #include <utility> #include <array> using namespace std; void input(int & n, int & m_start, vector<vector<int>> & start_neigh, set<pair<int,int>> & cur_set, int & m_dest, vector<vector<int>> & dest_neigh, set<pair<int,int>> & dest_set) { cin >> n; start_neigh.resize(n, vector<int>()); dest_neigh.resize(n, vector<int>()); cin >> m_start; for(int i = 0; i < m_start; i++) { int a, b; cin >> a >> b; a--; b--; if(a > b){ swap(a, b); } start_neigh[a].push_back(b); start_neigh[b].push_back(a); cur_set.insert({a,b}); } cin >> m_dest; for(int i = 0; i < m_dest; i++) { int a, b; cin >> a >> b; a--; b--; if(a > b){ swap(a, b); } dest_neigh[a].push_back(b); dest_neigh[b].push_back(a); dest_set.insert({a,b}); } } void start_dfs(int const ver, vector<vector<int>> const & start_neigh, set<pair<int,int>> & cur_set, vector<array<int,3>> & moves, vector<bool> & visited) { if(ver != 0 && cur_set.count({0,ver}) == 0) { cur_set.insert({0,ver}); moves.push_back({0,ver,1}); } visited[ver] = 1; for(auto const neigh : start_neigh[ver]) { if(!visited[neigh]) { start_dfs(neigh, start_neigh, cur_set, moves, visited); } } } void dest_dfs(int const ver, vector<vector<int>> const & dest_neigh, set<pair<int,int>> & cur_set, vector<array<int,3>> & moves, vector<bool> & visited, vector<pair<int,int>> & dest_stack) { if(ver != 0) { dest_stack.push_back({0,ver}); } visited[ver] = 1; for(auto const neigh : dest_neigh[ver]) { if(!visited[neigh]) { dest_dfs(neigh, dest_neigh, cur_set, moves, visited, dest_stack); } } } void solve() { int n; int m_start; vector<vector<int>> start_neigh; set<pair<int,int>> cur_set; int m_dest; vector<vector<int>> dest_neigh; set<pair<int,int>> dest_set; input(n, m_start, start_neigh, cur_set, m_dest, dest_neigh, dest_set); vector<array<int,3>> moves; vector<bool> visited(n, 0); start_dfs(0, start_neigh, cur_set, moves, visited); for(auto const e : dest_set) { if(cur_set.count(e) == 0) { cur_set.insert(e); moves.push_back({e.first,e.second,1}); } } vector<pair<int,int>> dest_stack; fill(visited.begin(), visited.end(), 0); dest_dfs(0, dest_neigh, cur_set, moves, visited, dest_stack); vector<pair<int,int>> to_remove; for(auto const e : cur_set) { if(e.first != 0 && dest_set.count(e) == 0) { to_remove.push_back(e); } } for(auto const e : to_remove) { moves.push_back({e.first,e.second,-1}); } for(int i = dest_stack.size() - 1; i >= 0; i--) { if(dest_set.count(dest_stack[i]) == 0) { moves.push_back({dest_stack[i].first,dest_stack[i].second,-1}); } } cout << moves.size() << "\n"; for(auto const i : moves) { if(i[2] == 1) { cout << "+ "; } else if(i[2] == -1) { cout << "- "; } cout << i[0] + 1 << " " << i[1] + 1 << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); 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 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 | #include <iostream> #include <vector> #include <algorithm> #include <set> #include <utility> #include <array> using namespace std; void input(int & n, int & m_start, vector<vector<int>> & start_neigh, set<pair<int,int>> & cur_set, int & m_dest, vector<vector<int>> & dest_neigh, set<pair<int,int>> & dest_set) { cin >> n; start_neigh.resize(n, vector<int>()); dest_neigh.resize(n, vector<int>()); cin >> m_start; for(int i = 0; i < m_start; i++) { int a, b; cin >> a >> b; a--; b--; if(a > b){ swap(a, b); } start_neigh[a].push_back(b); start_neigh[b].push_back(a); cur_set.insert({a,b}); } cin >> m_dest; for(int i = 0; i < m_dest; i++) { int a, b; cin >> a >> b; a--; b--; if(a > b){ swap(a, b); } dest_neigh[a].push_back(b); dest_neigh[b].push_back(a); dest_set.insert({a,b}); } } void start_dfs(int const ver, vector<vector<int>> const & start_neigh, set<pair<int,int>> & cur_set, vector<array<int,3>> & moves, vector<bool> & visited) { if(ver != 0 && cur_set.count({0,ver}) == 0) { cur_set.insert({0,ver}); moves.push_back({0,ver,1}); } visited[ver] = 1; for(auto const neigh : start_neigh[ver]) { if(!visited[neigh]) { start_dfs(neigh, start_neigh, cur_set, moves, visited); } } } void dest_dfs(int const ver, vector<vector<int>> const & dest_neigh, set<pair<int,int>> & cur_set, vector<array<int,3>> & moves, vector<bool> & visited, vector<pair<int,int>> & dest_stack) { if(ver != 0) { dest_stack.push_back({0,ver}); } visited[ver] = 1; for(auto const neigh : dest_neigh[ver]) { if(!visited[neigh]) { dest_dfs(neigh, dest_neigh, cur_set, moves, visited, dest_stack); } } } void solve() { int n; int m_start; vector<vector<int>> start_neigh; set<pair<int,int>> cur_set; int m_dest; vector<vector<int>> dest_neigh; set<pair<int,int>> dest_set; input(n, m_start, start_neigh, cur_set, m_dest, dest_neigh, dest_set); vector<array<int,3>> moves; vector<bool> visited(n, 0); start_dfs(0, start_neigh, cur_set, moves, visited); for(auto const e : dest_set) { if(cur_set.count(e) == 0) { cur_set.insert(e); moves.push_back({e.first,e.second,1}); } } vector<pair<int,int>> dest_stack; fill(visited.begin(), visited.end(), 0); dest_dfs(0, dest_neigh, cur_set, moves, visited, dest_stack); vector<pair<int,int>> to_remove; for(auto const e : cur_set) { if(e.first != 0 && dest_set.count(e) == 0) { to_remove.push_back(e); } } for(auto const e : to_remove) { moves.push_back({e.first,e.second,-1}); } for(int i = dest_stack.size() - 1; i >= 0; i--) { if(dest_set.count(dest_stack[i]) == 0) { moves.push_back({dest_stack[i].first,dest_stack[i].second,-1}); } } cout << moves.size() << "\n"; for(auto const i : moves) { if(i[2] == 1) { cout << "+ "; } else if(i[2] == -1) { cout << "- "; } cout << i[0] + 1 << " " << i[1] + 1 << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |