The King’s Problem HDU - 3861(tarjan缩点+最小路径覆盖)

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
#include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 5005;
int LOW[N],DFN[N],index;
int vis[N],include[N],cnt;
stack<int>p;
vector<int>G[N],mp[N];
int n,m;
void init(){
	index=cnt=0;
	for(int i=0;i<=n;i++){
		G[i].clear();
		mp[i].clear();
	}
	while(!p.empty()) p.pop();
	memset(include,0,sizeof(include));
	memset(vis,0,sizeof(vis));
	memset(DFN,0,sizeof(DFN));
	memset(LOW,0,sizeof(LOW));
}

void tarjan(int x){
	LOW[x]=DFN[x]=++index;
	vis[x]=1;
	p.push(x);
	for(int i=0;i<G[x].size();i++){
		int v=G[x][i];
		if(!DFN[v]){
			tarjan(v);
			LOW[x]=min(LOW[v],LOW[x]);
		}
		else if(vis[v]) LOW[x]=min(LOW[x],DFN[v]);
	}
	
	if(LOW[x]==DFN[x]){
		int v;
		cnt++;
		do{
			v=p.top();
			p.pop();
			include[v]=cnt;
			vis[v]=0;
		}while(v!=x);
	}
}
int match[N],visit[N];
int Find(int u){
	for(int i=0;i<mp[u].size();i++){
		int v=mp[u][i];
		if(!visit[v]){
			visit[v]=1;
			if(match[v]==-1||Find(match[v])){
				match[v]=u;
				return 1;
			}
		}
	}
	return 0;
}
void Maxmatch(){
	memset(match,-1,sizeof(match));
	memset(visit,0,sizeof(visit));
	int ans=0;
	for(int i=1;i<=cnt;i++){
		memset(visit,0,sizeof(visit));
		ans+=Find(i);
	}
	printf("%d\n",cnt-ans);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&m);
		init();
		for(int i=0;i<m;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			G[x].push_back(y);
		}
		
		for(int i=1;i<=n;i++)
		   if(!DFN[i]) tarjan(i);
		   
		for(int i=1;i<=n;i++){
			for(int j=0;j<G[i].size();j++){
				int v=G[i][j];
				int x=include[i],y=include[v];
				if(x!=y) mp[x].push_back(y);
			}
		}
		
		Maxmatch();
	    
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/80356954