单向并查集,问至少给几个点可以遍历全图,连通块数量n,入度为0的点的数量m,取max(n,m)~

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e6+;
int father[maxn],isRoot[maxn],T,M,N,u,v,visit[maxn];
void init () {
for (int i=;i<maxn;i++) father[i]=i;
fill (isRoot,isRoot+maxn,);
fill (visit,visit+maxn,);
}
int findfather (int x) {
int a=x;
while (x!=father[x]) x=father[x];
while (a!=father[a]) {
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union (int a,int b) {
int faA=findfather(a);
int faB=findfather(b);
if (faA!=faB) father[faA]=faB;
}
int main () {
while (~scanf ("%d",&T)) {
while (T--) {
init ();
scanf ("%d %d",&N,&M);
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
visit[v]=;
Union (u,v);
}
for (int i=;i<=N;i++) isRoot[findfather(i)]++;
int ans=,ans1=;
for (int i=;i<=N;i++) if (isRoot[i]) ans++;
for (int i=;i<=N;i++) if (!visit[i]) ans1++;
printf ("%d\n",max(ans,ans1));
}
}
return ;
}

最新文章

  1. 动态规划(DP)
  2. 【bzoj1016】 JSOI2008—最小生成树计数
  3. 转--简单工厂模式 Simple Factory
  4. WCF之事务
  5. sublime安装插件
  6. 洛谷 P1156 垃圾陷阱
  7. 【转】Android Building System 总结 - 一醉千年 - CSDN博客
  8. Gradle sync failed: failed to find Build Tools revision 21.1.2
  9. 通过git和Xcode将代码上传到GitHub
  10. window权限 及c++实现 【网摘】(转)
  11. ios7以上自定义导航栏标题的字体大小及颜色的方法
  12. yii性能调节
  13. 解决VS2010中winsock.h与winsock2.h冲突(重复定义)——转载
  14. PyTorch安装
  15. matplotlib绘图的基本操作
  16. zookeepeer4字命令实践
  17. html----属性操作
  18. tcp连接状态查看
  19. 微信小程序:bindtap等事件传参
  20. Socket模型(二):完成端口(IOCP)

热门文章

  1. spring boot 实战笔记(一)
  2. 题解 【Codeforces387B】George and Round
  3. setInterval 和 setTimeout 定时器
  4. 在电竞圈想摧枯拉朽的AI,到底能带来什么?
  5. python2.7 字符处理小节
  6. 1.java-谈谈接口
  7. Python 多任务(线程) day2 (1)
  8. springboot无法查询到后台的数据
  9. Git - 05. git log &amp; git show
  10. Mac配置内网穿透