#include <bits/stdc++.h> #define MP make_pair using namespace std; vector<int> adj[30002]; vector<int> adjD[30002]; int main(){ ios_base::sync_with_stdio(NULL); cin.tie(NULL); int n, m; cin >> n >> m; vector<pair<int, pair<int, int>>> res; vector<pair<int, int>> edgesF(m); vector<bool> visited(n+1); vector<bool> toRemove(n+1, true); vector<bool> connectedOne(n+1); visited[1]=true; queue<int> toBFS; toBFS.push(1); for(int i=0; i<m; i++){ int a, b; cin >> a >> b; if(a>b) swap(a, b); if(a==1) connectedOne[b]=true; edgesF[i]=MP(a, b); adj[a].push_back(b); adj[b].push_back(a); if(a==1){ toBFS.push(b); visited[b]=true; } } int md; cin >> md; vector<pair<int,int>> edgesD(md); for(int i=0; i<md; i++){ int a, b; cin >> a >> b; if(a>b) swap(a, b); edgesD[i]=MP(a, b); if(a==1) toRemove[b]=false; adjD[a].push_back(b); adjD[b].push_back(a); } while(!toBFS.empty()){ int u=toBFS.front(); toBFS.pop(); visited[u]=true; if(!connectedOne[u] && u!=1){ res.push_back(MP(1, MP(1, u))); connectedOne[u]=true; } for(int v : adj[u]){ if(!visited[v]) toBFS.push(v); } } for(int i=0; i<m; i++){ int a, b; a = edgesF[i].first; b = edgesF[i].second; if(a==1) continue; res.push_back(MP(-1, MP(a, b))); } for(int i=0; i<md; i++){ int a, b; a = edgesD[i].first; b = edgesD[i].second; if(a==1) continue; res.push_back(MP(1, MP(a, b))); } vector<bool> visitedInTS(n+1); priority_queue<pair<int, int>> pq; vector<int> val(n+1); for(int i=2; i<=n; i++){ if(!toRemove[i]) continue; int sum=adjD[i].size(); val[i]=sum; pq.push(MP(-sum, i)); } while(!pq.empty()){ pair<int, int> t=pq.top(); pq.pop(); if(visitedInTS[t.second]) continue; visitedInTS[t.second]=true; res.push_back(MP(-1, MP(1, t.second))); for(int u : adjD[t.second]){ if(toRemove[u] && !visitedInTS[u]){ val[u]--; pq.push(MP(-val[u], u)); } } } cout << res.size() << "\n"; for(int i=0; i<res.size(); i++){ if(res[i].first==1) cout << "+ " << res[i].second.first << " " << res[i].second.second << "\n"; else cout << "- " << res[i].second.first << " " << res[i].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 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 | #include <bits/stdc++.h> #define MP make_pair using namespace std; vector<int> adj[30002]; vector<int> adjD[30002]; int main(){ ios_base::sync_with_stdio(NULL); cin.tie(NULL); int n, m; cin >> n >> m; vector<pair<int, pair<int, int>>> res; vector<pair<int, int>> edgesF(m); vector<bool> visited(n+1); vector<bool> toRemove(n+1, true); vector<bool> connectedOne(n+1); visited[1]=true; queue<int> toBFS; toBFS.push(1); for(int i=0; i<m; i++){ int a, b; cin >> a >> b; if(a>b) swap(a, b); if(a==1) connectedOne[b]=true; edgesF[i]=MP(a, b); adj[a].push_back(b); adj[b].push_back(a); if(a==1){ toBFS.push(b); visited[b]=true; } } int md; cin >> md; vector<pair<int,int>> edgesD(md); for(int i=0; i<md; i++){ int a, b; cin >> a >> b; if(a>b) swap(a, b); edgesD[i]=MP(a, b); if(a==1) toRemove[b]=false; adjD[a].push_back(b); adjD[b].push_back(a); } while(!toBFS.empty()){ int u=toBFS.front(); toBFS.pop(); visited[u]=true; if(!connectedOne[u] && u!=1){ res.push_back(MP(1, MP(1, u))); connectedOne[u]=true; } for(int v : adj[u]){ if(!visited[v]) toBFS.push(v); } } for(int i=0; i<m; i++){ int a, b; a = edgesF[i].first; b = edgesF[i].second; if(a==1) continue; res.push_back(MP(-1, MP(a, b))); } for(int i=0; i<md; i++){ int a, b; a = edgesD[i].first; b = edgesD[i].second; if(a==1) continue; res.push_back(MP(1, MP(a, b))); } vector<bool> visitedInTS(n+1); priority_queue<pair<int, int>> pq; vector<int> val(n+1); for(int i=2; i<=n; i++){ if(!toRemove[i]) continue; int sum=adjD[i].size(); val[i]=sum; pq.push(MP(-sum, i)); } while(!pq.empty()){ pair<int, int> t=pq.top(); pq.pop(); if(visitedInTS[t.second]) continue; visitedInTS[t.second]=true; res.push_back(MP(-1, MP(1, t.second))); for(int u : adjD[t.second]){ if(toRemove[u] && !visitedInTS[u]){ val[u]--; pq.push(MP(-val[u], u)); } } } cout << res.size() << "\n"; for(int i=0; i<res.size(); i++){ if(res[i].first==1) cout << "+ " << res[i].second.first << " " << res[i].second.second << "\n"; else cout << "- " << res[i].second.first << " " << res[i].second.second << "\n"; } return 0; } |