#include <bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define pb push_back set<pair<int, int>> sa; set<pair<int, int>> wiz; vector<int> g[30009]; vector<int> g2[30009]; bool vis[30009]; vector<pair<int, pair<int, int>>> ww; void dfs(int v){ vis[v] = true; if(v != 1 && sa.find({v, 1}) == sa.end()){ sa.insert({v, 1}); sa.insert({1, v}); //cout<<"+ 1 "<<v<<endl; ww.push_back({1, {1, v}}); } for(int x: g[v]){ if(!vis[x]) dfs(x); } } void dfs2(int v){ vis[v] = true; for(int x: g2[v]){ if(!vis[x]) dfs2(x); } if(v != 1 && wiz.find({1, v}) == wiz.end()){ //cout<<"- "<<1<<" "<<v<<endl; ww.push_back({2, {1, v}}); } } int main(){ int n, m; cin>>n>>m; for(int i = 1; i <= m; i++){ int a, b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); sa.insert({a, b}); sa.insert({b, a}); } dfs(1); int q; cin>>q; for(int i = 1; i <= q; i++){ int a, b; cin>>a>>b; wiz.insert({a, b}); wiz.insert({b, a}); g2[a].push_back(b); g2[b].push_back(a); if(sa.find({a, b}) == sa.end()){ //cout<<"+ "<<a<<" "<<b<<endl; ww.push_back({1, {a, b}}); sa.insert({a, b}); sa.insert({b, a}); } } //cout<<"!"<<endl; set<pair<int, int>> dusu; for(pair<int, int> x: sa){ int a = x.fi, b = x.se; if(a == 1 || b == 1) continue; if(wiz.find({a, b}) == wiz.end()){ //cout<<"- "<<a<<" "<<b<<endl; if(dusu.find({a, b}) != dusu.end() || dusu.find({b, a}) != dusu.end()) continue; ww.push_back({2, {a, b}}); dusu.insert({a, b}); dusu.insert({b, a}); } } for(pair<int, int> x: dusu){ sa.erase(x); } //cout<<"@"<<endl; for(int i = 1; i <= n; i++) vis[i] = false; dfs2(1); //cout<<"#"<<endl; cout<<ww.size()<<endl; for(pair<int, pair<int, int>> x: ww){ if(x.fi == 1) cout<<"+ "; else cout<<"- "; cout<<x.se.fi<<" "<<x.se.se<<endl; } } /* 4 6 4 2 2 1 3 1 3 2 3 4 1 4 3 3 2 2 1 4 3 */
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define pb push_back set<pair<int, int>> sa; set<pair<int, int>> wiz; vector<int> g[30009]; vector<int> g2[30009]; bool vis[30009]; vector<pair<int, pair<int, int>>> ww; void dfs(int v){ vis[v] = true; if(v != 1 && sa.find({v, 1}) == sa.end()){ sa.insert({v, 1}); sa.insert({1, v}); //cout<<"+ 1 "<<v<<endl; ww.push_back({1, {1, v}}); } for(int x: g[v]){ if(!vis[x]) dfs(x); } } void dfs2(int v){ vis[v] = true; for(int x: g2[v]){ if(!vis[x]) dfs2(x); } if(v != 1 && wiz.find({1, v}) == wiz.end()){ //cout<<"- "<<1<<" "<<v<<endl; ww.push_back({2, {1, v}}); } } int main(){ int n, m; cin>>n>>m; for(int i = 1; i <= m; i++){ int a, b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); sa.insert({a, b}); sa.insert({b, a}); } dfs(1); int q; cin>>q; for(int i = 1; i <= q; i++){ int a, b; cin>>a>>b; wiz.insert({a, b}); wiz.insert({b, a}); g2[a].push_back(b); g2[b].push_back(a); if(sa.find({a, b}) == sa.end()){ //cout<<"+ "<<a<<" "<<b<<endl; ww.push_back({1, {a, b}}); sa.insert({a, b}); sa.insert({b, a}); } } //cout<<"!"<<endl; set<pair<int, int>> dusu; for(pair<int, int> x: sa){ int a = x.fi, b = x.se; if(a == 1 || b == 1) continue; if(wiz.find({a, b}) == wiz.end()){ //cout<<"- "<<a<<" "<<b<<endl; if(dusu.find({a, b}) != dusu.end() || dusu.find({b, a}) != dusu.end()) continue; ww.push_back({2, {a, b}}); dusu.insert({a, b}); dusu.insert({b, a}); } } for(pair<int, int> x: dusu){ sa.erase(x); } //cout<<"@"<<endl; for(int i = 1; i <= n; i++) vis[i] = false; dfs2(1); //cout<<"#"<<endl; cout<<ww.size()<<endl; for(pair<int, pair<int, int>> x: ww){ if(x.fi == 1) cout<<"+ "; else cout<<"- "; cout<<x.se.fi<<" "<<x.se.se<<endl; } } /* 4 6 4 2 2 1 3 1 3 2 3 4 1 4 3 3 2 2 1 4 3 */ |