网络流(最大独立点集):POJ 1466 Girls and Boys

时间:2023-03-09 20:16:53
网络流(最大独立点集):POJ 1466 Girls and Boys

Girls and Boys

Time Limit: 5000ms
Memory Limit: 10000KB

This problem will be judged on PKU. Original ID: 1466
64-bit integer IO format: %lld      Java class name: Main

In the second year of the university somebody started a study on the romantic(浪漫的) relations between the students. The relation "romantically(浪漫地) involved(包含)" is defined(定义) between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been "romantically involved". The result of the program is the number of students in such a set.

Input

The input(投入) contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

the number of students

the description of each student, in the following format

student_identifier(标识符):(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...

or

student_identifier:(0)

The student_identifier is an integer(整数) number between 0 and n-1 (n <=500 ), for n subjects.

Output

For each given data set, the program should write to standard output(输出) a line containing the result.

Sample Input

7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0

Sample Output

5
2 

题目大意:有n个人每个人又和别的人有关系,求的是没有关系的最大人数。

输入:

第一行 n个人,接下来n行 0--n-1:(与此人有关系的人的个数 )  有关系的人。

求的是二分图的最大独立集,此题不用划分集合,所以最后的最大匹配数要除以2。

二分图最大独立集 = N - 最大匹配数。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std;
const int INF=;
const int maxn=,maxm=;
int cnt,fir[maxn],nxt[maxm],cap[maxm],to[maxm],dis[maxn],gap[maxn],path[maxn]; void addedge(int a,int b,int c)
{
nxt[++cnt]=fir[a];
to[cnt]=b;
cap[cnt]=c;
fir[a]=cnt;
} bool BFS(int S,int T)
{
memset(dis,,sizeof(dis));
dis[T]=;
queue<int>q;q.push(T);
while(!q.empty())
{
int node=q.front();q.pop();
for(int i=fir[node];i;i=nxt[i])
{
if(dis[to[i]])continue;
dis[to[i]]=dis[node]+;
q.push(to[i]);
}
}
return dis[S];
}
int fron[maxn];
int ISAP(int S,int T)
{
if(!BFS(S,T))
return ;
for(int i=;i<=T;i++)++gap[dis[i]];
int p=S,ret=;
memcpy(fron,fir,sizeof(fir));
while(dis[S]<=T)
{
if(p==T){
int f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}
p=T;ret+=f;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
}
int &ii=fron[p];
for(;ii;ii=nxt[ii]){
if(!cap[ii]||dis[to[ii]]+!=dis[p])
continue;
else
break;
}
if(ii){
p=to[ii];
path[p]=ii;
}
else{
if(--gap[dis[p]]==)break;
int minn=T+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])
minn=min(minn,dis[to[i]]);
gap[dis[p]=minn+]++;
fron[p]=fir[p];
if(p!=S)
p=to[path[p]^];
}
}
return ret;
} void Init()
{
memset(fir,,sizeof(fir));
cnt=;
}
int main()
{
int n,k,to;
while(~scanf("%d",&n))
{
Init();
for(int i=;i<=n;i++)addedge(,i,),addedge(i,,),addedge(i+n,*n+,),addedge(*n+,i+n,);
for(int i=;i<n;i++){
scanf("%d: (%d)",&i,&k);
while(k--){
scanf("%d",&to);
addedge(i+,to+n+,);
addedge(to+n+,i+,);
}
}
printf("%d\n",ISAP(,*n+));
}
return ;
}

  当然,匈牙利算法是更合适的算法~~~

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std;
const int INF=;
const int maxn=,maxm=; int cnt,fir[maxn],nxt[maxm],to[maxm],match[maxn],vis[maxn]; void addedge(int a,int b)
{
nxt[++cnt]=fir[a];
to[cnt]=b;
fir[a]=cnt;
} int Hungary_algorithm(int node)
{
vis[node]=true;
for(int i=fir[node];i;i=nxt[i]){
if(!match[to[i]]){
match[to[i]]=node;
return ;
}
if(!vis[match[to[i]]]&&Hungary_algorithm(match[to[i]])){
match[to[i]]=node;
return ;
}
}
return ;
} void Init()
{
memset(fir,,sizeof(fir));
memset(match,,sizeof(match));
cnt=;
} int main()
{
int n,k,to,ans;
while(~scanf("%d",&n))
{
Init();ans=;
for(int i=;i<n;i++){
scanf("%d: (%d)",&i,&k);
while(k--){
scanf("%d",&to);
addedge(i+,to+);
}
}
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
ans+=Hungary_algorithm(i);
}
printf("%d\n",n-ans/);
}
return ;
}