HDU——T The King’s Problem
2024-08-31 11:11:39
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.
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
3 2
1 2
1 3
Sample Output
2
Source
Recommend
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 ;
}
最新文章
- Solaris 自动挂载
- 设置参数进行启动的Python脚本模板
- 查看oracle死锁进程并结束死锁
- 4-python学习——数据操作
- System-Defined Device Setup Classes Available to Vendors
- 小白日记50:kali渗透测试之Web渗透-CSRF
- ODB 下载与安装 (Linux)
- Web学习-apache视图log刊物
- AJAX应用的五个步骤
- (转载)Java多线程的监控分析工具(VisualVM)
- Gson序列化问题导致的内存溢出,tip:Background sticky concurrent mark sweep GC freed
- Spring cloud定义学习
- MySQL学习笔记Windows篇<;一>; Welcome to MySQL
- Linux alias别名命令
- 第二章&#160;向量(d5)有序向量:插值查找
- Linux 系统的网络配置文件
- class文件格式说明
- 利用gcc的__attribute__编译属性section子项构建初始化函数表【转】
- C++和Java函数传递数组参数比较
- C语言实现括号配对问题