HDU3173 Dominos
2024-09-04 10:56:24
单向并查集,问至少给几个点可以遍历全图,连通块数量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 ;
}
最新文章
- 动态规划(DP)
- 【bzoj1016】 JSOI2008—最小生成树计数
- 转--简单工厂模式 Simple Factory
- WCF之事务
- sublime安装插件
- 洛谷 P1156 垃圾陷阱
- 【转】Android Building System 总结 - 一醉千年 - CSDN博客
- Gradle sync failed: failed to find Build Tools revision 21.1.2
- 通过git和Xcode将代码上传到GitHub
- window权限 及c++实现 【网摘】(转)
- ios7以上自定义导航栏标题的字体大小及颜色的方法
- yii性能调节
- 解决VS2010中winsock.h与winsock2.h冲突(重复定义)——转载
- PyTorch安装
- matplotlib绘图的基本操作
- zookeepeer4字命令实践
- html----属性操作
- tcp连接状态查看
- 微信小程序:bindtap等事件传参
- Socket模型(二):完成端口(IOCP)