UVA 1160 X-Plosives
题意是一次装入物品,物品由两种元素组成,当遇到即将装入的物品与已经装入的物品形成k个物品,k种元素,跳过该物品的装入。可以将每种元素看成顶点,物品看成一条边。这样问题就转化为利用并查集求环的情况。
算法竞赛训练指南中的代码:
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define pb push_back
#define in freopen("in.txt", "r", stdin);
#define out freopen("out.txt", "w", stdout);
#define print(a) printf("%d\n",(a));
#define bug puts("********))))))");
#define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
#define inf 0x0f0f0f0f
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii,int> VII;
typedef vector<int>:: iterator IT; #define N (int)(1e5+100)
int pa[N], rank[N];
void init(void)
{
for(int i = ; i < N; i++)
pa[i] = i;
memset(rank, , sizeof(rank));
}
void merge(int x, int y)
{
if(rank[x] > rank[y])
pa[y] = x;
else {
pa[x] = y;
if(rank[x] == rank[y])
rank[y]++;
}
}
int findset(int x)
{
return (pa[x] == x) ? x: pa[x] = findset(pa[x]);
}
int main(void)
{
int x, y;
while(~scanf("%d", &x))
{
int cnt = ;
init();
while(x != -)
{
scanf("%d", &y);
x = findset(x), y = findset(y);
if(x == y)
cnt++;
else merge(x, y);
scanf("%d", &x);
}
printf("%d\n", cnt);
}
return ;
}
UVA 1160 X-Plosives的更多相关文章
- LA 3644 - X-Plosives ( 也即UVA 1160)
LA看题 请点击:传送门 UVA 上也有这题 :UVA 1160 - X-Plosives 题目大意就是如果车上存在 k 个简单化合物,正好包含 k 种元素 ,那么它们将有危险,此时你应该拒绝装车. ...
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...
- UVA - 1160(简单建模+并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- UVA - 1160 X-Plosives
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
随机推荐
- RabbitMQ 原文译1.2--"Hello Word"
本系列文章均来自官网原文,属于个人翻译,如有雷同,权当个人归档,忽喷. .NET/C# RabbitMQ 客户端下载地址:https://github.com/rabbitmq/rabbitmq-do ...
- hhgis驱动
function loadPathInfo 在car.js中 function loadPathInfo(carid, fnClass) { var qureydrive; if (jsonp) qu ...
- ###C中的extern-static-const关键词
#@date: 2014-06-14 #@author: gerui #@email: forgerui@gmail.com Contents extern的作用一般是用来声音一个外部变量和函数.一般 ...
- ###《More Effective C++》- 基础议题
More Effective C++ #@author: gr #@date: 2015-05-11 #@email: forgerui@gmail.com 一.仔细区别pointers和refere ...
- 规则引擎ILog和CKRule的对比
IBM™ WebSphere™ ILOG是业界最有影响力的业务规则商业软件,它提供了最好的业务规则管理系统,在Java领域更是有广泛的成功案例.网上关于ILOG的技术资料非常多,大家都比较了解ILOG ...
- 06_init()和destroy()方法
[工程截图] [HelloWorld.java] package com.HigginCui; public class HelloWorld { public HelloWorld(){ Syste ...
- makefile-0711-168 SEVERE ERROR: Input file:
ld: 0711-168 SEVERE ERROR: Input file: /cicm/commlib/include Input files must be regular file ...
- 配置Apache+Mysql+Php
以下操作均在Debian 6.0 64bit 环境root权限下进行,如果提示权限不足请切换至root用户或者sudo,本人比较喜欢自行安装,因为安装的过程中能最小化安装而且能够知道安装了什么,然后可 ...
- web api写api接口时返回
web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...
- jQuery.ajax()的一些例子
例子: Example: 保存数据到服务器,成功时显示信息. 1 2 3 4 5 6 7 $.ajax({ method: "POST", url: "some.php& ...