// Author : Jakub Rożek // Task : Alchemik Bajtazar // Contest : PA 2024 r2 B // Memory : O(n + m) // Time : O(m * log(m)) // Move : O(2(n+m)) ~ 160'000 // Solv : Rozwiązanie dobre #include "bits/stdc++.h" using namespace std; using LL = long long; template <typename T> using P = pair<T, T>; template <typename T> using VV = vector<vector<T>>; #define all(x) x.begin(), x.end() #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define REP(i,n) for(int i=0; i<(n); ++i) #define ssize(x) int((x).size()) #ifdef DEBUG template <typename T1, typename T2> auto&operator<<(auto&o,pair<T1,T2>p){return o<<'('<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";for(auto e:x)o<<","<<e;return o<<"}";} #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif // const int INF = 1'000'000'009; const int N = 30'000; struct Str { char z; int a, b; }; int n, m, x, y; VV<int> graph; VV<int> graph2; vector<P<int>> to_remove; set <P<int>> edges; set <P<int>> edges_want; bool visited[N+1]; vector <Str> ans; void add_edge(int a, int b, bool answer=true) { if (a > b) swap(a, b); if (edges.count({a, b}) == 1) return; edges.insert({a, b}); if (answer) { ans.push_back({'+', a, b}); } } void remove_edge(int a, int b, bool answer=true) { if (a > b) swap(a, b); if (edges.count({a, b}) == 0) return; edges.erase({a, b}); if (answer) { ans.push_back({'-', a, b}); } } void dfs1(int a) { if (visited[a] == 1) return; visited[a] = 1; if (a != 1) { add_edge(1, a); } for (auto i:graph[a]) { dfs1(i); } } void dfs2(int a) { if (visited[a] == 0) return; visited[a] = 0; for (auto i:graph2[a]) { dfs2(i); } if (a != 1) { remove_edge(1, a); } } void solution() { cin >> n; graph.resize(n+1); graph2.resize(n+1); cin >> m; REP (i, m) { cin >> x >> y; graph[x].push_back(y); graph[y].push_back(x); add_edge(x, y, false); } cin >> m; REP (i, m) { cin >> x >> y; graph2[x].push_back(y); graph2[y].push_back(x); if (x > y) swap(x, y); edges_want.insert({x, y}); } // Dodaje do odpowiedzi krwedzie typu 1-x. (n) dfs1(1); // Dodaje do odpowiedzi brakujące krawędzie. (m) for (auto i:edges_want) { add_edge(i.first, i.second); } // Usuwam krawedzie ze zbioru które są dobre. for (auto i:edges_want) { remove_edge(i.first, i.second, false); } // Usuwam z odpowiedzi krawedzie typu a-b, takie ze a,b != 1 (m) for (auto i:edges) { if (i.first == 1) continue; to_remove.push_back(i); } for (auto i:to_remove) { remove_edge(i.first, i.second); } // Usuwam z odpowiedzi krwedzie typu 1-x. (n) dfs2(1); // Wypisuje 2(n+m) cout << ans.size() << '\n'; for (auto i:ans) { cout << i.z << ' ' << i.a << ' ' << i.b << '\n'; } } int main() { cin.tie(0)->sync_with_stdio(0); int tests = 1; // cin>>tests; FOR (i, 1, tests) { solution(); } 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | // Author : Jakub Rożek // Task : Alchemik Bajtazar // Contest : PA 2024 r2 B // Memory : O(n + m) // Time : O(m * log(m)) // Move : O(2(n+m)) ~ 160'000 // Solv : Rozwiązanie dobre #include "bits/stdc++.h" using namespace std; using LL = long long; template <typename T> using P = pair<T, T>; template <typename T> using VV = vector<vector<T>>; #define all(x) x.begin(), x.end() #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define REP(i,n) for(int i=0; i<(n); ++i) #define ssize(x) int((x).size()) #ifdef DEBUG template <typename T1, typename T2> auto&operator<<(auto&o,pair<T1,T2>p){return o<<'('<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";for(auto e:x)o<<","<<e;return o<<"}";} #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif // const int INF = 1'000'000'009; const int N = 30'000; struct Str { char z; int a, b; }; int n, m, x, y; VV<int> graph; VV<int> graph2; vector<P<int>> to_remove; set <P<int>> edges; set <P<int>> edges_want; bool visited[N+1]; vector <Str> ans; void add_edge(int a, int b, bool answer=true) { if (a > b) swap(a, b); if (edges.count({a, b}) == 1) return; edges.insert({a, b}); if (answer) { ans.push_back({'+', a, b}); } } void remove_edge(int a, int b, bool answer=true) { if (a > b) swap(a, b); if (edges.count({a, b}) == 0) return; edges.erase({a, b}); if (answer) { ans.push_back({'-', a, b}); } } void dfs1(int a) { if (visited[a] == 1) return; visited[a] = 1; if (a != 1) { add_edge(1, a); } for (auto i:graph[a]) { dfs1(i); } } void dfs2(int a) { if (visited[a] == 0) return; visited[a] = 0; for (auto i:graph2[a]) { dfs2(i); } if (a != 1) { remove_edge(1, a); } } void solution() { cin >> n; graph.resize(n+1); graph2.resize(n+1); cin >> m; REP (i, m) { cin >> x >> y; graph[x].push_back(y); graph[y].push_back(x); add_edge(x, y, false); } cin >> m; REP (i, m) { cin >> x >> y; graph2[x].push_back(y); graph2[y].push_back(x); if (x > y) swap(x, y); edges_want.insert({x, y}); } // Dodaje do odpowiedzi krwedzie typu 1-x. (n) dfs1(1); // Dodaje do odpowiedzi brakujące krawędzie. (m) for (auto i:edges_want) { add_edge(i.first, i.second); } // Usuwam krawedzie ze zbioru które są dobre. for (auto i:edges_want) { remove_edge(i.first, i.second, false); } // Usuwam z odpowiedzi krawedzie typu a-b, takie ze a,b != 1 (m) for (auto i:edges) { if (i.first == 1) continue; to_remove.push_back(i); } for (auto i:to_remove) { remove_edge(i.first, i.second); } // Usuwam z odpowiedzi krwedzie typu 1-x. (n) dfs2(1); // Wypisuje 2(n+m) cout << ans.size() << '\n'; for (auto i:ans) { cout << i.z << ' ' << i.a << ' ' << i.b << '\n'; } } int main() { cin.tie(0)->sync_with_stdio(0); int tests = 1; // cin>>tests; FOR (i, 1, tests) { solution(); } return 0; } |