http://acm.hdu.edu.cn/showproblem.php?pid=3861

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3863 3859 3868 3865 3862 
 
 
Tarjan缩点+最大独立集(强连通个数-最大匹配数)
 #include <cstring>
#include <cstdio> #define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
const int N(+);
const int M(+);
int hed[N],sumedge,had[N];
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[M],e[M];
inline void ins(int u,int v,int *head,Edge *edge)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} int tim,dfn[N],low[N];
int top,instack[N],Stack[N];
int sumcol,col[N],rd[N],cd[N];
void DFS(int u)
{
low[u]=dfn[u]=++tim;
Stack[++top]=u; instack[u]=;
for(int v,i=hed[u];i;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v]) DFS(v), low[u]=min(low[u],low[v]);
else if(instack[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
col[u]=++sumcol;
for(;u!=Stack[top];top--)
{
col[Stack[top]]=sumcol;
instack[Stack[top]]=;
}
instack[u]=; top--;
}
} int sumvis,vis[N],match[N];
bool find(int u)
{
for(int v,i=had[u];i;i=e[i].next)
{
v=e[i].v;
if(vis[v]==sumvis) continue;
vis[v]=sumvis;
if(!match[v]||find(match[v]))
{
match[v]=u;
return true;
}
}
return false;
} inline void init()
{
tim=top=sumedge=sumcol=sumvis=;
memset(e,,sizeof(e));
memset(vis,,sizeof(vis));
memset(col,,sizeof(col));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(hed,,sizeof(hed));
memset(had,,sizeof(had));
memset(edge,,sizeof(edge));
memset(Stack,,sizeof(Stack));
memset(match,,sizeof(match));
memset(instack,,sizeof(instack));
}
inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int main()
{
int t; read(t);
for(int n,m;t--;init())
{
read(n),read(m);
for(int u,v;m--;)
read(u),read(v),ins(u,v,hed,edge);
for(int i=;i<=n;i++)
if(!dfn[i]) DFS(i);
for(int u=;u<=n;u++)
for(int v,i=hed[u];i;i=edge[i].next)
{
v=edge[i].v;
if(col[u]!=col[v]) ins(col[u],col[v],had,e);
}
int ans=;
for(int i=;i<=sumcol;i++)
{
sumvis++;
if(find(i)) ans++;
}
printf("%d\n",sumcol-ans);
}
return ;
}
 

HDU——T 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

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

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

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

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

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

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

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

随机推荐

  1. 工具-vscode使用

    1.智能感知 vscode使用DefinitelyTyped进行自动完成所以需要先安装tsd,命令: npm install -g tsd 安装完成后,首先安装node基本语法支持 tsd query ...

  2. jQuery Video Extend

    HTML5视频扩展插件 能够加入Logo 加入标记 用法: 下载:jquery-video-extend <script src="js/jquery-2.1.4.min.js&quo ...

  3. HDOJ 2647 Reward 【逆拓扑排序+分层】

    题意:每一个人的基础工资是888. 因为一部分人要显示自己水平比較高,要求发的工资要比其它人中的一个人多.问你能不能满足他们的要求,假设能的话终于一共要发多少钱,假设不能就输出-1. 策略:拓扑排序. ...

  4. 淘宝数据库OceanBase SQL编译器部分 源代码阅读--生成逻辑计划

    淘宝数据库OceanBase SQL编译器部分 源代码阅读--生成逻辑计划 SQL编译解析三部曲分为:构建语法树.生成逻辑计划.指定物理运行计划. 第一步骤,在我的上一篇博客淘宝数据库OceanBas ...

  5. VS2013找不到SDKDDKVer.h

    今天在升级vs2010 的project的时候遇到了一个这种问题.提示:找不到SDKDKVer.h 通过查找资料发现,原来是vs版本号之间Windows SDK的路径宏定义不同,有些坑. 网上有人说能 ...

  6. Android获取系统时间的多种方法

    Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现. 现总结如下: 方法一: ? 1 2 3 4 5 ...

  7. nginx的安装步骤

    nginx学习资料;https://zhuanlan.zhihu.com/p/34943332 1.下载nginx的安装包:https://nginx.org/en/download.html 2. ...

  8. 36.QT地图

    widget.h #ifndef MAPWIDGET_H #define MAPWIDGET_H #include <QGraphicsView> #include <QLabel& ...

  9. 移动端H5页面编辑器开发实战--经验技巧篇

    很久前的写的文章了,转载下发到这里 原本地址: https://blog.csdn.net/tech_meizu/article/details/52484775

  10. ORM框架——Dapper的应用

    常见的ORM框架有EF,Dapper,NHibernate 参考:http://shuai7boy.iteye.com/blog/2357339 http://www.cnblogs.com/Sint ...