#include <cstring>
#include <cstdlib>
#include <cstdio>
缩点的好处就是可以将乱七八糟的有向图 转化为无环的有向图
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
#define MAXN 200010
#define clr(x,k) memset((x),(k),sizeof(x))
struct node
{
int st,to,next;
}
edge[MAXN];
int n,m,ct,id;
int head[MAXN],low[MAXN],dfn[MAXN],belong[MAXN],in[MAXN],to[MAXN];
//DFN[i]表示 遍历到 i 点时是第几次dfs
//Low[u] 表示 以u点为父节点的 子树 能连接到 [栈中] 最上端的点 的DFN值
bool instack[MAXN];
stack<int>q;
void add_e(int i,int u,int v)
{
edge[i].st=u;
edge[i].to=v;
edge[i].next=head[u];
head[u]=i;
}
void tarjan(int i)
{
int j;
dfn[i]=low[i]=++id;
q.push(i);
instack[i]=1;
for(int u=head[i]; ~u; u=edge[u].next)
{
j=edge[u].to;
if(dfn[j]==0)
{
tarjan(j);
if(low[i]>low[j])
low[i]=low[j];
}
else if(instack[j]&&low[i]>low[j])
low[i]=dfn[j];
}
if(dfn[i]==low[i])
{
ct++;
do
{
j=q.top();
q.pop();
instack[j]=0;
belong[j]=ct;
}
while(i!=j);
}
}
int main()
{
int t,i,u,v,sum1,sum2;
cin>>t;
while(t--)
{
clr(head,-1);
clr(low,0);
clr(dfn,0);
clr(belong,0);
clr(in,0);
clr(to,0);
while(!q.empty())
q.pop();
cin>>n>>m;
for(i=0; i<m; i++)
{
cin>>u>>v;
add_e(i,u,v);
}
id=ct=0;
for(i=1; i<=n; i++)
{
if(!dfn[i])
tarjan(i);
}
if(ct==1)
{
cout<<0<<endl;
continue;
}
for(i=1; i<=ct; i++)
{
in[i]=to[i]=0;
}
for(i=0; i<m; i++)// 利用染色进行缩点 新的图的点的坐标为第i个强连通分量
{
if(belong[edge[i].st]!=belong[edge[i].to])
{
in[belong[edge[i].st]]++;
to[belong[edge[i].to]]++;
}
}
sum1=sum2=0;
for(i=1; i<=ct; i++)
{
if(in[i]==0)
sum1++;
if(to[i]==0)
sum2++;
}
cout<<max(sum1,sum2)<<endl;
}
return 0;
}

最新文章

  1. unable to boot the simulator,无法启动模拟器已解决
  2. Find Peak Element
  3. 省市级联.net
  4. 状态压缩 poj 3254
  5. cocos2d-x之物理引擎初试
  6. WinForm窗体间如何传值
  7. C++经典编程题#5:寻找下标
  8. 使用Mysqldump 备份数据库
  9. linux 上下文切换带来的影响
  10. HTML里面Textarea换行总结
  11. c++11 生产者/消费者
  12. 9.Flask Cookie和Session
  13. ViewPager和Fragment中的View的点击事件冲突
  14. LightOJ 1258 Making Huge Palindromes(KMP)
  15. UserInfoActivity用户图像修改和退出登录
  16. vi-vim :删除、撤销、恢复删除、复制删除
  17. c++编译时打印宏定义
  18. Mysql 用户权限管理--从 xxx command denied to user xxx
  19. PHP时间戳 strtotime()使用方法和技巧
  20. C. Line (扩展欧几里得)

热门文章

  1. centos7的网络配置参考
  2. 重读APUE(5)-文件权限
  3. 进程 | 线程 | 当Linux多线程遭遇Linux多进程
  4. C之枚举
  5. JavaScript函数中的this四种绑定形式
  6. AutoHome项目的学习
  7. 通过route指令指定笔记本同时连接外网和内网
  8. 【JS新手教程】弹出两层div,及在LODOP内嵌上层
  9. CentOS7.2环境下安装Nginx
  10. SNIPER-MXNet中出现ValueError: could not broadcast input array from shape (XXX,5) into shape (100,5)