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 ...
随机推荐
- 移动端 -webkit-user-select:text; ios10 bug 解决方案
移动端一般body的css.会设置 作用就不解释了: body{ height:100%;min-height:100%; font-family: "微软雅黑",'Helveti ...
- Ubuntu 11.10 Server下搭建Maven私服
安装Nexus服务的文档可以参考官方站点:http://www.sonatype.com/books/nexus-book/reference/install-sect-install.html ...
- Java并发系列[1]----AbstractQueuedSynchronizer源码分析之概要分析
学习Java并发编程不得不去了解一下java.util.concurrent这个包,这个包下面有许多我们经常用到的并发工具类,例如:ReentrantLock, CountDownLatch, Cyc ...
- alibaba架包FastJson使用例子
alibaba的架包FastJson可以对json字符串进行快捷的类型转换.下面是一些各种类型转换的使用例子. 一.下载FastJson的架包,并导入项目中,如下: Maven项目pom.xml配置如 ...
- MyISAM 存储引擎的特点及优化方法
MyISAM: MyISAM 管理非事务表.是ISAM 的扩展格式.除了提供ISAM里所没有的索引的字段管理等的大量功能.MyISAM 还使用一种表格锁定的机制.来优化多个并发的读写操作.My ...
- android 通过getDimension,getDimensionPixelOffset和getDimensionPixelSize获取dimens.xml文件里面的变量值
dimens.xml里写上三个变量: <dimen name="activity_vertical_margin1">16dp</dimen> <di ...
- 6.Ray-消息订阅器编写
消息订阅器: Ray是基于Event Sourcing设计的ES/Actor框架,消息发布后需要订阅处理,订阅器主要有以下两类: CoreHandler消息订阅器=RabbitSub+SubHandl ...
- ArcGIS License启动无响应
根据对网上的总结以及个人的经验,首先建议关闭电脑的防火墙以及杀毒软件 如果点击启动,依然没有反应,建议在dos下进行启动,命令为: lmgrd -z -c service.txt 如果需要深入研究,可 ...
- JAVA并发编程学习笔记------FutureTask
FutureTask是Future和Callable的结合体.传统的代码是这样写的Future f = executor.submit(new Callable()); 然后通过Future来取得计算 ...
- Flex Grid学习-链接
这些是我个人在学习这两种布局的时候参考的资料,希望对大家有用-- 1.Flex 阮一峰(flex语法讲解):http://blog.csdn.net/naruto_luoluo/article/det ...