Cat VS Dog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 2220 Accepted Submission(s): 767
Total Submission(s): 2220 Accepted Submission(s): 767
Problem Description
The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the child's like-animal is a cat, then his/hers dislike-animal must be a dog, and vice versa.
Now the zoo administrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. So the administrator wants to know which animals he should remove to make maximum number of happy children.
Now the zoo administrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. So the administrator wants to know which animals he should remove to make maximum number of happy children.
Input
The input file contains multiple test cases, for each case, the first line contains three integers N <= 100, M <= 100 and P <= 500.
Next P lines, each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details)
Next P lines, each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details)
Output
For each case, output a single integer: the maximum number of happy children.
Sample Input
1 1 2 C1 D1 D1 C1 1 2 4 C1 D1 C1 D1 C1 D2 D2 C1
Sample Output
1 3HintCase 2: Remove D1 and D2, that makes child 1, 2, 3 happy.
Source
Recommend
这题真是我想了很多办法,都不对,一开始想是最大流,结果建好图后不行就放弃了,其实是可以的
最大独立集= 顶点数-最大匹配数/2(无向图,有向图就不用/2 了) 把这个给忘了
也解决了长期困扰我的要有x就不能有Y的问题。
最大独立集= 顶点数-最大匹配数/2(无向图,有向图就不用/2 了) 把这个给忘了
也解决了长期困扰我的要有x就不能有Y的问题。
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#define N 500010
#define INF 0x7fffff
using namespace std;
struct num
{
int e,next,pos,val;
}a[2*N];
int b[N],c[N][2],tag[N],level[N],Top,end,sta;
int n,m,p;
char cr[N][2];
map<string,int>q;
char s1[20],s2[20];
int main()
{
//freopen("data.in","r",stdin);
void addeage(int x,int y,int pos,int val);
int Dinic();
while(scanf("%d %d %d",&n,&m,&p)!=EOF)
{
q.clear();
int ba=1;
for(int i=1;i<=p;i++)
{
scanf("%s %s",s1,s2);
if(q[s1]==0)
{
q[s1]=ba++;
}
if(q[s2]==0)
{
q[s2] = ba++;
}
c[i][0] = q[s1];
c[i][1] = q[s2];
cr[i][0] = s1[0];
cr[i][1] = s2[0];
}
Top=0;
memset(b,-1,sizeof(b));
sta = 0;
int sum1=0;
for(int i=1;i<=p;i++)
{
addeage(0,i,Top+1,1);
addeage(i,0,Top-1,0);
}
for(int i=1;i<=p;i++)
{
for(int j=i+1;j<=p;j++)
{
if(c[i][1]==c[j][0]||c[i][0]==c[j][1])
{
addeage(i,j+p,Top+1,1);
addeage(j+p,i,Top-1,0);
addeage(j,i+p,Top+1,1);
addeage(i+p,j,Top-1,0);
}
}
}
end = 2*p+1;
for(int i=1;i<=p;i++)
{
addeage(i+p,end,Top+1,1);
addeage(end,i+p,Top-1,0);
}
int ans = Dinic();
printf("%d\n",p-ans/2);
}
return 0;
}
void addeage(int x,int y,int pos,int val)
{
a[Top].e = y;
a[Top].next = b[x];
a[Top].pos = pos;
a[Top].val = val;
b[x] = Top;
Top++;
}
int bfs()
{
memset(level,0,sizeof(level));
queue<int> que;
que.push(0);
level[0] = 1;
while(!que.empty())
{
int x = que.front();
que.pop();
for(int i=b[x];i!=-1;i=a[i].next)
{
int y = a[i].e;
if(!level[y]&&a[i].val>0)
{
level[y] = level[x]+1;
que.push(y);
}
}
}
return level[end];
}
int dfs(int x,int val)
{
if(x==end)
{
return val;
}
for(int i=tag[x];i!=-1;i=a[i].next)
{
int y = a[i].e;
if(level[y]==level[x]+1&&a[i].val>0)
{
int k = dfs(y,min(a[i].val,val));
if(k)
{
tag[x] = a[i].next;
a[i].val -=k;
int pos = a[i].pos;
a[pos].val+=k;
return k;
}
}
}
return 0;
}
int Dinic()
{
int ans=0;
while(bfs())
{
for(int i=0;i<=end;i++)
{
tag[i] = b[i];
}
while(1)
{
int k = dfs(sta,INF);
if(k==0)
{
break;
}
ans+=k;
}
}
return ans;
}