ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (贪心)

"Oh, There is a bipartite graph.""Make it Fantastic."
X wants to check whether a bipartite graph is a fantastic graph. He has two fantastic numbers, and he wants to let all the degrees to between the two boundaries. You can pick up several edges from the current graph and try to make the degrees of every point to between the two boundaries. If you pick one edge, the degrees of two end points will both increase by one. Can you help X to check whether it is possible to fix the graph?

Input
There are at most 30 test cases.

For each test case,The first line contains three integers N the number of left part graph vertices, M the number of right part graph vertices, and K the number of edges ( 1≤N≤2000,0≤M≤2000,0≤K≤6000). Vertices are numbered from 1 to N.

The second line contains two numbers L,R(0≤L≤R≤300). The two fantastic numbers.

Then K lines follows, each line containing two numbers U, V (1≤U≤N,1≤V≤M). It shows that there is a directed edge from U-th spot to V-th spot.

Note. There may be multiple edges between two vertices.

Output
One line containing a sentence. Begin with the case number. If it is possible to pick some edges to make the graph fantastic, output "Yes" (without quote), else output "No" (without quote).

样例输入
3 3 7
2 3
1 2
2 3
1 3
3 2
3 3
2 1
2 1
3 3 7
3 4
1 2
2 3
1 3
3 2
3 3
2 1
2 1

样例输出
Case 1: Yes
Case 2: No

题意

一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间

题解

比赛的时候写的网络流A的,赛后把自己hack了。。

然后写了个贪心,发现还是贪心好写(雾)

考虑两个集合A和B,A为L<=d[i]<=R,B为d[i]>R

枚举每个边

1.如果u和v都在B集合,直接删掉
2.如果u和v都在A集合,无所谓
3.如果u在B,v在A,并且v可删边即d[v]>L
4.如果u在A,v在B,并且u可删边即d[u]>L

最后枚举N+M个点判断是否在[L,R]之间

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn=6005;
 5 
 6 int main()
 7 {
 8     int N,M,K,L,R,o=1,u[maxn],v[maxn],d[maxn];
 9     while(scanf("%d%d%d",&N,&M,&K)!=EOF)
10     {
11         memset(d,0,sizeof d);
12         scanf("%d%d",&L,&R);
13         int sum=0,flag=1;
14         for(int i=0;i<K;i++)
15         {
16             scanf("%d%d",&u[i],&v[i]);v[i]+=N;
17             d[u[i]]++,d[v[i]]++;
18         }
19         for(int i=0;i<K;i++)
20         {
21             int uu=u[i],vv=v[i];
22             if(d[uu]>R&&d[vv]>R)d[uu]--,d[vv]--;
23             else if(L<=d[uu]&&d[uu]<=R&&L<=d[vv]&&d[vv]<=R)continue;
24             else if(L+1<=d[uu]&&d[uu]<=R&&d[vv]>R)d[uu]--,d[vv]--;
25             else if(d[uu]>R&&L+1<=d[vv]&&d[vv]<=R)d[uu]--,d[vv]--;
26         }
27         for(int i=1;i<=N+M;i++)if(d[i]<L||d[i]>R)flag=0;
28         printf("Case %d: %s\n",o++,flag?"Yes":"No");
29     }
30     return 0;
31 }

给一点测试数据,网上有的贪心过不去这些数据Yes Yes Yes Yes No

4 4 16
1 3
1 1
1 1
2 2
2 2
3 3
3 3
4 4
4 4
1 1
1 1
2 2
2 2
3 3
3 3
4 4
4 4

4 3 6
1 2
1 1
2 1
2 2
3 1
3 1
4 3

4 4 10
1 3
1 2
1 3
2 1
2 1
2 1
3 1
3 2
3 3
4 3
4 4

3 3 7
1 1
1 2
2 3
1 3
3 2
3 3
2 1
2 1

1 3 3
1 1
1 1
1 2
1 3

猜你喜欢

转载自www.cnblogs.com/taozi1115402474/p/9610719.html