codeforces 723F : st-Spanning Tree
Posted ljh2000
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces 723F : st-Spanning Tree相关的知识,希望对你有一定的参考价值。
Description
There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads.
The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it.
The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input.
Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland.
The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It‘s guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities.
It is guaranteed that the total number of cities in all testset of input data doesn‘t exceed 200.
Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one.
For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it.
In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once.
2
5 5
2 1
4 5
2 3
1 3
3 5
7 2
3 7
4 2
3
1 3
3 5
5 4
3 2
2 1
3
2 4
3 7
正解:构造+贪心
解题报告:
这道题比赛的时候%王队的代码,结果王队写萎了一个地方,我不仅写萎了同一个地方,还自己弄出了一个新错误,直接FST。考虑我们希望使得s和t的度数尽可能小。那么显然,我们需要把不含s和t的边能连上的就连上,那么我们可以得到S、T和若干连通块。并且这些连通块之间没有边。我们考虑我们优先选择S、T和连通块相连,能连则连。
其次我们考虑如果直接连接S和T,事实上是不划算的,因为如果把S和T分别和一个未被连入整体的连通块的话,同样减少一点度数,可以得到更优秀的答案(多连入了一个连通块)。所以我们接着考虑连接某个连通块,他与S、T都相连。需要注意:找到一个这样的连通块之后,我们只需要之后的对于这种连通块我们都只需要连上S或者T就可以了,显然选择度数比较多的那个会更优。最后我们再考虑S、T直接相连的情况。
有很多细节,注意一下。
1 //It is made by jump~ 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 using namespace std; 14 typedef long long LL; 15 const int inf = (1<<30); 16 const int MAXN = 200011; 17 const int MAXM = 400011; 18 int n,m,ecnt,s,t,ds,dt,root; 19 int ans[MAXM],cnt,father[MAXN]; 20 bool use[MAXM]; 21 int ok[MAXM][2],jilu[MAXN][2]; 22 struct edge{ 23 int x,y; 24 }e[MAXM]; 25 inline int find(int x){ if(father[x]!=x) father[x]=find(father[x]); return father[x]; } 26 inline void rr(){ printf("No"); exit(0); } 27 inline int getint() 28 { 29 int w=0,q=0; char c=getchar(); 30 while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); if(c==‘-‘) q=1,c=getchar(); 31 while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar(); return q ? -w : w; 32 } 33 34 inline void work(){ 35 n=getint(); m=getint(); for(int i=1;i<=m;i++) e[i].x=getint(),e[i].y=getint(); 36 s=getint(); t=getint(); ds=getint(); dt=getint(); int r1,r2; for(int i=1;i<=n;i++) father[i]=i; 37 for(int i=1;i<=m;i++) { 38 if(e[i].x==s || e[i].y==s) continue; if(e[i].x==t || e[i].y==t) continue; 39 r1=find(e[i].x); r2=find(e[i].y); 40 if(r1!=r2) { father[r1]=r2; ans[++cnt]=i; use[i]=1; } 41 } 42 bool flag=false; 43 for(int i=1;i<=m;i++) { 44 if(use[i]) continue; if((e[i].x==s && e[i].y==t) || (e[i].x==t && e[i].y==s) ) { flag=true; root=i; continue; } 45 if(e[i].x==s) ok[find(e[i].y)][0]=1,jilu[find(e[i].y)][0]=i; 46 if(e[i].y==s) ok[find(e[i].x)][0]=1,jilu[find(e[i].x)][0]=i; 47 if(e[i].x==t) ok[find(e[i].y)][1]=1,jilu[find(e[i].y)][1]=i; 48 if(e[i].y==t) ok[find(e[i].x)][1]=1,jilu[find(e[i].x)][1]=i; 49 } 50 for(int i=1;i<=n;i++) { 51 if(find(i)!=i) continue; if(i==s || i==t) continue; 52 if(ok[i][0]+ok[i][1]==0) rr(); 53 else if(ok[i][0]+ok[i][1]==1) { 54 if(ok[i][0]) ds--,ans[++cnt]=jilu[i][0],use[jilu[i][0]]=1,r1=find(s),r2=find(i),father[r2]=r1; 55 else if(ok[i][1]) dt--,ans[++cnt]=jilu[i][1],use[jilu[i][1]]=1,r1=find(t),r2=find(i),father[r2]=r1; 56 } 57 } 58 if(ds<0 || dt<0) rr(); 59 for(int i=1;i<=n;i++) { 60 if(find(i)!=i) continue; if(ok[i][0]+ok[i][1]<=1) continue; 61 ds--; dt--; r1=find(s); r2=find(i); father[r2]=r1; r1=find(i); r2=find(t); father[r1]=r2; 62 ans[++cnt]=jilu[i][0]; ans[++cnt]=jilu[i][1]; 63 use[jilu[i][0]]=use[jilu[i][1]]=1; 64 break; 65 } 66 if(ds<0 || dt<0) rr(); 67 for(int i=1;i<=n;i++) { 68 if(find(i)!=i) continue; if(ok[i][0]+ok[i][1]<=1) continue; 69 if(find(i)==find(s)) continue; 70 if(ds>dt) ds--,ans[++cnt]=jilu[i][0]; 71 else dt--,ans[++cnt]=jilu[i][1]; 72 } 73 if(ds<0 || dt<0) rr(); 74 if(find(s)!=find(t)) { 75 if(flag) { ds--,dt--,ans[++cnt]=root; } 76 else rr(); 77 } 78 if(ds<0 || dt<0) rr(); 79 printf("Yes\n"); 80 for(int i=1;i<=cnt;i++) printf("%d %d\n",e[ans[i]].x,e[ans[i]].y); 81 } 82 83 int main() 84 { 85 work(); 86 return 0; 87 }
以上是关于codeforces 723F : st-Spanning Tree的主要内容,如果未能解决你的问题,请参考以下文章