思路

每个元素设为点,化合物设为边

不能出现k条边k个点的环

直接并查集检查即可

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int fa[100100],ans=0,a,b;
void init(void){
ans=0;
for(int i=1;i<=100010;i++)
fa[i]=i;
}
int find(int x){
return (fa[x]==x)?x:fa[x]=find(fa[x]);
}
void merge(int x,int y){
if(find(x)==find(y))
ans++;
else
fa[find(x)]=find(y);
}
int main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
while(scanf("%d",&a)==1){
init();
while(a!=-1){
scanf("%d",&b);
merge(a,b);
scanf("%d",&a);
}
printf("%d\n",ans);
}
return 0;
}

UVA1160 X-Plosives的更多相关文章

  1. uva1160 易爆物

    #include<iostream>#include<cstdio>#include<algorithm>#include<cstdlib>using ...

随机推荐

  1. 本地上传文件至服务器的技巧(linux文件压缩及解压文件)

    linux(ubuntu)文件解压及压缩文件 ubuntu支持文件的解压及压缩功能, 如果ubuntu上面没有安装过unzip工具的话,可以通过下面命令安装: sudo apt-get install ...

  2. css3奇数偶数的伪属性

    <style> /*奇数*/ ul li:nth-child(odd){ background-color: green; } /*偶数*/ ul li:nth-child(even){ ...

  3. java学习之路--多线程实现的方法

    1 继承Thread类 继承Thread类的方法尽管被我列为一种多线程实现方式,但Thread本质上也是实现了Runnable接口的一个实例,它代表一个线程的实例,并且,启动线程的唯一方法就是通过Th ...

  4. Jupyter notebook安装

    之前就装了jupyter notebook,但今天打开来发现是python2,并且似乎没法转换到python3??? so,再把python3的版本安装一下 打开CMD pip install jup ...

  5. ZOJ 4067 - Books - [贪心][2018 ACM-ICPC Asia Qingdao Regional Problem J]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4067 题意: 给出 $n$ 本书(编号 $1 \sim n$), ...

  6. 搭建Harbor企业级docker仓库

    搭建Harbor企业级docker仓库 一.Harbor简介 1.Harbor介绍 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如 ...

  7. VB调用C# dll

    cd C:\Windows\Microsoft.NET\Framework\v4.0.30319注册regasm myTest.dll /tlb:myTest.tlb

  8. 自动化部署iptables防火墙脚本

    #!/bin/sh # Remove any existing rules # 清除规则 /sbin/iptables -F # 清除用户自定义规则 /sbin/iptables -X # 清除链的计 ...

  9. 004-mac下Java6与Java8 安装

    一.Java6安装 官方下载下载地址:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-down ...

  10. java框架之SpringBoot(6)-Restful风格的CRUD示例

    准备 环境 IDE:Idea SpringBoot版本:1.5.19 UI:BootStrap 4 模板引擎:thymeleaf 3 效果:Restful 风格 CRUD 功能的 Demo 依赖 &l ...