题解 CF690C1
题目大意:
给定一张 \(n\) 个点 \(m\) 条边的无向图,判断这是不是一棵树。
题目分析:
两种思路:
思路一:
不需要建图,直接使用并查集判环即可
最后判断一下图联不联通就行,具体方法就是看并查集中是不是 \(fa_x = x\) 的情况只存在一个,或者看边数是否等于 \(n-1\)。
时间复杂度 \(O(n + m \log n)\)
思路二:
建图,用 \(dfs\) 判环。
时间复杂度 \(O(n + m)\)
这里给出思路一的代码供大家参考。
代码实现:
#include <bits/stdc++.h>
#define debug(x) cerr<<#x<<": "<<x<<endl;
#define int long long
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
namespace Larry76{
const int MAX_SIZE = 1.1e5;
int fa[MAX_SIZE];
int getfa(int x){
if(fa[x]==x)
return fa[x];
return fa[x] = getfa(fa[x]);
}
void merge(int x,int y){
int fx = getfa(x);
int fy = getfa(y);
fa[fy] = fx;
}
void main(){
//Code Here;
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
fa[i] = i;
while(m--){
int u,v;
cin>>u>>v;
if(getfa(u)==getfa(v)){
cout<<"no"<<endl;
return;
}
merge(u,v);
}
int same = 0;
for(int i=1;i<=n;i++)
if(fa[i] == i)
same ++;
if(same>1)
cout<<"no"<<endl;
else
cout<<"yes"<<endl;
}
}
signed main(){
#ifdef LOCAL
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
double c1 = clock();
#else
ios::sync_with_stdio(false);
#endif
//============================================
Larry76::main();
//============================================
#ifdef LOCAL
double c2 = clock();
cerr<<"Used Time: "<<c2-c1<<"ms"<<endl;
if(c2-c1>1000)
cerr<<"Warning!! Time Limit Exceeded!!"<<endl;
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
题解 CF690C1的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
- JSOI2016R3 瞎BB题解
题意请看absi大爷的blog http://absi2011.is-programmer.com/posts/200920.html http://absi2011.is-programmer.co ...
随机推荐
- ansible 的特点
ansible的特点 基于Python语言实现 模块化,调用特定的模块,完成特定任务 部署简单,基于python和SSH(默认已安装),yum install 即可,不需要客户端 安全,基于OpenS ...
- 2021-11-30 wpf的mvvm绑定2
主页页面代码 <Grid> <TextBox x:Name="First" Width="80" Height="20" ...
- 2021-4-19 vs加速启动小技巧之intellitrace
在选项界面中将intellitrace的启用关闭后对于程序的打开有加速作用.
- RAT蓝队自动化测试框架
RAT蓝队自动化测试框架 介绍 RAT 是根据 MITRE ATT&CK 战术矩阵测试蓝队检测能力的脚本框架,由 python2.7 编写,共有 50 多种不同 ATT&CK 技术点和 ...
- 一个批处理,解决你重装python第三方模块的烦恼~(1.0版本)
@echo offpip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simplepython -m pip insta ...
- 用python selenium提取网页中的所有<a>标签中的超级链接地址
urls = driver.find_elements_by_xpath("//a") for url in urls: print(url.get_attribute(" ...
- CSSRelated
CSS 几种常用的清除浮动方法 ️️️ 父级 div 定义伪类:after 和 zoom; /* 这个class名指的是需要清除浮动的父级 */ .clearfloat:after { display ...
- 一些不错的VSCode设置和插件
设置 同步设置 我们做的各项设置,不希望再到其他机器的时候还得再重新配置一次.VSCode中我们可以登陆微软账号或者GitHub账号,登陆后我们可以开启同步设置.开启设置同步,根据提示登陆即可. 允许 ...
- C# 中关于 T 泛型【C# 基础】
〇.前言 C# 里面的泛型不仅可以使用泛型函数.泛型接口,也可以使用泛型类.泛型委托等等.在使用泛型的时候,它们会自行检测你传入参数的类型,因此它可以为我们省去大量的时间,不用一个个编写方法的重载.与 ...
- 论文解读(SentiX)《SentiX: A Sentiment-Aware Pre-Trained Model for Cross-Domain Sentiment Analysis》
Note:[ wechat:Y466551 | 可加勿骚扰,付费咨询 ] 论文信息 论文标题:SentiX: A Sentiment-Aware Pre-Trained Model for Cross ...