hdu 3861 The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3289 Accepted Submission(s):
1165
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.
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.
you should just output an integer which is the least number of states the king
have to divide into.
#include <cstring>
#include <cstdio>
#define N 100005 struct Edge
{
Edge *next;
int to;
}*head[N],edge[N];
struct EDge
{
EDge *next;
int to;
}*newhead[N],newedge[N];
bool instack[N],vis[N];
int cnt,T,n,m,ans,stack[N],top,low[N],dfn[N],tim,col[N],sumcol,f[N];
inline void init()
{
ans=top=tim=sumcol=cnt=;
memset(f,-,sizeof(f));
memset(col,,sizeof(col));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
memset(newhead,,sizeof(newhead));
memset(newedge,,sizeof(newedge));
}
struct node
{
int x,y;
}e[N<<];
inline int min(int a,int b) {return a>b?b:a;}
void tarjan(int x)
{
low[x]=dfn[x]=++tim;
instack[x]=;
stack[++top]=x;
for(Edge * u=head[x];u;u=u->next)
{
int v=u->to;
if(instack[v]) low[x]=min(low[x],dfn[v]);
else if(!dfn[v])
{
tarjan(v);
low[x]=min(low[x],low[v]);
}
}
if(low[x]==dfn[x])
{
int k;
sumcol++;
do
{
k=stack[top--];
instack[k]=false;
col[k]=sumcol;
}while(k!=x);
}
}
bool dfs(int x)
{
for(EDge * u=newhead[x];u;u=u->next)
{
int v=u->to;
if(!vis[v])
{
vis[v]=;
if(f[v]==-||dfs(f[v]))
{
f[v]=x;
return ;
}
}
}
return ;
}
inline void ins(int u,int v)
{
edge[++cnt].next=head[u];
edge[cnt].to=v;
head[u]=edge+cnt;
}
inline void insnew(int u,int v)
{
newedge[++cnt].next=newhead[u];
newedge[cnt].to=v;
newhead[u]=newedge+cnt;
}
int Main()
{
scanf("%d",&T);
for(;T--;)
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<=m;++i)
{
scanf("%d%d",&e[i].x,&e[i].y);
ins(e[i].x,e[i].y);
}
for(int i=;i<=n;++i)
if(!dfn[i]) tarjan(i);
cnt=;
for(int i=;i<=m;++i)
{
int cx=col[e[i].x],cy=col[e[i].y];
if(cx!=cy) insnew(cx,cy);
}
for(int i=;i<=sumcol;++i)
{
memset(vis,,sizeof(vis));
if(dfs(i)) ans++;
}
printf("%d\n",sumcol-ans);
}
return ;
}
int sb=Main();
int main(int argc,char *argv[]) {;}
hdu 3861 The King’s Problem的更多相关文章
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...
- HDU 3861.The King’s Problem 强联通分量+最小路径覆盖
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 3861 The King’s Problem trajan缩点+二分图匹配
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu——3861 The King’s Problem
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 3861 The King’s Problem 最小路径覆盖(强连通分量缩点+二分图最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...
- HDU 3861 The King’s Problem(强连通分量+最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...
- HDU 3861 The King's Problem(强连通分量缩点+最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意 ...
- HDU 3861 The King’s Problem(tarjan连通图与二分图最小路径覆盖)
题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...
随机推荐
- git for eclipse 如何取消误操作的忽略(ignore)操作
直接删除ignore文件即可.如下显示: 原文引用:https://blog.csdn.net/exceptionss/article/details/79082601
- jquery easyui datebox 的使用 .
jquery easyui datebox 的使用 . 分类: jquery-easyui2012-10-09 19:07 266人阅读 评论(0) 收藏 举报 目录(?)[+] 看了jquery e ...
- SpringMVC使用fastjson自定义Converter支持返回jsonp格式(转)
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import c ...
- C++ com
http://www.cnblogs.com/hlxs/p/3783920.html 昨天看了<COM本质论>的第一章"COM是一个更好的C++",觉得很有必要做一些笔 ...
- codevs1105 过河
1105 过河 2005年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 在河上有一座独木桥,一 ...
- uoj#36. 【清华集训2014】玛里苟斯(线性基+概率期望)
传送门 为啥在我看来完全不知道为什么的在大佬们看来全都是显然-- 考虑\(k=1\)的情况,如果序列中有某一个\(a_j\)的第\(i\)位为\(1\),那么\(x\)的第\(i\)位为\(1\)的概 ...
- LCS最大公共子序列【转载】
在两个字符串中,有些字符会一样,可以形成的子序列也有可能相等,因此,长度最长的相等子序列便是两者间的最长公共字序列,其长度可以使用动态规划来求. 以s1={1,3,4,5,6,7,7,8},s2={3 ...
- 服务器安装docker后免除sudo命令
1. 先建立一个docker组:sudo groupadd docker 2. 将用户加入docker组:sudo usermod -aG docker (用户名) 3. 先退出登录:exit 4. ...
- Leetcode:单调数列
题目 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的. 如果对于所有 i <= j,A[i]> ...
- block size大小
1.用tune2fs查看block size大小: 1 2 tune2fs -l /dev/sda1 |grep "Block size" Block size: 1024 2.用 ...