hdoj 3861 The King’s Problem 【有向图tarjan求SCC + 缩点 + 最小路径覆盖】

The King’s Problem

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


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
 

题意:国王有一个n个城市,m条单向边的国土。现在他为了治理国家更加有效率,要把国土划分几个州。但是要求是: 若两点v,u有路径(v, u),(u, v)那么这些路径必须要在同一个州中。并且他还要保证建立了州以后,一个区域内的任意两点至少要有单向路径。问他至少要建多少州。


按着题意来,首先求SCC(保证两点间的路径在同一个州里面),然后缩点后构成新图,接下来 就是 求最小路径覆盖 



AC代码:


#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 50000+10
#define MAXM 200000+10
#define INF 10000000
using namespace std;
struct Edge
{
	int from, to, next;
}edge[MAXM];
int head[MAXN], edgenum;
int low[MAXN], dfn[MAXN];
int dfs_clock;
int sccno[MAXN], scc_cnt; 
stack<int> S;
bool Instack[MAXN];
int in[MAXN], out[MAXN];//记录SCC出度和入度 
vector<int> G[MAXN];//缩点后新图 
vector<int> scc[MAXN];//存储SCC的点 
int n, m;
int pipei[MAXN];//二分图匹配 用到 
int used[MAXN];
void init()
{
	edgenum = 0;
	memset(head, -1, sizeof(head));
} 
void addEdge(int u, int v)
{
	Edge E = {u, v, head[u]};
	edge[edgenum] = E;
	head[u] = edgenum++;
} 
void getMap()
{
	int a, b;
	while(m--)
	{
		scanf("%d%d", &a, &b);
		addEdge(a, b); 
	}
}
void tarjan(int u, int fa)
{
	int v;
	low[u] = dfn[u] = ++dfs_clock;
	S.push(u);
	Instack[u] = true;
	for(int i = head[u]; i != -1; i = edge[i].next)
	{
		v = edge[i].to;
		if(!dfn[v])
		{
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
		}
		else if(Instack[v])
		low[u] = min(low[u], dfn[v]);
	} 
	if(low[u] == dfn[u])
	{
		scc_cnt++;
		scc[scc_cnt].clear();
		for(;;)
		{
			v = S.top(); S.pop();
			scc[scc_cnt].push_back(v);
			Instack[v] = false;
			sccno[v] = scc_cnt; 
			if(v == u) break;
		}
	}
}
void find_cut(int l, int r)
{
	memset(low, 0, sizeof(low));
	memset(dfn, 0, sizeof(dfn));
	memset(sccno, 0, sizeof(sccno));
	memset(Instack, false, sizeof(Instack));
	dfs_clock = scc_cnt = 0;
	for(int i = l; i <= r; i++)
	if(!dfn[i]) tarjan(i, -1); 
}
void suodian()
{
	for(int i = 1; i <= scc_cnt; i++) G[i].clear(), in[i] = out[i] = 0;
	for(int i = 0; i < edgenum; i++)
	{
		int u = sccno[edge[i].from];
		int v = sccno[edge[i].to];
		if(u != v)
		G[u].push_back(v), in[v]++, out[u]++;
	}
}
int find(int x)//匹配 
{
	for(int i = 0; i < G[x].size(); i++)
	{
		int y = G[x][i];
		if(used[y] == 0)
		{
			used[y] = 1;
			if(pipei[y] == -1 || find(pipei[y]))
			{
				pipei[y] = x;
				return 1;
			}
		}
	} 
	return 0;
}
void solve()
{
	int ans = 0;
	memset(pipei, -1, sizeof(pipei));
	for(int i = 1; i <= scc_cnt; i++)
	{
		memset(used, 0, sizeof(used));
		ans += find(i);
	}
	printf("%d\n", scc_cnt - ans);//最小路径覆盖等于 节点数减去最大匹配数 
}
int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d%d", &n, &m);
		init();
		getMap();
		find_cut(1, n);//求所有SCC 
		suodian();//缩点 
		solve();//求最小路径覆盖  
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值