Brain Network (easy)(并查集水题)
G - Brain Network (easy)
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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 n brains (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.
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 a b it connects (1 ≤ a, b ≤ n, a ≠ b).
Output
The output consists of one line, containing either yes or no depending on whether the nervous system is valid.
Sample Input
4 4
1 2
2 3
3 1
4 1
no
6 5
1 2
2 3
3 4
4 5
3 6
Output
yes
//并查集水题,第一行 n , m 是点,和边个数,然后 m 边的描述,问是否都连通了且没有多余的边
#include<stdio.h>
int f[];
int find(int x)
{
if (f[x]!=x)
f[x]=find(f[x]);
return f[x];
}
int main()
{
int n,m;
while (scanf("%d%d",&n,&m)!=EOF)
{
int i,j;
for (i=;i<=n;i++)
f[i]=i; int a,b,temp;
scanf("%d%d",&a,&b);
temp=a;
f[a]=f[b];
int ok=;
for (i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
int head_a=find(a);
int head_b=find(b);
if (head_a!=head_b)
f[head_a]=f[head_b];
else
ok=;
}
for (i=;i<=n;i++)
{
if (ok==) break;
if (find(i)!=find(temp))
{
ok=;
break;
}
}
if (ok)
printf("yes\n");
else
printf("no\n");
}
return ;
}
Brain Network (easy)(并查集水题)的更多相关文章
- 【PAT-并查集-水题】L2-007-家庭房产
L2-007. 家庭房产 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下 ...
- poj2524(并查集水题)
题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...
- POJ2524并查集水题
Description There are so many different religions in the world today that it is difficult to keep tr ...
- HDU1863(Kruskal+并查集水题)
https://cn.vjudge.net/problem/HDU-1863 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可). ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- codeforces 690C1 C1. Brain Network (easy)(水题)
题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- POJ 2236 Wireless Network(并查集)
传送门 Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 24513 Accepted ...
- POJ 2236 Wireless Network (并查集)
Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...
- POJ 2236:Wireless Network(并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 36363 Accepted: 150 ...
随机推荐
- ElasticSearch中设置排序Java
有用的链接:http://stackoverflow.com/questions/12215380/sorting-on-several-fields-in-elasticsearch 有的时候,需要 ...
- asp.net显示评论的时候为几天前的格式
自己做的一个小项目实现的功能,做个记录先~ 效果如图: 代码如下: public static class TimerHelper { public static string GetTimeSpan ...
- WINDOWS 2003系统时间24小时制与12小时显示格式不一致问题与解决
WINDOWS 2003 SERVER系统时间24小时制与12小时格式不一致问题,而导致论坛或BLOG不能正常显示更新日期的问题及解决方法问题的方法. 在WIN2003上出现这种情况的原因是在程序中使 ...
- 【HTML 元素】标记文字
1.用基本的文字元素标记内容 先看显示效果: 对应HTML代码: <!DOCTYPE html> <html lang="en"> <head> ...
- SSRF漏洞的挖掘经验
本文转自:https://sobug.com/article/detail/11 SSRF概述 SSRF(Server-Side Request Forgery:服务器端请求伪造) 是一种由攻击者构造 ...
- 【VBA】查看当前窗口的宽与高
打开的Excle如何使用VBA查看当前窗口的宽与高呢?代码如下: Sub 查看宽度与高度() Dim myWidth As Double Dim myHeigth As Double myWidth ...
- index+small+row+if经典函数组合应用
EXCEL中index+small+row+if 函数组合可以查出满足同一条件的所有记录,通过实例讲解: 本文为原创,转载需标明出处,谢谢! 例:查找出一年级的所有班级及人数: A B C D 1 年 ...
- 工作总结 用, 隔开数据 后台不可以用 List<string> 接收 get请求直接通过浏览器发请求传数组或者list到后台
旁边的 css js 为项目的 加载 css js 地址 只加载引用的样式 js http://localhost:8736/LinInFoKPI/ExcelPrint?line=&start ...
- MQTT--topic
1.topic 定阅与发布必须要有主题,只有当定阅了某个主题后,才能收到相应主题的payload,才能进行通信. 2. 主题层级分隔符—“/” 主题层级分隔符使得主题名结构化.如果存在分隔符,它将 ...
- Hadoop 配置及hadoop HA 的配置
注:本文中提到的ochadoop 不要感到奇怪,是亚信公司内部自己合成的一个包.把全部的组件都放在一个包内了.免去了组件的下载过程和解决兼容问题.事实上也能够自己下载的.不要受到影响. 另,转载请注明 ...