UVALive - 3644 X-Plosives (并查集)
思路:每一个product都可以作一条边,每次添加一条边,如果这边的加入使得某个集合构成环,就应该refuse,那么就用并查集来判断。
AC代码:
//#define LOCAL
#include <stdio.h>
#include <string.h>
const int maxn = 1e5 + 5;
int par[maxn], rank[maxn];
void init() {
memset(rank, 0, sizeof(rank));
for(int i = 0; i <= maxn; i++) {
par[i] = i;
}
}
int findRoot(int x) {
return x != par[x] ? par[x] = findRoot(par[x]) : x;
}
//启发式合并
void unionSet(int x, int y) {
if(rank[x] > rank[y]) {
par[y] = x;
} else {
par[x] = y;
if(rank[x] == rank[y]) rank[y]++;
}
}
int main() {
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif // LOCAL
int x, y, refuse = 0;
init();
while(scanf("%d", &x) == 1) {
if(x == -1) {
printf("%d\n", refuse);
init();
refuse = 0;
}else {
scanf("%d", &y);
x = findRoot(x);
y = findRoot(y);
if(x == y) {
//refuse it
refuse++;
} else {
//union
unionSet(x, y);
}
}
}
return 0;
}
如有不当之处欢迎指出!
UVALive - 3644 X-Plosives (并查集)的更多相关文章
- UVALive - 3644 X-Plosives (并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- UVALive(LA) 3644 X-Plosives (并查集)
题意: 有一些简单化合物,每个化合物都由两种元素组成的,你是一个装箱工人.从实验员那里按照顺序把一些简单化合物装到车上,但这里存在安全隐患:如果车上存在K个简单化合物,正好包含K种元素,那么他们就会组 ...
- UVALive 4487 Exclusive-OR 加权并查集神题
已知有 x[0-(n-1)],但是不知道具体的值,题目给定的信息 只有 I P V,说明 Xp=V,或者 I P Q V,说明 Xp ^ Xq=v,然后要求回答每个询问,询问的是 某任意的序列值 Xp ...
- UVALive - 3027 Corporative Network (并查集)
这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...
- UVALive 6910 Cutting Tree 并查集
Cutting Tree 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- UVALive 6906 Cluster Analysis 并查集
Cluster Analysis 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemi ...
- UVALive 6889 City Park 并查集
City Park 题目连接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/F Description P ...
- 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856
并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...
- LA 3644 易爆物 并查集
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
随机推荐
- 转载-Linux Shell 数组建立及使用技巧
转载自:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html 如侵犯版权,请联系我删除 linux shell在编程方面比win ...
- linkin大话设计模式--建造模式
linkin大话设计模式--建造模式 建造模式是对象的创建模式,可以讲一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象. 建造模式的结构: 抽象建造者 ...
- Centos 7 安装 memcached
一.准备工作: 安装之前确保你的系统上已经安装libevent和telnet· 1.测试是否安装telnet: # rpm -qa |grep telnet 如果显示结果为: telnet-0.17- ...
- Storm Topology Parallelism
Understanding the Parallelism of a Storm Topology What makes a running topology: worker processes, e ...
- awkOFS问题
awk改变了OFS,$0却没变化一个文件1.txt,内容如下 a b c d e 目的把列变行,输出为: a b c d e 脚本如下: awk 'BEGIN{RS="";FS=& ...
- python自动化运维常用模块安装
1.系统信息性能模块:psutil 源码安装: wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.tar.gz -- ...
- java for循环增强(foreach)
for循环增强,在此之前还不知道foreach有这样的功能,先鄙视一下自己,留给自己看: 功能: ***若List用foreach : [ for(Student stu : list) ]这种形 ...
- use zlib lib to compress or decompress file
If you want to compress or decompress file when writing C++ code,you can choose zlib library,that's ...
- [DeeplearningAI笔记]改善深层神经网络_深度学习的实用层面1.9_归一化normalization
觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.9 归一化Normaliation 训练神经网络,其中一个加速训练的方法就是归一化输入(normalize inputs). 假设我们有一个 ...
- Erlang调度器细节探析
Erlang调度器细节探析 Erlang的很多基础特性使得它成为一个软实时的平台.其中包括垃圾回收机制,详细内容可以参见我的上一篇文章Erlang Garbage Collection Details ...