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
- php学习笔记-PHP中的几个取整函数
floor是向下取整,比如4.5,它是在4和5之间的一个数,那么结果就是4. ceil是向上取整,比如3.7,它是在3和4之间的一个数,那么结果就是4. round是对一个数四舍五入,小数部分如果小于 ...
- ASPNET session客户端与服务…
除非程序通知服务器删除一个session,否则服务器会一直保留,程序一般都是在用户做log off的时候发个指令去删除session.然而浏览器从来不会主动在关闭之前通知服务器它将要关闭,因此服务器根 ...
- 5、scala面向对象-类
一.类 1.定义类 ##定义并调用 scala> :paste // Entering paste mode (ctrl-D to finish) class HelloWord { priva ...
- ubuntu12.04+virtualbox+winxp的关于摄像头无法使用,声音出不来的问题
前天在ubuntu上安装了个virtualbox的虚拟机.以前在windows下面是用的vmware.结果到了ubuntu下面折腾半天用不了,于是就装了个virtualbox,在virtualbox里 ...
- JS控制GridView行选择
ASP.NET里的GridView控件使用非常广泛,虽然其功能强大,但总有一些不尽如人意的地方.比如在选择行的时候,它就没有UltraWebGrid做的友好:UltraWebGrid允许用户设置是否显 ...
- css hack汇总
注意点: 网上很多资料中常常把!important也作为一个hack手段,其实这是一个误区.!important常常被我们用来更改样式,而不是兼容hack.造成这个误区的原因是IE6在某些情况下不主动 ...
- 将Gridview导出到Excel
GridViewToExcel(EdceExcelGV, "application/ms-exce","xxxxxx表"); protected void Gr ...
- u17 u18共存
公司用的Unity版本是2017版本的,由于需要尝试一些实验性的新功能,我就安装了Unity2018版本,结果发现Unity2018版本破解之后,Unity2017版本不能用了.那么怎么解决两个版本的 ...
- cogs 421. HH的项链
421. HH的项链 http://218.28.19.228/cogs/problem/problem.php?pid=421 ★★★ 输入文件:diff.in 输出文件:diff.out ...