POJ1161(并查集)
2024-09-02 22:44:35
1、题目链接地址
http://poj.org/problem?id=1161
2、源代码
#include <iostream>
using namespace std;
int parent[];
int suspect[]; int find(int x)
{
if(parent[x] == x)
{
return x;
}
else
{
return parent[x] = find(parent[x]);
}
} void Union(int a, int b)
{
int x = find(a);
int y = find(b);
if(x == y)
{
return;
}
parent[y] = x;
suspect[x] = suspect[x] + suspect[y];
} int main()
{
int m, n;
int a, b, c;
int x;
int i;
while(cin >> m >> n)
{
if(m == && n == )
{
break;
} for(i = ; i <= m; i++)
{
parent[i] = i;
suspect[i] = ;
} while(n--)
{
cin >> a >> b;
a--;
while(a--)
{
cin >> c ;
if(find(c) != find(b))
{
Union(c, b);
}
}
} cout << suspect[find()] << endl;
}
return ;
}
最新文章
- SQL Server的镜像是基于物理块变化的复制 镜像Failover之后数据的预热问题
- [译] 理解PHP内部函数的定义(给PHP开发者的PHP源码-第二部分)
- css基础知识
- Android (二维码)关于java.lang.UnsatisfiedLinkError的小案例
- BC#32 1002 hash
- tagName和nodeName的区别
- C\C++拾遗------C#程序员重温C\C++之要点
- Windows安全模式的妙用
- idhttp.post方式 调用datasnap rest 远程方法
- mvc3.0中[ValidateInput(false)]失效的问题
- (转载)Convolutional Neural Networks卷积神经网络
- sql2005如何附加数据库
- 上传jar包到Apache Archiva本地仓库
- css(二)
- Linux企业级项目实践之网络爬虫(1)——项目概述及准备工作
- javaScript 工作必知(二) null 和undefined
- pwnable.kr brainfuck之write up
- 我自己总结的C#开发命名规范整理了一份
- (五十六)iOS多线程之NSOperation
- Java面试题中常考的容易混淆的知识点区别