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
Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.
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.
1 3 2 1 2 1 3
2
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;
}