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算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...
随机推荐
- day1 java基础回顾-泛型
2.泛型(Generic) 当集合中存储的对象类型不同时,那么会导致程序在运行的时候的转型异常 1 import java.util.ArrayList; 2 import java.util.Ite ...
- DropDownlist数据SelectedIndexChanged触发问题解决
1.设置DropDownlist的AutoPostBack为True 2.绑定DropDownlist数据时出现了重复项, 在载入数据时保存数据状态应该写在Load事件中的if (!IsPostBac ...
- Linear Algebra - Determinant(几何意义)
二阶行列式的几何意义 二阶行列式 \(D = \begin{vmatrix}a_1&a_2\\b_1&b_2\end{vmatrix} = a_1b_2 - a_2b_1\) 的几何意 ...
- SqlServer2012——多表连接查询
1.基本连接 select A.姓名,A.性别,B.班级名,B.家庭住址 From 学生信息 A,班级信息 B where A.所属班级=B.班级编号 --把A表与B表连接起来 2.内连接 --内连接 ...
- DSOFramer控件使用注意事项
1.引用dll==>AxInterop.DSOFramer.dll ==>Interop.DSOFramer.dll ==>WindowsFormsIntegration ==> ...
- tarjan求割点割边的思考
这个文章的思路是按照这里来的.这里讨论的都是无向图.应该有向图也差不多. 1.如何求割点 首先来看求割点.割点必须满足去掉其以后,图被分割.tarjan算法考虑了两个: 根节点如果有两颗及以上子树,它 ...
- SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景
一.拦截器简介 1.拦截器定义 拦截器,请求的接口被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. 拦截器主要用来按照指定规则拒绝请求. 2.拦截器中应用 Token令牌 ...
- jquery获取文档高度和窗口高度汇总
jquery获取窗口高度和窗口高度,$(document).height().$(window).height() $(document).height():整个网页的文档高度 $(window).h ...
- C/C++<算法>进制转换超详细
16转10 用竖式计算: 16进制数的第0位的权值为16的0次方,第1位的权值为16的1次方,第2位的权值为16的2次方 第0位: 5 * 16^0 = 5 第1位: F * 16^1 = 240 第 ...
- CSS3动画总结学习(一)
参考文章: CSS3 Transitions, Transforms和Animation使用简介与应用展示 CSS 参考手册 动画的分类 平移动画 transform: 就是变换, 变换, 变换 也就 ...