hihoCoder#1322(树的判定)
描述
给定一个包含 N 个顶点 M 条边的无向图 G ,判断 G 是不是一棵树。
输入
第一个是一个整数 T ,代表测试数据的组数。 (1 ≤ T ≤ 10)
每组测试数据第一行包含两个整数 N 和 M 。(2 ≤ N ≤ 500, 1 ≤ M ≤ 100000)
以下 M 行每行包含两个整数 a 和 b ,表示顶点 a 和顶点 b 之间有一条边。(1 ≤ a, b ≤ N)
输出
对于每组数据,输出YES或者NO表示 G 是否是一棵树。
- 样例输入
-
2
3 2
3 1
3 2
5 5
3 1
3 2
4 5
1 2
4 1 - 样例输出
-
YES
NO 思路:判定是否为树的2个充要条件:①边的数目等于结点数-1 ②图连通(并查集只有一个根)#include <iostream>
#include <string.h>
using namespace std;
const int MAXN=;
int n,m;
int par[MAXN];
void prep()
{
for(int i=;i<MAXN;i++)
{
par[i]=i;
}
}
int fnd(int x)
{
if(x==par[x])
{
return x;
}
return par[x]=fnd(par[x]);
}
void unite(int x,int y)
{
int a=fnd(x);
int b=fnd(y);
par[a]=b;
}
int main()
{
int T;
cin>>T;
while(T--)
{
prep();
cin>>n>>m;
for(int i=;i<m;i++)
{
int x,y;
cin>>x>>y;
unite(x,y);
}
int root=-;
int cnt=;
for(int i=;i<=n;i++)
{
int fa=fnd(i);
if(fa!=root)
{
root=fa;
cnt++;
}
}
if(n-==m&&cnt==)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return ;
}
hihoCoder#1322(树的判定)的更多相关文章
- hihocoder -1121-二分图的判定
hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...
- nyoj 129 树的判定
并查集+欧拉 树的判定 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 A tree is a well-known data structure that is e ...
- hihocoder 1391 树状数组
#1391 : Countries 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are two antagonistic countries, countr ...
- hihocoder 1322 - 树结构判定 - [hiho一下161周][模板题/水题]
题目链接:http://hihocoder.com/problemset/problem/1322 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶 ...
- hihoCoder 1014trie树(字典树)
hihoCoder 1014 题目提示已经很清楚了~ 贴代码…… #include <iostream> #include <cstdio> #include <cstr ...
- HihoCoder——Trie树
本文出自:http://blog.csdn.net/svitter 原题:http://hihocoder.com/contest/hiho2/problem/1 题解:使用Trie树..基础题目.一 ...
- HihoCoder 1511: 树的方差(prufer序)
题意 对于一棵 \(n\) 个点的带标号无根树,设 \(d[i]\) 为点 \(i\) 的度数,定义一棵树的方差为数组 \(d[1..n]\) 的方差. 给定 \(n\) ,求所有带标号的 \(n\) ...
- #1014 : Trie树 HihoCoder(字典树)
描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题: ...
- NYOJ 129 树的判定 (并查集)
题目链接 描述 A tree is a well-known data structure that is either empty (null, void, nothing) or is a set ...
随机推荐
- Python内置标准模块
time 模块 1 >>> import time 2 >>> time.time() 3 1491064723.808669 4 >>> # t ...
- 【bzoj1232】[Usaco2008Nov]安慰奶牛cheer(最小生成树)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1232 这道题要保留的道路肯定是原图的一棵生成树,因为要保留n-1条边,且使删边后的图连 ...
- ElasticSearch入门常用命令
基于开源项目MyAlice智能客服学习ElasticSearch https://github.com/hpgary/MyAlice/wiki/%E7%AC%AC01%E7%AB%A0%E5%AE%8 ...
- python爬虫-初步认识
特此声明: 以下内容来源于博主:http://blog.csdn.net/pleasecallmewhy http://cuiq ...
- HDU 3473 Minimum Sum (划分树求区间第k大带求和)(转)
题意:在区间中找一个数,求出该区间每个数与这个数距离的总和,使其最小 找的数字是中位数(若是偶数个,则中间随便哪个都可)接着找到该区间比此数大的数的总和 区间中位数可以使用划分树,然后在其中记录:每层 ...
- 【jsoi】第一季 [略]精简题解
UPD:好像有两道题的代码逃跑了?= =就先不找了,反正都是水题. 精简题解系列第四弹.(其实也不是那么精简啊= =) [JSOI2008]最大数maxnumber 单点修改,区间最大值查询,裸线段树 ...
- zhly
5. 百叶002 名字2008 1.新浪 阿里矢量图库账号 15031116087 名字2008 2.acdsee 账号 1173209945 同密码一样 3.zhly 我的名字2016 ...
- phalcon查询:单条查询,多条查询,多表查询
单条查询, $order = \OrderMain::findFirst("oid='" . $oid . "'"); 多条查询, $shop = \Order ...
- Python爬虫之利用BeautifulSoup爬取豆瓣小说(三)——将小说信息写入文件
#-*-coding:utf-8-*- import urllib2 from bs4 import BeautifulSoup class dbxs: def __init__(self): sel ...
- Adobe 产品更新直接下载链接
mac:http://prodesigntools.com/adobe-cc-2015-updates-links-mac.html win:http://prodesigntools.com/ado ...