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:

  1. It should be possible to exchange thoughts between any two pairs of brains (perhaps indirectly, through other brains).
  2. 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.

Input

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 ab it connects (1 ≤ a, b ≤ na ≠ b).

Output

The output consists of one line, containing either yes or no depending on whether the nervous system is valid.

Examples
input
4 4
1 2
2 3
3 1
4 1
output
no
input
6 5
1 2
2 3
3 4
4 5
3 6
output
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的更多相关文章

  1. CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)

    题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...

  2. Helvetic Coding Contest 2016 online mirror A1

    Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...

  3. Helvetic Coding Contest 2016 online mirror F1

    Description Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure ...

  4. Helvetic Coding Contest 2016 online mirror B1

    Description The zombies are gathering in their secret lair! Heidi will strike hard to destroy them o ...

  5. Helvetic Coding Contest 2016 online mirror C2

    Description Further research on zombie thought processes yielded interesting results. As we know fro ...

  6. Helvetic Coding Contest 2016 online mirror D1

    Description "The zombies are lurking outside. Waiting. Moaning. And when they come..." &qu ...

  7. Helvetic Coding Contest 2019 online mirror (teams allowed, unrated)

    http://codeforces.com/contest/1184 A1 找一对整数,使x^x+2xy+x+1=r 变换成一个分式,保证整除 #include<iostream> #in ...

  8. [Helvetic Coding Contest 2017 online mirror]

    来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...

  9. 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记

    第一次打ACM赛制的团队赛,感觉还行: 好吧主要是切水题: 开场先挑着做五道EASY,他们分给我D题,woc什么玩意,还泊松分布,我连题都读不懂好吗! 果断弃掉了,换了M和J,然后切掉了,看N题: l ...

随机推荐

  1. windows 2013 datacenter 安装sql server2008 r2兼容性

    add-windowsfeature RSAT-Clustering-AutomationServer

  2. bzoj 4537: [Hnoi2016]最小公倍数 分块+并查集

    题目大意: 给定一张n个点m条边的无向图,每条边有两种权.每次询问某两个点之间是否存在一条路径上的边的两种权的最大值分别等于给定值. n,q <= 50000. m <= 100000 题 ...

  3. w3c上的SQL 教程---基本语法 语句学习

    SQL 教程路径:http://www.w3school.com.cn/sql/index.asp

  4. 洛谷 4178 Tree——点分治

    题目:https://www.luogu.org/problemnew/show/P4178 点分治.如果把每次的 dis 和 K-dis 都离散化,用树状数组找,是O(n*logn*logn),会T ...

  5. grep的用法(转)

    grep参数 -c  : 显示匹配的行数(就是显示有多少行匹配了): -n :显示匹配内容所在文档的行号: -i  :匹配时忽略大小写: -s :错误信息不输出: -v :输出不匹配内容: -o : ...

  6. 解决mysql 客户端连接问题

        问题:在linux 上安装了mysql服务端,使用客户端连接时报错信息为:ERROR 1045 (28000): Access denied for user 'root'@'localhos ...

  7. HDOJ(1069)最长下降子序列

    每个箱子可有3种叠加方式,所以有3*n个箱子.将箱子按长度由大到小排序,有求箱子按宽度的最长下降子序列的高度之和即可. #include<cstdio> #include<algor ...

  8. 文件异步上传,多文件上传插件uploadify

    本文中使用java作为例子 uploadify下载 http://files.cnblogs.com/chyg/uploadify.zip jsp页面中需要引入: <script type=&q ...

  9. UML核心元素--参与者

    定义:参与者是在系统之外与系统交互的某人或某事物.参与者在建模过程中处于核心地位. 1.系统之外:系统之外的定义说明在参与者和系统之间存在明确的边界,参与者只能存在于边界之外,边界之内的所有人和事务都 ...

  10. java基础知识(7)---多态

    多 态:(面向对象特征之一):函数本身就具备多态性,某一种事物有不同的具体的体现.体现:父类引用或者接口的引用指向了自己的子类对象.//Animal a = new Cat();多态的好处:提高了程序 ...