【CTSC2008】【BZOJ】【P1143】【祭祀river】【题解】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1143

Floyd传递闭包,然后求最大独立集,Orz wangxz!!!

首先我觉得这个做法的正确性显然

但由于讲不清楚被D了所以得说一下为什么floyd后如果u->v有边那么在二分图中只有一条左u到右v的边而没有左v到右u的边

【CTSC2008】【BZOJ】【P1143】【祭祀river】【题解】_第1张图片

Code:

/*//Orz wangxz!!!!!!
	ID:zky//Orz wangxz!!!!!!
	OJ:BZOJ//Orz wangxz!!!!!!
	Index:1143//Orz wangxz!!!!!!
	Language:C++//Orz wangxz!!!!!!
*///Orz wangxz!!!!!!
#include<cstdio>//Orz wangxz!!!!!!
#include<cstring>//Orz wangxz!!!!!!
#include<iostream>//Orz wangxz!!!!!!
#include<algorithm>//Orz wangxz!!!!!!
using namespace std;//Orz wangxz!!!!!!
int n,m;//Orz wangxz!!!!!!
int vis[101];//Orz wangxz!!!!!!
int Link[101];//Orz wangxz!!!!!!
int map[101][101];//Orz wangxz!!!!!!
bool find(int x){//Orz wangxz!!!!!!
	for(int i=1;i<=n;i++){//Orz wangxz!!!!!!
		if(!vis[i]&&map[x][i]){//Orz wangxz!!!!!!
			vis[i]=1;//Orz wangxz!!!!!!
			if(!Link[i]||find(Link[i])){//Orz wangxz!!!!!!
				Link[i]=x;//Orz wangxz!!!!!!
				return true;//Orz wangxz!!!!!!
			}//Orz wangxz!!!!!!
		}//Orz wangxz!!!!!!
	}//Orz wangxz!!!!!!
	return false;//Orz wangxz!!!!!!
}//Orz wangxz!!!!!!
int main(){//Orz wangxz!!!!!!
	cin>>n>>m;//Orz wangxz!!!!!!
	memset(map,0,sizeof(map));//Orz wangxz!!!!!!
	for(int i=1;i<=m;i++){//Orz wangxz!!!!!!
		int u,v;//Orz wangxz!!!!!!
		cin>>u>>v;//Orz wangxz!!!!!!
		map[u][v]=1;//Orz wangxz!!!!!!
//		map[v][u]=1;//Orz wangxz!!!!!!
	}//Orz wangxz!!!!!!
	for(int k=1;k<=n;k++)//Orz wangxz!!!!!!
	for(int i=1;i<=n;i++)//Orz wangxz!!!!!!
	for(int j=1;j<=n;j++)//Orz wangxz!!!!!!
	map[i][j]=map[i][j]||(map[i][k]&&map[k][j]);//Orz wangxz!!!!!!
	//Orz wangxz!!!!!!
//	for(int i=1;i<=n;i++)
//	for(int j=1;j<=n;j++)
//	if(map[i][j])map[i][j]=0;else map[i][j]=1;
	//Orz wangxz!!!!!!
	int ans=0;//Orz wangxz!!!!!!
	for(int i=1;i<=n;i++){//Orz wangxz!!!!!!
		memset(vis,0,sizeof(vis));//Orz wangxz!!!!!!
		if(find(i))ans++;//Orz wangxz!!!!!!
	}//Orz wangxz!!!!!!
	cout<<n-ans<<endl;//Orz wangxz!!!!!!
	return 0;//Orz wangxz!!!!!!
}//Orz wangxz!!!!!!


你可能感兴趣的:(bzoj)