当前位置:网站首页>(POJ - 1308)Is It A Tree? (tree)
(POJ - 1308)Is It A Tree? (tree)
2022-07-02 13:38:00 【AC__ dream】
Topic link :1308 -- Is It A Tree?
The question : Multi group input , Give you some edges every time ,(0,0) Is the end of each output flag ,(-1,-1) It is the end mark of the title output , Finally, judge whether the given side is a tree .
This problem is easy to make mistakes , An empty node is also a tree , That is, direct input 0,0 It's just a tree , The second is that nodes are not necessarily continuous , That is to say, the node given to you is 10, But not necessarily 9 This node , So we need to record which nodes appear in the process , Nothing else , Just use the union set to judge
Here is the code :
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=10003;
int fu[N];
bool vis[N];
int find(int x)
{
if(x==fu[x]) return fu[x];
return fu[x]=find(fu[x]);
}
int main()
{
int T=0;
int u,v;
while(scanf("%d%d",&u,&v)&&(u!=-1||v!=-1))
{
if(u==0&&v==0)
{
printf("Case %d is a tree.\n",++T);
continue;
}
u++;v++;
for(int i=1;i<=5001;i++)
fu[i]=i,vis[i]=false;
int mx=0,cnt=1;//cnt Record edges ,mx Record the number of different nodes in the tree
if(!vis[u]) vis[u]=true,mx++;
if(!vis[v]) vis[v]=true,mx++;
bool flag=true;
int f1=find(u),f2=find(v);
if(f1==f2) flag=false;
else fu[f2]=f1;
while(1)
{
scanf("%d%d",&u,&v);
if(u==0&&v==0) break;
u++;v++;
if(!vis[u]) vis[u]=true,mx++;
if(!vis[v]) vis[v]=true,mx++;
f1=find(u),f2=find(v);
if(f1==f2) flag=false;
else fu[f2]=f1;
cnt++;
}
if(!flag||cnt<mx-1)
printf("Case %d is not a tree.\n",++T);
else
printf("Case %d is a tree.\n",++T);
}
return 0;
}
边栏推荐
- 互联网常见34个术语解释
- Achievements in science and Technology (27)
- Fundamentals of machine learning (II) -- division of training set and test set
- We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
- Record idea shortcut keys
- Mysql常用命令详细大全
- Let juicefs help you with "remote backup"
- 文件的下载与图片的预览
- Find love for speed in F1 delta time Grand Prix
- Jerry's watch gets the default ringtone selection list [article]
猜你喜欢
OpenFOAM:lduMatrix&lduAddressing
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
[OpenGL] notes 29. Advanced lighting (specular highlights)
Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
上海交大教授:何援军——包围盒(包容体/包围盒子)
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
Can automatically update the universal weekly report template, you can use it with your hand!
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
Partner cloud form strong upgrade! Pro version, more extraordinary!
Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
随机推荐
Mysql常用命令详细大全
嵌入式软件开发
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
Unity skframework framework (XIV), extension extension function
nohup命令
伙伴云表格强势升级!Pro版,更非凡!
能自动更新的万能周报模板,有手就会用!
二、帧模式 MPLS 操作
[OpenGL] notes 29. Advanced lighting (specular highlights)
Lucky numbers in the [leetcode daily question] matrix
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
Find love for speed in F1 delta time Grand Prix
【云原生数据库】遇到慢SQL该怎么办(上)?
Performance optimization of memory function
Fundamentals of face recognition (facenet)
Unity SKFramework框架(十九)、POI 兴趣点/信息点
Chinese name extraction (toy code - accurate head is too small, right to play)
【youcans 的图像处理学习课】总目录
Jerry's watch ringtone audition [article]
Nohup command