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. 1071: [SCOI2007]组队 - BZOJ

    Description NBA每年都有球员选秀环节.通常用速度和身高两项数据来衡量一个篮球运动员的基本素质.假如一支球队里速度最慢的球员速度为minV,身高最矮的球员高度为minH,那么这支球队的所有 ...

  2. hdu 1085

    额    母函数 #include <cstdio> #include <cstring> int a[3],b[3]= {1,2,5}; int c1[10001],c2[1 ...

  3. the structure of the project (MVC)

    HTML <--- JSP <---- JS <---- Java controller <---- DAO <---- Database The JSP Model 2 ...

  4. MFC线程钩子和全局钩子[HOOK DLL]

    第一部分:API函数简介 1.       SetWindowsHookEx函数 函数原型 HHOOK SetWindowsHookEx( int idHook,        // hook typ ...

  5. ajax请求返回json数据弹出下载框的解决方法

    将返回的Content-Type由application/json改为text/html. 在struts2下: <action name="XXXAjax" class=& ...

  6. 学点PYTHON基础的东东--数据结构,算法,设计模式---观察者模式

    按照小明明的设计模式抄抄看看.. http://dongweiming.github.io/python-observer.html # 这个是观察者基类 class Subject(object): ...

  7. Ubuntu环境下手动配置openSSH

    配置openSSH 1.手动下载压缩文件(.tar.gz) zlib-1.2.7.tar.gz openssl-1.0.1j.tar.gz openssh-6.0p1.tar.gz 2.安装zlib ...

  8. 【转】wireshark过滤规则

    WireShark过滤语法 1.过滤IP,如来源IP或者目标IP等于某个IP 例子:ip.src eq 192.168.1.107 or ip.dst eq 192.168.1.107或者ip.add ...

  9. Qt之高级网络操作(HTTP/FTP快速上手)Qt之QHttpPart和QHttpMultiPart

    http://blog.csdn.net/u011012932/article/details/52535755 http://blog.csdn.net/u011012932/article/det ...

  10. 120. Triangle

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...