当前位置:网站首页>(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;
}边栏推荐
- 2022 Heilongjiang provincial examination on the writing skills of Application Essays
- 上海交大教授:何援军——包围盒(包容体/包围盒子)
- Android kotlin broadcast technology point
- ADB basic commands
- 2、 Frame mode MPLS operation
- De4000h storage installation configuration
- 题解:《压缩技术》(原版、续集版)
- Don't spend money, spend an hour to build your own blog website
- Unity SKFramework框架(十二)、Score 计分模块
- JS逆向之巨量创意signature签名
猜你喜欢
![Jerry's watch time synchronization [chapter]](/img/64/a48772b4e503ae0a2d36fc292e4e0d.jpg)
Jerry's watch time synchronization [chapter]

Web Foundation

leetcode621. 任务调度器

net share

de4000h存储安装配置

Unity SKFramework框架(十四)、Extension 扩展函数

【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解

Unity skframework framework (XVI), package manager development kit Manager

为什么switch 的default后面要跟break?

屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
随机推荐
Daily practice of C language --- monkeys divide peaches
Jerry's weather code table [chapter]
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
Verification failed, please check your call back website. You can follow the instructions
Security RememberMe原理分析
验证失败,请检查您的回电网址。您可以按照指导进行操作
Unity SKFramework框架(十四)、Extension 扩展函数
JS reverse massive creative signature
Unity skframework framework (XIV), extension extension function
Answer: can the audio be set to on by default during easydss video on demand?
Pocket Raider comments
Error function ERF
最近公共祖先LCA的三种求法
不会看器件手册的工程师不是个好厨子
[cloud native database] what to do when encountering slow SQL (Part 1)?
What are eNB, EPC and PGW?
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
Word efficiency guide - word's own template
Finally, someone explained the supervised learning clearly
Unity SKFramework框架(十六)、Package Manager 開發工具包管理器