UVAlive X-Plosives

思路:

   “如果车上存在k个简单化合物,正好包含k种元素,那么他们将组成一个易爆的混合物”  如果将(a,b)看作一条边那么题意就是不能出现环,很容易联想到Kruskal算法中并查集的判环功能(新加入的边必须属于不同的两个集合否则出现环),因此本题可以用并查集实现。模拟装车过程即可。

代码:

 #include<cstdio>
#include<cstring>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std;
const int maxn= +; int p[maxn];
int find_set(int u){ //寻找root+路径压缩
return u==p[u]? u : p[u]=find_set(p[u]);
}
int main(){
int x,y,refusal;
while(scanf("%d",&x)==){
refusal=; //直接用变量统计
FOR(i,,maxn) p[i]=i;
while(x != -){
scanf("%d",&y);
int xr=find_set(x),yr=find_set(y);
if(xr == yr) refusal ++;
else p[xr]=yr;
scanf("%d",&x);
}
printf("%d\n",refusal);
}
return ;
}

【暑假】[实用数据结构]UVAlive 3644 X-Plosives的更多相关文章

  1. 【暑假】[实用数据结构]UVAlive 3135 Argus

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

  2. 【暑假】[实用数据结构]UVAlive 3026 Period

    UVAlive 3026 Period 题目: Period   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  3. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  4. 【暑假】[实用数据结构]UVAlive 4329 Ping pong

    UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: % ...

  5. 【暑假】[实用数据结构]UVAlive 3027 Corporative Network

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

  6. 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns

    UVAlive 4670 Dominating Patterns 题目:   Dominating Patterns   Time Limit: 3000MS   Memory Limit: Unkn ...

  7. 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?

    UVa11991 Easy Problem from Rujia Liu?  思路:  构造数组data,使满足data[v][k]为第k个v的下标.因为不是每一个整数都会出现因此用到map,又因为每 ...

  8. 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!

    UVa11995  I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...

  9. 【暑假】[实用数据结构]范围最小值问题(RMQ)

    范围最小值问题: 提供操作: Query(L,R):计算min{AL ~ AR } Sparse-Table算法: 定义d[i][j]为从i开始长度为2j的一段元素的最小值.所以可以用递推的方法表示. ...

随机推荐

  1. MySQL Replication 常用架构

    转自: http://www.cnblogs.com/ggjucheng/archive/2012/11/13/2768879.html 前言 MySQLReplicaion本身是一个比较简单的架构, ...

  2. scikit-learn安装

    1.依赖包: Cython.rose.numpy.scipy.lapack.atlas http://blog.chinaunix.net/uid-22488454-id-3978860.html

  3. 1058-Tom and Jerry

    描述 Tom和Jerry在10*10的方格中: *...*..... ......*... ...*...*.. .......... ...*.C.... *.....*... ...*...... ...

  4. jmeter 启用gzip压缩——解决测试中web服务器上行流量过大的问题

    最近测了几个前端的项目,发现它们都有一个共同点:应用所在服务器的网卡上行(trans)非常大——经常是 117 MB/S,这已经逼近了千兆网卡的极限了.下面记录下排查和解决过程: 一. jmeter ...

  5. 让阿里云的Centos,PHP组件 ImageMagick支持png和jpeg格式

    我们在Centos安装ImageMagick教程中讲述了如何安装ImageMagick,安装完毕之后发现程序并不支持png和jpeg格式的图片,但是这两种图片又是我们平时所常见的,所以我们还要进一步地 ...

  6. Apache Tomcat下载、安装、配置图文教程

    本文已迁移到我的个人网站 http://www.wshunli.com 文章地址: http://www.wshunli.com/2016/03/19/Tomcat安装配置/ (整理截图.安装过程更加 ...

  7. Fundamental Datastructure

    11988 - Broken Keyboard (a.k.a. Beiju Text) 可以用deque来模拟. #include <iostream> #include <stri ...

  8. JS复制对象

    CSSCommonJS.DeepCopy = function (json) { if (typeof json == 'number' || typeof json == 'string' || t ...

  9. JavaScript动态操作style

    1.易错:修改元素的样式不是设置class属性,而是className属性.class是JS的一个保留关键字. 2.易错:单独修改样式的属性使用"style.属性名"3.注意在cs ...

  10. 怎么知道RTL Schematic中的instance与哪段代码对应呢

    2013-06-23 20:15:47 ISE综合后可以看到RTL Schematic,但我们知道在RTL编码时,要经常问自己一个问题“我写的这段代码会综合成什么样的电路呢”.对于一个简单的设计,比如 ...