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. Redis,Memcache的区别和具体应用场景

    1. Memcached简介 Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric为首开发的高性能分布式内存缓存服务器.其本质上就是一个 ...

  2. NEFU 119

    和上一题一样,注意除不尽为0 #include <iostream> #include <cstdio> #include <cstring> #include & ...

  3. POJ 1320

    作弊了--!该题可以通过因式分解得到一个佩尔方程....要不是学着这章,估计想不到.. 得到x1,y1后,就直接代入递推式递推了 x[n]=x[n-1]*x[1]+d*y[n-1]*y[1] y[n] ...

  4. mysql点滴_02程序中运行sql语句报字符集问题解决

    程序中运行  "SELECT t.EVENT_TYPE_ID FROM RATABLE_EVENT_TYPE t WHERE t.NAME='帐期末费用转移事件'"  报错 错误码 ...

  5. Android广播机制分析

    1.1. 广播简单介绍         Android 广播与生活中的广播概念不同,它是指系统中产生事件后的通知. Android 广播不关心接收者是否收到处理或者怎样处理广播,能够说是一种单向的通知 ...

  6. maven 项目加载本地JAR

     将jar安装到本地的maven仓库 1.首先确定本地有maven环境. 2.安装本地jar 模板: mvn install:install-file -Dfile=<path-to-file& ...

  7. 在Maven中引入spring的DAO、DOMAIN、CONTROLLER、VIEW

    除了mysql外麻雀虽小,五脏俱全. 参照之前的博客建立好的maven项目如图. 第一步 : 向maven项目中的pom文件添加依赖 ,然后maven install

  8. Underscore模板的使用

    一.开篇 下载underscode.js 二.使用 <!DOCTYPE html> <html lang="en"> <head> <me ...

  9. listview添加的头部布局超过一屏头部内容显示不全

    headView的实际高度超过一个屏幕,但是显示的结果只有一个屏幕,超过一个屏幕高度意外的部分显示不全. 只使用了listView.getRefreshable().addHeadView(headV ...

  10. Python更换pip源,更换conda源

    更换pip源: 1.在windows文件管理器中,输入 %APPDATA% 2.在该目录下新建pip文件夹,然后到pip文件夹里面去新建个pip.ini文件 3.在新建的pip.ini文件中输入以下内 ...