#include <bits/stdc++.h> using namespace std; vector<pair<bool, pair<int, int>>> answer; int main(){ ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); int n; cin>>n; vector<set<int>> graph(n+1); set<pair<int, int>> edges, toadd, todel; //------------------- int m, u, v; cin>>m; while(m--){ cin>>u>>v; if(u > v) swap(u, v); edges.insert({u, v}); graph[u].insert(v); graph[v].insert(u); } todel = edges; //------------------- cin>>m; while(m--){ cin>>u>>v; if(u > v) swap(u, v); if(edges.count({u, v})) todel.erase({u, v}); else toadd.insert({u, v}); } //---------------------------------------------- vector<bool> visited(n+1, 0); visited[1] = 1; queue<int> q; q.push(1); while(!q.empty()){ int u = q.front(); q.pop(); for(auto v: graph[u]){ if(visited[v]) continue; if(!edges.count({1, v})){ answer.push_back({1, {1, v}}); graph[1].insert(v); graph[v].insert(1); if(!toadd.count({1, v})) todel.insert({1, v}); else toadd.erase({1, v}); } visited[v] = 1; q.push(v); } } for(auto k: toadd) {answer.push_back({1, {k.first, k.second}}); graph[k.first].insert(k.second); graph[k.second].insert(k.first);}; set<pair<int, int>> todel2 = todel; for(auto k: todel) if(k.first != 1){answer.push_back({0, {k.first, k.second}}); graph[k.first].erase(k.second); graph[k.second].erase(k.first); todel2.erase(k);} visited.assign(n+1, 0); q.push(1); vector<int> order; while(!q.empty()){ int u = q.front(); q.pop(); for(auto v: graph[u]){ if(visited[v]) continue; if(todel2.count({u, v})) continue; visited[v] = 1; q.push(v); } order.push_back(u); } for(int i=(int)(order.size())-1; i>0; i--) if(todel.count({1, order[i]})) answer.push_back({0, {1, order[i]}}); //------------------- cout<<answer.size()<<'\n'; for(auto &k: answer){ if(k.first) cout<<"+ "; else cout<<"- "; cout<<k.second.first<<' '<<k.second.second<<'\n'; } 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 | #include <bits/stdc++.h> using namespace std; vector<pair<bool, pair<int, int>>> answer; int main(){ ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); int n; cin>>n; vector<set<int>> graph(n+1); set<pair<int, int>> edges, toadd, todel; //------------------- int m, u, v; cin>>m; while(m--){ cin>>u>>v; if(u > v) swap(u, v); edges.insert({u, v}); graph[u].insert(v); graph[v].insert(u); } todel = edges; //------------------- cin>>m; while(m--){ cin>>u>>v; if(u > v) swap(u, v); if(edges.count({u, v})) todel.erase({u, v}); else toadd.insert({u, v}); } //---------------------------------------------- vector<bool> visited(n+1, 0); visited[1] = 1; queue<int> q; q.push(1); while(!q.empty()){ int u = q.front(); q.pop(); for(auto v: graph[u]){ if(visited[v]) continue; if(!edges.count({1, v})){ answer.push_back({1, {1, v}}); graph[1].insert(v); graph[v].insert(1); if(!toadd.count({1, v})) todel.insert({1, v}); else toadd.erase({1, v}); } visited[v] = 1; q.push(v); } } for(auto k: toadd) {answer.push_back({1, {k.first, k.second}}); graph[k.first].insert(k.second); graph[k.second].insert(k.first);}; set<pair<int, int>> todel2 = todel; for(auto k: todel) if(k.first != 1){answer.push_back({0, {k.first, k.second}}); graph[k.first].erase(k.second); graph[k.second].erase(k.first); todel2.erase(k);} visited.assign(n+1, 0); q.push(1); vector<int> order; while(!q.empty()){ int u = q.front(); q.pop(); for(auto v: graph[u]){ if(visited[v]) continue; if(todel2.count({u, v})) continue; visited[v] = 1; q.push(v); } order.push_back(u); } for(int i=(int)(order.size())-1; i>0; i--) if(todel.count({1, order[i]})) answer.push_back({0, {1, order[i]}}); //------------------- cout<<answer.size()<<'\n'; for(auto &k: answer){ if(k.first) cout<<"+ "; else cout<<"- "; cout<<k.second.first<<' '<<k.second.second<<'\n'; } return 0; } |