#include<bits/stdc++.h> using namespace std; #define f first #define s second #define PB push_back typedef long long ll; typedef long double ld; #define REP(x,y) for(int x=0;x<(y);x++) #define ROF(x,y) for(int x=(y);x>0;x--) #define FOR(x,z,y) for(int x=(z);x<=(y);x++) #define INT(x) int x;scanf("%d",&x) #define LL(x) ll x;scanf("%lld",&x) #define CZ(x) char x;scanf(" %c",&x) typedef pair<int,int> pii; typedef pair<int,pii> piii; const int MAX_N = 300005; set<pii> support_set; set<pii> target; set<pii> edges; vector<int> adj[MAX_N], adj2[MAX_N]; bool visited[MAX_N]; vector<pii> to_add, to_remove; void build_support(int root, int current){ if(visited[current]) return; visited[current]=true; pii p = {min(current,root),max(current,root)}; support_set.insert(p); if(!edges.contains(p)){ to_add.PB(p); //printf("+ %d %d\n",p.f,p.s); } for(int i:adj[current]){ build_support(root,i); } } void remove_support(int root, int current){ if(!visited[current]) return; visited[current]=false; for(int i:adj2[current]){ remove_support(root,i); } pii p = {min(current,root),max(current,root)}; if(!target.contains(p) && support_set.contains(p)){ //printf("- %d %d\n",p.f,p.s); to_remove.PB(p); support_set.erase(p); } } int main(){ INT(n);INT(m); REP(i,m){ INT(a); INT(b); adj[a].PB(b); adj[b].PB(a); edges.insert({min(a,b),max(a,b)}); } INT(k); REP(i,k){ INT(a); INT(b); adj2[a].PB(b); adj2[b].PB(a); target.insert({min(a,b),max(a,b)}); } int root=1; FOR(i,2,n){ if(adj[i].size()>adj[root].size()){ root=i; } } visited[root]=true; for(int i:adj[root]){ build_support(root,i); } for(pii p:target){ if(!support_set.contains(p) && !edges.contains(p)){ to_add.PB(p); //printf("+ %d %d\n",p.f,p.s); } } for(pii p:edges){ if(!target.contains(p) && !support_set.contains(p)){ to_remove.PB(p); //printf("- %d %d\n",p.f,p.s); } } visited[root]=false; for(int i:adj2[root]){ remove_support(root,i); } printf("%ld\n",to_add.size()+to_remove.size()); for(pii p:to_add){ printf("+ %d %d\n",p.f,p.s); } for(pii p:to_remove){ printf("- %d %d\n",p.f,p.s); } }
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 | #include<bits/stdc++.h> using namespace std; #define f first #define s second #define PB push_back typedef long long ll; typedef long double ld; #define REP(x,y) for(int x=0;x<(y);x++) #define ROF(x,y) for(int x=(y);x>0;x--) #define FOR(x,z,y) for(int x=(z);x<=(y);x++) #define INT(x) int x;scanf("%d",&x) #define LL(x) ll x;scanf("%lld",&x) #define CZ(x) char x;scanf(" %c",&x) typedef pair<int,int> pii; typedef pair<int,pii> piii; const int MAX_N = 300005; set<pii> support_set; set<pii> target; set<pii> edges; vector<int> adj[MAX_N], adj2[MAX_N]; bool visited[MAX_N]; vector<pii> to_add, to_remove; void build_support(int root, int current){ if(visited[current]) return; visited[current]=true; pii p = {min(current,root),max(current,root)}; support_set.insert(p); if(!edges.contains(p)){ to_add.PB(p); //printf("+ %d %d\n",p.f,p.s); } for(int i:adj[current]){ build_support(root,i); } } void remove_support(int root, int current){ if(!visited[current]) return; visited[current]=false; for(int i:adj2[current]){ remove_support(root,i); } pii p = {min(current,root),max(current,root)}; if(!target.contains(p) && support_set.contains(p)){ //printf("- %d %d\n",p.f,p.s); to_remove.PB(p); support_set.erase(p); } } int main(){ INT(n);INT(m); REP(i,m){ INT(a); INT(b); adj[a].PB(b); adj[b].PB(a); edges.insert({min(a,b),max(a,b)}); } INT(k); REP(i,k){ INT(a); INT(b); adj2[a].PB(b); adj2[b].PB(a); target.insert({min(a,b),max(a,b)}); } int root=1; FOR(i,2,n){ if(adj[i].size()>adj[root].size()){ root=i; } } visited[root]=true; for(int i:adj[root]){ build_support(root,i); } for(pii p:target){ if(!support_set.contains(p) && !edges.contains(p)){ to_add.PB(p); //printf("+ %d %d\n",p.f,p.s); } } for(pii p:edges){ if(!target.contains(p) && !support_set.contains(p)){ to_remove.PB(p); //printf("- %d %d\n",p.f,p.s); } } visited[root]=false; for(int i:adj2[root]){ remove_support(root,i); } printf("%ld\n",to_add.size()+to_remove.size()); for(pii p:to_add){ printf("+ %d %d\n",p.f,p.s); } for(pii p:to_remove){ printf("- %d %d\n",p.f,p.s); } } |