Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3289    Accepted Submission(s):
1165

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
 
 
Tarjan缩点+匈牙利算法
Tarjan写错了。。
#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的更多相关文章

  1. HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)

    HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...

  2. HDU 3861.The King’s Problem 强联通分量+最小路径覆盖

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. 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 ...

  4. 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 ...

  5. hdu——3861 The King’s Problem

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDU 3861 The King’s Problem 最小路径覆盖(强连通分量缩点+二分图最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...

  7. HDU 3861 The King’s Problem(强连通分量+最小路径覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...

  8. HDU 3861 The King's Problem(强连通分量缩点+最小路径覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意 ...

  9. HDU 3861 The King’s Problem(tarjan连通图与二分图最小路径覆盖)

    题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...

随机推荐

  1. phpcms后台栏目权限修改无效的原因和解决方法

    现象:在phpcms后台中,新建角色,然后修改角色对应栏目权限,结果一直只能选择一半数量的栏目.剩下的栏目怎么修改都不生效. 对比: step1:再另一个phpcms后台做同样操作,依旧是这个结果.跟 ...

  2. Git的使用 强制放弃本地所有修改,获取master中最新版本更新本地

    git fetch --all git reset --hard origin/master git fetch --all 的意思是,下载远程库的所有内容,但不与本地做任何合并 git reset ...

  3. HDU - 5878 2016青岛网络赛 I Count Two Three(打表+二分)

    I Count Two Three 31.1% 1000ms 32768K   I will show you the most popular board game in the Shanghai ...

  4. HDU 1556 Color the ball 前缀和+思维

    Color the ball N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球 ...

  5. Linux 系统初始化和服务

    系统的初始化和服务 1. Linux 系统启动流程 打开计算机,从主板 BIOS(Basic Input/Out System)读取其中所存储的程序,引导你找到存储系统的硬件(如光盘.硬盘等) 接下来 ...

  6. python + requests实现的接口自动化框架详细教程

    前段时间由于公司测试方向的转型,由原来的web页面功能测试转变成接口测试,之前大多都是手工进行,利用postman和jmeter进行的接口测试,后来,组内有人讲原先web自动化的测试框架移驾成接口的自 ...

  7. [poj]1050 To the Max dp

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  8. xrange与range之间的区别

    对于这两个好像功能都差不多,这两个经常会被搞混,所以今天一定要把这个完全弄清楚. 首先我们看看range: range([start,] stop[, step]),根据start与stop指定的范围 ...

  9. c++弱引用与强引用

    https://www.zhihu.com/question/26851369 智能指针一个很重要的概念是"所有权",所有权意味着当这个智能指针被销毁的时候,它指向的内存(或其它资 ...

  10. 洛谷P1582 倒水

    P1582 倒水 题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把 ...