Helvetic Coding Contest 2016 online mirror C1
Description
One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect – it's the opposite! Independent researchers confirmed that the nervous system of a zombie is highly complicated – it consists of nbrains (much like a cow has several stomachs). They are interconnected by brain connectors, which are veins capable of transmitting thoughts between brains. There are two important properties such a brain network should have to function properly:
- It should be possible to exchange thoughts between any two pairs of brains (perhaps indirectly, through other brains).
- There should be no redundant brain connectors, that is, removing any brain connector would make property 1 false.
If both properties are satisfied, we say that the nervous system is valid. Unfortunately (?), if the system is not valid, the zombie stops thinking and becomes (even more) dead. Your task is to analyze a given nervous system of a zombie and find out whether it is valid.
The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 1000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains a b it connects (1 ≤ a, b ≤ n, a ≠ b).
The output consists of one line, containing either yes or no depending on whether the nervous system is valid.
4 4
1 2
2 3
3 1
4 1
no
6 5
1 2
2 3
3 4
4 5
3 6
yes
并查集判断环~
#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
//#include<map>
#include<sstream>
#include<set>
#include<queue>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x3fffffff
using namespace std;
const double pi = acos(-1.);
#define maxn 100005
int fa[maxn];
bool ff[maxn];
int findd( int x )
{
int k, j, r;
r = x;
while(r != fa[r]) //查找跟节点
r = fa[r]; //找到跟节点,用r记录下
k = x;
while(k != r) //非递归路径压缩操作
{
j = fa[k]; //用j暂存parent[k]的父节点
fa[k] = r; //parent[x]指向跟节点
k = j; //k移到父节点
}
return r; //返回根节点的值
}
int main()
{
int u,v,x,y,flag = 1;
memset(ff, false, sizeof(ff));
int k = 0;
int n,m;
cin>>n>>m;
for(int i = 1; i <=n; i++)
{
fa[i]=i;
}
for(int j=1;j<=m;j++)
{
cin>>u>>v;
x = findd(u), y = findd(v);
if(x!=y)
fa[x]=y;
else
flag=0;
}
for(int i=1;i<=n;i++)
{
if(fa[i]==i)
{
k++;
}
}
if(flag&&k<=1)
{
puts("yes");
}
else
{
puts("no");
}
return 0;
}
Helvetic Coding Contest 2016 online mirror C1的更多相关文章
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
- Helvetic Coding Contest 2016 online mirror A1
Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...
- Helvetic Coding Contest 2016 online mirror F1
Description Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure ...
- Helvetic Coding Contest 2016 online mirror B1
Description The zombies are gathering in their secret lair! Heidi will strike hard to destroy them o ...
- Helvetic Coding Contest 2016 online mirror C2
Description Further research on zombie thought processes yielded interesting results. As we know fro ...
- Helvetic Coding Contest 2016 online mirror D1
Description "The zombies are lurking outside. Waiting. Moaning. And when they come..." &qu ...
- Helvetic Coding Contest 2019 online mirror (teams allowed, unrated)
http://codeforces.com/contest/1184 A1 找一对整数,使x^x+2xy+x+1=r 变换成一个分式,保证整除 #include<iostream> #in ...
- [Helvetic Coding Contest 2017 online mirror]
来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...
- 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记
第一次打ACM赛制的团队赛,感觉还行: 好吧主要是切水题: 开场先挑着做五道EASY,他们分给我D题,woc什么玩意,还泊松分布,我连题都读不懂好吗! 果断弃掉了,换了M和J,然后切掉了,看N题: l ...
随机推荐
- ACM学习历程—Rotate(HDU 2014 Anshan网赛)(几何)
Problem Description Noting is more interesting than rotation! Your little sister likes to rotate thi ...
- codevs1060 搞笑世界杯
题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...
- 弱网测试--使用fiddle进行弱网测试
数据源于:http://blog.csdn.net/eleven521/article/details/19089671 弱网测试原理以及方法(一)一.为什么要进行弱网测试?按照移动特性,各种网络连接 ...
- Java 编程规范,常见规范,命名规范,复杂度
方法/步骤 1. *不允许把多个短语句写在一行中,即一行只写一条语句 1. 示例:如下例子不符合规范. LogFilename now = null; LogFilename t ...
- hibernate 数据关联一对一
第一种一对一 person和card,card的id即作为主键,又作为外键 // 各村对方的一个对象 public class Person { private Integer id; privat ...
- LAMP 1.5 测试PHP解析 问题解决
安装完php从新加载了一个模块 php5_module 重新启动模块 /usr/local/apache2/bin/apachectl restart ifconfig 查看本机ip,在浏览器里面输入 ...
- java 学习众多API和手册资源下载
这个资源包里面有jdk api 还有SSH框架的手册,数据库的手册,Jquery手册等等,还有正则表达式等, 希望可以帮助有需要的人 链接:http://希望pan.baidu.com/s/1pJ60 ...
- p2371&bzoj2118 墨墨的等式
传送门(bzoj) 题目 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存 ...
- PAM认证
PAM认证 摘自: http://www.cnblogs.com/shenxm/p/8451889.html PAM(Pluggable Authentication Modules) Sun公司于1 ...
- Haskell语言为什么值得你去学习
摘自http://www.vaikan.com/why-haskell-is-worth-learning/ Haskell语言为什么值得你去学习 当我向一些新手推荐学习Haskell语言时,得到的反 ...