向前走莫回头❤

【hdu 3861】The King’s Problem(Tarjan缩点+匈牙利算法)

The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2644    Accepted Submission(s): 960


Problem Description
In the Kingdom of Silence, the king has a new problem. There are N cities in the kingdom and there are M directional roads between the cities. That means that if there is a road from u to v, you can only go from city u to city v, but can’t go from city v to city u. In order to rule his kingdom more effectively, the king want to divide his kingdom into several states, and each city must belong to exactly one state.What’s more, for each pair of city (u, v), if there is one way to go from u to v and go from v to u, (u, v) have to belong to a same state.And the king must insure that in each state we can ether go from u to v or go from v to u between every pair of cities (u, v) without passing any city which belongs to other state.
  Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.
 

Input
The first line contains a single integer T, the number of test cases. And then followed T cases.

The first line for each case contains two integers n, m(0 < n <= 5000,0 <= m <= 100000), the number of cities and roads in the kingdom. The next m lines each contains two integers u and v (1 <= u, v <= n), indicating that there is a road going from city u to city v.
 

Output
The output should contain T lines. For each test case you should just output an integer which is the least number of states the king have to divide into.
 

Sample Input
1 3 2 1 2 1 3
 

Sample Output
2
 

Source
 
[题意][求把点至少能分为几个区域,要求:1)能互相到达的点必须在一个区域 2)在一个区域里的点至少都有边相连]
【题解】【Tarjan缩点+匈牙利算法】
【因为要求最少的区域,先缩点,重新建图,然后跑二分图最大匹配,然后用缩点后的图中的点数减去最大匹配即为答案】
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
	int start,end;
}ask[100010];
int T,n,m;
int a[100010],nxt[100010],p[50010],tot;
int v[100010],to[100010],point[50010],tt;
int dft[50010],dis[50010],f[50010],belong[50010];
int que[100010],top,root,cnt,ans;
bool vis[50010];
inline void add(int x,int y)
{
	tot++; a[tot]=y; nxt[tot]=p[x]; p[x]=tot;
}
inline void Add(int x,int y)
{
	tt++; v[tt]=y; to[tt]=point[x]; point[x]=tt;
}
void tarjan(int x)
{
	dft[x]=dis[x]=++cnt; vis[x]=1;
	que[++top]=x;
	for(int i=p[x];i!=-1;i=nxt[i])
	 if(!dft[a[i]])
	  {
	  	tarjan(a[i]);
	  	dis[x]=min(dis[x],dis[a[i]]);
	  }
	 else
	  if(vis[a[i]]&&dis[x]>dft[a[i]]) dis[x]=dft[a[i]];
	int b;
	if(dft[x]==dis[x])
	 {
	 	root++;
	 	do{
	 		b=que[top--];
	 		vis[b]=0;
	 		belong[b]=root;
		 }while(b!=x);
	 }
}
bool dfs(int x)
{
	for(int i=point[x];i!=-1;i=to[i])
	 if(!vis[v[i]])
      {
      	vis[v[i]]=1;
      	if(f[v[i]]==-1||dfs(f[v[i]]))
      	 {
      	 	f[v[i]]=1;
      	 	return 1;
		   }
	  }
	return 0;
}
int main()
{
 //   freopen("int.txt","r",stdin);
  //  freopen("my.txt","w",stdout);
	int i;
	scanf("%d",&T);
	while(T--)
	 {
	 	tot=root=top=cnt=tt=ans=0;
	 	memset(p,-1,sizeof(p));
	 	memset(f,-1,sizeof(f));
	 	memset(to,-1,sizeof(to));
	 	memset(vis,0,sizeof(vis));
	 	memset(dft,0,sizeof(dft));
	 	memset(dis,0,sizeof(dis));
	 	memset(point,-1,sizeof(point));
	 	memset(belong,0,sizeof(belong));
	 	scanf("%d%d",&n,&m);
	 	for(i=1;i<=m;++i)
	 	 {
	 	 	int x,y;
	 	 	scanf("%d%d",&x,&y);
	 	 	add(x,y);
	 	 	ask[i].start=x; ask[i].end=y;
		  }
		for(i=1;i<=n;++i)
		 if(!dft[i]) tarjan(i);
		for(i=1;i<=m;++i)
		 {
		 	int x=ask[i].start,y=ask[i].end;
		 	x=belong[x]; y=belong[y];
		 	if(x!=y) Add(x,y);
		 }
		for(i=1;i<=root;++i)
		 {
		 	memset(vis,0,sizeof(vis));
		 	if(dfs(i)) ans++;
		 }
		printf("%d\n",root-ans);
	 }
	return 0;
}



posted @ 2016-11-10 00:42  lris0-0  阅读(93)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m