当前位置:网站首页>(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;
}边栏推荐
- Jerry's watch ringtone audition [article]
- Unity skframework framework (XIV), extension extension function
- 2、 Frame mode MPLS operation
- [OpenGL] notes 29. Advanced lighting (specular highlights)
- Fundamentals of machine learning (II) -- division of training set and test set
- JS generates 4-digit verification code
- Redis database persistence
- [true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
- [unity] using GB2312, the solution to abnormal program after packaging
- 题解《子数整数》、《欢乐地跳》、《开灯》
猜你喜欢

Chinese name extraction (toy code - accurate head is too small, right to play)

2022零代码/低代码开发白皮书【伙伴云出品】附下载

Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China

Node. JS accessing PostgreSQL database through ODBC
![Jerry's watch delete alarm clock [chapter]](/img/7f/d51b37872b4ce905a0a723a514b2dc.jpg)
Jerry's watch delete alarm clock [chapter]

Unity SKFramework框架(十二)、Score 计分模块

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

(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software

OpenFOAM:lduMatrix&lduAddressing

Research shows that "congenial" is more likely to become friends
随机推荐
机器学习基础(二)——训练集和测试集的划分
Unity SKFramework框架(十三)、Question 问题模块
Everyone wants to eat a broken buffet. It's almost cold
【youcans 的图像处理学习课】总目录
口袋奇兵点评
互联网常见34个术语解释
[indomitable medal activity] life goes on and writing goes on
Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
Principle analysis of security rememberme
SSL证书的分类有哪些?如何选择合适的SSL证书?
de4000h存储安装配置
Unity skframework framework (XIII), question module
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
Nohup command
二、帧模式 MPLS 操作
Research shows that "congenial" is more likely to become friends
Jerry's watch stops ringing [article]
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
linux下清理系统缓存并释放内存