题目1109:连通图
来源:互联网 发布:av5644769只有我知 编辑:程序博客网 时间:2024/05/01 22:47
#include <iostream>#include <stdio.h>#include <algorithm>#include <math.h>#include <string>#include <string.h>#include <vector>#include <queue>#include <stack>using namespace std;//1109 method2int T[1010];void init(int n){ for(int i=1;i<=n;i++){ T[i]=i; }}//查找根并进行路径优化int findRoot(int x){ if(T[x]==x) return x; else{ int t=findRoot(T[x]); T[x]=t; return t; }}void unio(int a,int b){ if(a!=b) T[a]=b;}int main(){ int n,m; //freopen("E://PAT/input.txt","r",stdin); while(scanf("%d %d",&n,&m)!=EOF&&n!=0){ init(n); while(m--){ int a,b;scanf("%d %d",&a,&b); a=findRoot(a),b=findRoot(b); unio(a,b); } int cnt=0; for(int i=1;i<=n;i++) if(T[i]==i) cnt++; printf("%s\n",cnt>1?"NO":"YES"); }}/************************************************************** Problem: 1109 User: cust123 Language: C++ Result: Accepted Time:20 ms Memory:1524 kb****************************************************************/
0 0
- 题目1109:连通图
- 题目1109:连通图
- 题目1109:连通图
- 题目1109:连通图
- 题目1109:连通图
- 题目1109:连通图
- 题目1109:连通图
- 九度oj 题目1109:连通图
- 九度 题目1109:连通图
- 九度题目1109:连通图
- 九度 题目1109:连通图
- 题目1109:连通图 九度OJ
- 九度 题目1109:连通图
- 九度OJ题目1109:连通图
- 题目68:连通图
- 【九度OJ】题目1109:连通图 解题报告
- 题目1545:奇怪的连通图
- 题目1545:奇怪的连通图
- [Lpp]MySQL服务器的用户权限管理
- 关于/var/run/docker.sock
- Javascript学习-简单测试环境
- EntityUtils.toString(entity)
- 关于select 被选中触发事件,onclick事件失效问题
- 题目1109:连通图
- Qt---log日志框架(1)
- 求最大公约数
- 【Oracle】建立索引
- applicationContext.xml文件中class中的内容不能提示
- C#函数
- CSS层模型
- @Transactional(rollbackFor=Exception.class)的作用
- 使用memmove对数组中的指定内容进行删除操作