HDU - 3861 The King’s Problem(強聯通分量+最小路徑覆蓋)

The King’s Problem

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


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.同一個州中任意兩個城市u,v要滿足,要麼u能到達v,要麼v能到達u 
3.一個城市只能被劃分到一個州

分析:

因爲互通的點必須在一起,所以先求強連通分量,縮點。
爲了滿足2,3條件也很容易,求一下二分圖最大匹配。
然後 點-最大匹配就是答案啦~

AC代碼:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=5050;
vector<int>v[maxn];
vector<int>g[maxn];
stack<int>S;
int dfn[maxn],low[maxn];
int viss[maxn];
int marry[maxn],vis[maxn];
int index,cnt;
void init(int n)
{
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(viss,0,sizeof(viss));
	for(int i=0;i<=n;i++)
	v[i].clear();
	index=0,cnt=0;
} 
void tarjan(int x)
{
	dfn[x]=low[x]=++index;
	S.push(x);
	for(int i=0;i<v[x].size();i++)
	{
		int y=v[x][i];
		if(!dfn[y])
		{
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		else if(!viss[y])
		{
			low[x]=min(low[x],dfn[y]);
		}
	}
	if(dfn[x]==low[x])
	{
		cnt++;
		while(1)
		{
			int y=S.top();S.pop();
			viss[y]=cnt;
			if(y==x)
			break;
		}
	}
}
void solve(int n)
{
	for(int i=1;i<=n;i++)
	{
		if(!dfn[i])
		tarjan(i);
	}
}
bool dfs(int u)
{
	for(int i=0;i<g[u].size();i++)
	{
		int s=g[u][i];
		if(!vis[s])
		{
			vis[s]=1;
			if(!marry[s]||dfs(marry[s]))
			{
				marry[s]=u;
				return true;
			}
		}
	}
	return false;
}
int hungry()
{
	memset(marry,0,sizeof(marry));
	int ans=0;
	for(int i=1;i<=cnt;i++)
	{
		memset(vis,0,sizeof(vis));
		if(dfs(i))
		ans++;
	}
	return ans;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		init(n);
		for(int i=0;i<m;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			v[a].push_back(b);
		}
		solve(n);
		for(int i=0;i<=cnt;i++)
		g[i].clear();
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<v[i].size();j++)
			{
				int y=v[i][j];
				if(viss[i]!=viss[y])
				g[viss[i]].push_back(viss[y]);
			}
		}
		printf("%d\n",cnt-hungry());
	}
}



 

發佈了173 篇原創文章 · 獲贊 12 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章