UVA - 1160(简单建模+并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a specific association of products occurs. Each product is a mix of two different simple compounds, to which we call a binding pair. If N > 2, then mixing N different binding pairs containing N simple compounds creates a powerful explosive. For example, the binding pairs A+B, B+C, A+C (three pairs, three compounds) result in an explosive, while A+B, B+C, A+D (three pairs, four compounds) does not. You are not a secret agent but only a guy in a delivery agency with one dangerous problem: receive binding pairs in sequential order and place them in a cargo ship. However, you must avoid placing in the same room an explosive association. So, after placing a set of pairs, if you receive one pair that might produce an explosion with some of the pairs already in stock, you must refuse it, otherwise, you must accept it. An example. Lets assume you receive the following sequence: A+B, G+B, D+F, A+E, E+G, F+H. You would accept the first four pairs but then refuse E+G since it would be possible to make the following explosive with the previous pairs: A+B, G+B, A+E, E+G (4 pairs with 4 simple compounds). Finally, you would accept the last pair, F+H. Compute the number of refusals given a sequence of binding pairs.
Input
The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line. Instead of letters we will use integers to represent compounds. The input contains several lines. Each line (except the last) consists of two integers (each integer lies between 0 and 105) separated by a single space, representing a binding pair. Each test case ends in a line with the number ‘-1’. You may assume that no repeated binding pairs appears in the input.
Output
For each test case, the output must follow the description below. A single line with the number of refusals.
Sample Input
1 2 3 4 3 5 3 1 2 3 4 1 2 6 6 5 -1
Sample Output
3
题意:
有n种化合物,每种化合物由两种元素组成。当几种的化合物数量等于他们所含不同元素的数量时,就会发生爆炸。现在依次给出化合物的组成,当新的化合物与之前的化合物放在一起会发生爆炸时,就不能允许这个化合物放进来。输出拒绝的次数。
思路:
把元素看成点,化合物看成边,每次新的化合物进来当成连一条边。如果图中没有环,则每个连通分量是一棵树,其边数等于点数减1,不可能存在爆炸的情况;如果图中有环,则这个环上点数等于边数,就会爆炸。使用并查集连边,如果要连的两个点在同一集合中,则答案加1。
code:
#include <iostream>
#include<algorithm>
#include <cstdio>
#include<cstring>
#include<math.h>
#include<memory>
using namespace std;
typedef long long LL;
#define max_v 100005
int pa[max_v];
int re[max_v];
int n,ans;
void make_set(int x)
{
pa[x]=x;
re[x]=;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y)
{
x=find_set(x);
y=find_set(y); if(x==y)
{
ans++;
return ;
} if(re[x]>re[y])
pa[y]=x;
else
{
pa[x]=y;
if(re[x]==re[y])
re[y]++;
}
}
int main()
{
int x,y;
while(~scanf("%d",&x))
{
ans=;
for(int i=;i<max_v;i++)
make_set(i);
while(x!=-)
{
scanf("%d",&y);
union_set(x,y);
scanf("%d",&x);
}
printf("%d\n",ans);
}
return ;
}
//判断有没有构成环,构成环的不放进去且计数加1
//输出计数
UVA - 1160(简单建模+并查集)的更多相关文章
- The Suspects 简单的并查集
Description 严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not ...
- The Suspects(简单的并查集)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- UVA 11987 - Almost Union-Find(并查集)
UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...
- hdu 1182 A Bug's Life(简单种类并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...
- UVA 12232 Exclusive-OR(并查集+思想)
题意:给你n个数,接着三种操作: I p v :告诉你 Xp = v I p q v :告诉你 Xp ^ Xq = v Q k p1 p2 … pk:问你k个数连续异或的结果 注意前两类操作可能会出现 ...
- hdu 1213 (How Many Tables)(简单的并查集,纯模板)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- UVA 10158 War(并查集)
//思路详见课本 P 214 页 思路:直接用并查集,set [ k ] 存 k 的朋友所在集合的代表元素,set [ k + n ] 存 k 的敌人 所在集合的代表元素. #include< ...
- UVA - 11987 Almost Union-Find 并查集的删除
Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to imp ...
- uva 6910 - Cutting Tree 并查集的删边操作,逆序
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
随机推荐
- Drupal7配置简洁URL(Clear URL)
参考:Apache Rewrite url重定向功能的简单配置 这两天折腾死了 首先说一下我的环境: Aache2.4.25-x64-vc14-r1;php-7.0.19-Win32-VC14-x64 ...
- 关于电脑宽带显示连接 qq可以登录 但是无法上网的问题
---恢复内容开始--- 大家都遇到过这种情况吧,右下角显示网络已连接,但就是上不了网,解决的办法大都是什么,打开网络与共享中心设置什么协议什么的,当然,这些有可能是有用的,但是有一些不管怎么设置协议 ...
- windows 下 openssl 生成RSA私钥公钥以及PKCS8
生成RSA私钥 打开bin文件夹下面的openssl.exe,输入genrsa -out rsa_private_key.pem 1024 把RSA私钥转换成PKCS8格式 输入命令pkcs8 -to ...
- ssh key生成步骤
1. 安装git,从程序目录打开 "Git Bash" ,或者直接用git shell,github自带的工具 2. 键入命令:ssh-keygen -t rsa -C " ...
- Linux下通过samba进行文件共享与挂载
1.在共享服务端安装samba:# yum install samba samba-client.x86_64 samba-common -y 2.在共享服务端需要防火墙开通139.445端口: # ...
- maven dependendency
登录|注册 zhengsj的专栏 目录视图 摘要视图 订阅 [公告]博客系统优化升级 [收藏]Html5 精品资源汇集 博乐招募开始啦 Maven De ...
- sort 、sorted、range、join方法 数字的正序、倒叙、翻转
大家可以想象一下 如果一串数字 是混乱的没有顺序这个时候你想要排序怎么办呢? 这个时候顺势而生的就有了sort方法 把数字从大到小的排列----->sort() 只对列表 li = [3, 5 ...
- 企业级Ngnix基于域名的配置_server
普通的nginx配置 egrep -v "#|^$" /usr/local/nginx/conf/nginx.conf.default 更改nginx的配置文件-->注意空格 ...
- centos6.3_gcc安装软件及流程
分享链接下载文件上传安装即可 链接:https://pan.baidu.com/s/15eUhm9LTUdHDPkXOyRb9Tg 密码:tohr # 文件内置安装脚本,直接安装即可 [root@lo ...
- 在VisualStudio中远程调试IIS站点
如何在本地调试服务器上的站点? 如果要在本地调试服务器上的站点我们需要用到一个远程调试工具msvsmon.exe. 我们可以再VS安装目录中找到它 如:C:\Program Fil ...