The King’s Problem(tarjan求强连通分量缩点+匈牙利求有向无环图的最小路径覆盖)

171 篇文章 0 订阅
63 篇文章 0 订阅


Link:http://acm.hdu.edu.cn/showproblem.php?pid=3861

The King’s Problem

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


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、强连通分量的所有点必须在同一个区域(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.

2、一个点只能对应一个区域(each city must belong to exactly one state)。 

3、一个区域内的两点至少要一方能通过该区域的点到达另一方,而不经过其他区域的点。(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.)。


编程思想:由规则1可知必然要对强连通分量进行缩点,缩点后变成了一个弱连通图(有向无环图)。根据规则2、3可知即是要求图的最小路径覆盖。因此利用tarjan求强连通分量缩点(有缩点构成的有向无环图G)+匈牙利求有向无环图G的最小路径覆盖。


下面给出二分图相关定义和性质:

定义:

最小路径覆盖:在图中找一些路径(路径数最少),使之覆盖了图中所有的顶点,且每个顶点有且仅和一条路径有关联。
最小顶点覆盖:在图中找一些点(顶点数最少),使之覆盖了图中所有的边,每条边至少和一个顶点有关联。


性质公式:   

最小点覆盖数=最大匹配数

最小路径覆盖=最大独立子集=节点数-最大匹配数=节点数-最小点覆盖数



AC code:

#include <iostream>
#include <cmath>
#include<stdlib.h> 
#include<vector>
#include<cstring>
#include<stdio.h>
#include<algorithm>
#define LL long long
using namespace std;
#define INF 0x7fffffff
#define N 50100
//N为最大点数
#define M 150100
//M为最大边数
int n, m;//n m 为点数和边数

struct Edge{
	int from, to, nex,w;
	bool sign;//是否为桥
}edge[M<<1];
int head[N], edgenum;
void add(int u, int v,int w){//边的起点和终点
	Edge E={u, v, head[u], w,false};
	edge[edgenum] = E;
	head[u] = edgenum++;
}

int DFN[N], Low[N], Stack[N], top, Time; //Low[u]是点集{u点及以u点为根的子树} 中(所有反向弧)能指向的(离根最近的祖先v) 的DFN[v]值(即v点时间戳)
int taj;//连通分支标号,从1开始
int Belong[N];//Belong[i] 表示i点属于的连通分支
bool Instack[N];
vector<int> bcc[N]; //标号从1开始
int in[N*2],out[N*2];//记录缩点后的入度出度

void tarjan(int u ,int fa){  
	DFN[u] = Low[u] = ++ Time ;  
	Stack[top ++ ] = u ;  
	Instack[u] = 1 ;  

	for (int i = head[u] ; ~i ; i = edge[i].nex ){  
		int v = edge[i].to ;  
		if(DFN[v] == -1)
		{  
			tarjan(v , u) ;  
			Low[u] = min(Low[u] ,Low[v]) ;
			if(DFN[u] < Low[v])
			{
				edge[i].sign = 1;//为割桥
			}
		}  
		else if(Instack[v]) Low[u] = min(Low[u] ,DFN[v]) ; 		
	}  
	if(Low[u] == DFN[u]){  
		int now;
		taj ++ ; bcc[taj].clear();
		do{
			now = Stack[-- top] ;  
			Instack[now] = 0 ; 
			Belong [now] = taj ;
			bcc[taj].push_back(now);
		}while(now != u) ;
	}
}

void tarjan_init(int all){
	memset(DFN, -1, sizeof(DFN));
	memset(Instack, 0, sizeof(Instack));
	top = Time = taj = 0;
	for(int i=1;i<=all;i++)if(DFN[i]==-1 )tarjan(i, i); //注意开始点标!!!
}
vector<int>G[N];//求解缩点形成的有向无环图 
int du[N];//入度 
int cost[N],w[N]; 
void suodian(){
	memset(du, 0, sizeof(du));
	for(int i = 1; i <= taj; i++) G[i].clear(),w[i]=INF;
	for(int i = 0; i < edgenum; i++){
		int u = Belong[edge[i].from], v = Belong[edge[i].to];//注意这里就是所属的强连通分量,也就是属于哪一个缩点
		if(u!=v)G[u].push_back(v), du[v]++,w[v]=min(w[v],edge[i].w);//入度++,强连通模板(缩点),寻找每个点的从其他点到该点的最小权值的边 
	}
}
void init(){memset(head, -1, sizeof(head)); edgenum=0;}

int match_num,unmatch_node,u,v;
int match[N];
bool vis[N];
//vector<int>map[MAXN];
bool dfs(int x)
{
	for(int i=0;i<G[x].size();i++)
	{
		if(!vis[G[x][i]])
		{
			vis[G[x][i]]=true;
			if(match[G[x][i]]==-1||dfs(match[G[x][i]]))
			{
				match[G[x][i]]=x;
				return true;
			}
		}
	}
	return false;
}
void hangry()
{
	match_num=0;
	memset(match,-1,sizeof(match));
	for(int i=1;i<=taj;i++)
	{
		memset(vis,false,sizeof(vis));
		if(dfs(i))
		{
			match_num++;
		}
	}
	//match_num/=2;//求无向图的匹配时才需要除以2,有向图不可以!!! 
}

int main()
{
    //freopen("D:\\in.txt","r",stdin);
	int u,v,ww,i,j,k,T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		init();
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&u,&v);
			add(u,v,0);
		}
		tarjan_init(n);
		suodian();
	    hangry();
	    printf("%d\n",taj-match_num);
	}
	return 0;
 } 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值