当前位置:网站首页>(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;
}边栏推荐
- 科技的成就(二十七)
- 日本赌国运:Web3.0 ,反正也不是第一次失败了!
- 研究表明“气味相投”更易成为朋友
- Fundamentals of machine learning (II) -- division of training set and test set
- 验证失败,请检查您的回电网址。您可以按照指导进行操作
- OpenFOAM:lduMatrix&lduAddressing
- [Unity]使用GB2312,打包后程序不正常解决方案
- Unity SKFramework框架(十四)、Extension 扩展函数
- Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
- JS reverse row query data decryption
猜你喜欢

Can automatically update the universal weekly report template, you can use it with your hand!

Research shows that "congenial" is more likely to become friends
![[indomitable medal activity] life goes on and writing goes on](/img/c1/54e3f1b37db25af3f1998b39da301b.png)
[indomitable medal activity] life goes on and writing goes on

不会看器件手册的工程师不是个好厨子

上海交大教授:何援军——包围盒(包容体/包围盒子)

屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
![2022 zero code / low code development white paper [produced by partner cloud] with download](/img/46/92c51090e0c476df3bcffd2d11fb6d.png)
2022 zero code / low code development white paper [produced by partner cloud] with download
![[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术](/img/a7/44609a5acf25021f1fca566c3d8c90.png)
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术

De4000h storage installation configuration

Daily practice of C language --- monkeys divide peaches
随机推荐
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
[unity] using GB2312, the solution to abnormal program after packaging
EasyDSS点播服务分享时间出错如何修改?
[indomitable medal activity] life goes on and writing goes on
Essential for operation and maintenance - Elk log analysis system
Unity SKFramework框架(十三)、Question 问题模块
Engineers who can't read device manuals are not good cooks
ADB basic commands
How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
Jerry's watch gets the default ringtone selection list [article]
Astro learning notes
numpy数组计算
[Unity]使用GB2312,打包后程序不正常解决方案
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
【云原生数据库】遇到慢SQL该怎么办(上)?
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
能自动更新的万能周报模板,有手就会用!
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script