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. Elasticsearch---基于scroll技术滚动搜索大量数据

    如果一次性要查出来比如10万条数据,那么性能会很差,此时一般会采取用scoll滚动查询,一批一批的查,直到所有数据都查询完处理完 使用scoll滚动搜索,可以先搜索一批数据,然后下次再搜索一批数据,以 ...

  2. 编程算法 - 求1+2+...+n(模板类) 代码(C++)

    求1+2+...+n(模板类) 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 求1+2+...+n, 要求不能使用乘除法\for\whil ...

  3. POJ 2189

    P是端点,牛在区域中啊... #include <iostream> #include <cstdio> #include <cstring> #include & ...

  4. POJ 1948

    这道题我记得是携程比赛上的一道. 开始时想直接设面积,但发现不可以,改设能否构成三角形.设dp[i][j][k]为前i根木棍构成边长为j和k的三角形,那么转移可以为dp[i][j][k]=dp[i-1 ...

  5. 在不同的浏览器下FORM及它的小伙伴们默认样式的CSS属性值是不全然一致

    我们一般在定义CSS样式的时候都须要定义去掉HTML标签默认样式的CSS,原因是在不同的浏览器以下它们的表现出来的默认样式不全然一致,我们要保证在不同的浏览器下它们能正常显示出我们想要达到的预期效果, ...

  6. UML期末绘图及细节总结

    往届期末绘图的题目例如以下所看到的: Read the providing materials carefully, and then do tasks. 2.1: Use Case Diagram ...

  7. HTML5 格式化方式以及应用

    <b>加粗字体 <big>定义大号字体 <em>定义着重文字 <i>定义斜体字 <small>定义小号字体 <strong>定义 ...

  8. DB-MySQL:MySQL 临时表

    ylbtech-DB-MySQL:MySQL 临时表 1.返回顶部 1. MySQL 临时表 MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysq ...

  9. 不使用Store安装WSL

    Windows Store经常会因为各种原因打不开, 这时候我们可以尝试直接下载安装WSL     1. PowerShell里运行下载: PS C:\WINDOWS\system32> Inv ...

  10. [转]Linux常用命令学习

    转自 https://www.cnblogs.com/gaojun/p/3359355.html 1.ls命令 就是list的缩写,通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文 ...