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 ...
随机推荐
- Android屏幕保持唤醒状态
我们程序偶尔会有需要屏幕一直或较长时间的保持唤醒状态,而用户的睡眠时间又设置的比较短.这时可能会对程序以及用户的使用造成一定的影响.在Android中有两种方法,可以让我们在我们需要保持唤醒的页面长时 ...
- springmvc前后端传值总结
1 前端向后端传参 1.1 普通方式传参 1.1.1 页面 参数需要解析成json对象:JSON.parse(JSON.stringify(query)) $.getJ ...
- sqlserver的增删改查
select*from shuiguo 查询表 且小于等于7的范围) select distinct name from shuiguo--去除重复,只针对一列 select * from shui ...
- android API文档查询---context、toast、SharedPreferences
/*查阅api ---context1.abstract AssetManager getAssets() Returns an AssetManager instance for the a ...
- lucene4.0 基于smb文件服务器的全文检索
使用lucene 4.0版本的全文检索 所需要的jar包 网速太慢,下次有空再把jar传上来 1.FileIndex 建立索引,查询,删除,更新 package com.strongit.tool ...
- 01_SpringMVC流程架构图
[组件说明] 以下组件通常使用框架提供实现: 1.DisPatcherServlet:前端控制器(不需要程序员开发) 用户请求到达前端控制器,它相当于MVC模式中的C(Controller),Disp ...
- Java小程序---接口中抽象方法的实现(解决了JAVA语言不能多继承的问题)
public interface Sing { public static final String eyecolor="black"; public void sleep(); ...
- SSD论文优秀句子
1. Nonvolatile memory(e.g., Phase Change Memory) blurs the boundary between memory and storage and i ...
- Qt-获取主机网络信息之QNetworkAddressEntry
QNetworkAddressEntry类存储了一个网络接口所支持的一个IP地址,同时还有与之相关的子网掩码和广播地址. 每个网络接口可以包含0个或多个IP地址,这些IP地址可以分别关联一个子网掩码和 ...
- post提交与get提交的一个小知识点
今天偶然发现post提交与get提交的一个小知识点,记下来以后可以看看. 将form表单的method的设置为get后,通过url传递的参数将不会被传递到服务器,例如1.ashx?a=123,的a=1 ...