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. 转载ASP.NET MVC中Session的处理机制

    本文章转载自 http://www.cnblogs.com/darrenji/p/3951065.html ASP.NET MVC中的Session以及处理方式   最近在ASP.NET MVC项目中 ...

  2. 12. nc/netcat 用法举例

    nc命令用法举例 什么是nc nc是netcat的简写,有着网络界的瑞士军刀美誉.因为它短小精悍.功能实用,被设计为一个简单.可靠的网络工具 nc的作用 (1)实现任意TCP/UDP端口的侦听,nc可 ...

  3. 读取静态Json文件

    创建web项目: string Json = string.Empty; string filePath = Server.MapPath("/***.json");//根目录下的 ...

  4. view如何从action中取得数据和 Html辅助方法

    方式:1使用弱类型取,2,使用强类型,两者的差别在于view页面最上方声明的方式   如果使用弱类型接受来自控制器的数据,在view页面里完全不需要有任何的生命,数据可以从ViewData,ViewB ...

  5. 脚本执行JavaScript代码

    下面是一个具体的demo测试脚本引擎,执行javas文件. package Rhino; import java.io.FileReader;import java.net.URL; import j ...

  6. Linux之用户和用户组简析

    学习网址:http://c.biancheng.net/linux_tutorial/60/

  7. Unity手游引擎安全解析及实践

    近日,由Unity主办的"Unity技术开放日"在广州成功举办,网易移动安全技术专家卓辉作为特邀嘉宾同现场400名游戏开发者分享了网易在手游安全所积累的经验.当下,很多手游背后都存 ...

  8. [Swift]Xcode实际操作

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. 概念端类型“xxx”中的成员“ID”的类型“Edm.Decimal”与对象端类型“xxx”中的成员“ID”的类型“System.Int64”不匹配

    概念端类型“xxx”中的成员“ID”的类型“Edm.Decimal”与对象端类型“xxx”中的成员“ID”的类型“System.Int64”不匹配 使用EF实体模型映射之后将edmx中xml映射关系中 ...

  10. blur和focus的运用

    这两个事件不仅仅只能运用与input.span之类的元素.还可以运用于window. 可以切换title. 当切换当前页面时,改变title的文字为‘离开了’. <!DOCTYPE html&g ...