HDU3861 The King’s Problem
来源:互联网 发布:模具加工中心编程 编辑:程序博客网 时间:2024/05/28 06:04
传送门
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
2011 Multi-University Training Contest 3 - Host by BIT
题目大意
有n个点,一些点之间存在有向边,定义一种集合为集合中的点可以互相到达或者可以单向到达,且都不能经过别的集合中的点。问至少需要分成几个集合。(多组数据)
题解
tarjan缩点当然是一眼看出的,但是由于单向到达也可以算在内,所以我们需要将这个新图转换成二分图,跑二分图最大匹配,可以用最大流解决。
CODE:
#include<queue>#include<cstdio>#include<cstring>using namespace std;const int N=50005;const int M=1e5+10;const int INF=1e9;struct edge{ int nxt,to;}a[M];struct Edge{ int nxt,to,remain;}e[M<<4];int head[N],Head[N];int dfn[N],low[N],block[N];int s[N],top;int deep[N];bool instack[N];int Q,n,m,x,y,num,Num,tot,Time,S,T;queue<int>q;inline int min(const int &a,const int &b){return a<b?a:b;}inline void add(int x,int y){ a[++num].nxt=head[x],a[num].to=y,head[x]=num;}inline void add2(int x,int y){ e[++Num].nxt=Head[x],e[Num].to=y,e[Num].remain=1,Head[x]=Num; e[++Num].nxt=Head[y],e[Num].to=x,e[Num].remain=0,Head[y]=Num;}void dfs(int now){ dfn[now]=low[now]=++Time; instack[now]=1; s[++top]=now; for(int i=head[now];i;i=a[i].nxt) if(!dfn[a[i].to]) { dfs(a[i].to); low[now]=min(low[now],low[a[i].to]); } else if(instack[a[i].to]) low[now]=min(low[now],dfn[a[i].to]); if(low[now]==dfn[now]) { int tmp; tot++; do tmp=s[top--],instack[tmp]=0,block[tmp]=tot; while(tmp!=now); }}inline bool bfs(){ memset(deep,0x3f,sizeof(deep)); deep[S]=0;q.push(S); while(!q.empty()) { int tmp=q.front();q.pop(); for(int i=Head[tmp];i;i=e[i].nxt) if(deep[e[i].to]>INF&&e[i].remain) q.push(e[i].to),deep[e[i].to]=deep[tmp]+1; } return deep[T]<INF;}int dfs(int now,int limit){ if(now==T||!limit) return limit; int flow=0,f; for(int i=Head[now];i;i=e[i].nxt) if(deep[e[i].to]==deep[now]+1&&e[i].remain&&(f=dfs(e[i].to,min(limit,e[i].remain)))) { flow+=f,limit-=f,e[i].remain-=f,e[i^1].remain+=f; if(!limit) return flow; } deep[now]=-1;return flow;}inline int dinic(){int ans=0;while(bfs())ans+=dfs(S,INF);return ans;}int main(){ scanf("%d",&Q); while(Q--) { scanf("%d%d",&n,&m); memset(dfn,0,sizeof(dfn)); memset(head,0,sizeof(head)); memset(Head,0,sizeof(Head)); num=tot=Time=0;Num=1; for(int i=1;i<=m;i++) scanf("%d%d",&x,&y),add(x,y); for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i); for(int j=1;j<=n;j++) for(int i=head[j];i;i=a[i].nxt) if(block[j]!=block[a[i].to]) add2(block[j],block[a[i].to]+tot); S=tot<<1|1,T=S+1; for(int i=1;i<=tot;i++) add2(S,i),add2(tot+i,T); printf("%d\n",tot-dinic()); } return 0;}
- hdu3861 The King’s Problem
- HDU3861 The King’s Problem
- HDU3861 The King’s Problem
- hdu3861 The King’s Problem --- 强连通+二分图
- hdu3861 The King’s Problem【强连通+最小路径覆盖】
- HDU3861 The King’s Problem Tarjan缩点+二分图求最小路径覆盖
- hdu3861 The King’s Problem (强连通缩点+最小路径覆盖)
- hdu3861 The King’s Problem (强连通+最小路径覆盖)
- HDU3861-The King’s Problem(有向图强连通缩点+最小路径覆盖)
- [HDU3861]The King's Problem(Tarjan缩点+二分图匹配)
- [HDU3861]The King's Problem(Tarjan缩点+二分图匹配)
- hdu3861 The King’s Problem 强连通分量+二分图匹配
- HDU 3861 The King’s Problem
- hdu 3861 The King’s Problem
- hdu 3861 The King’s Problem
- hdu 3861 The King’s Problem
- HDOJ 3861 The King’s Problem
- HDU 3861 The King’s Problem
- 子网掩码及网络划分
- POJ 1627 Experiment "X": Explosions Expected 笔记
- C# HTML
- 第35课: 打通Spark系统运行内幕机制循环流程
- CSS样式
- HDU3861 The King’s Problem
- mvp入门
- deep learning 自编码算法详细理解与代码实现
- mvp进阶
- wordpress教程 – 修改增加默认文章阅读量的最简单办法
- Java基础--枚举类型
- 30 C语言中的整数(short,int,long)
- 欢迎使用CSDN-markdown编辑器
- 5-29