当前位置:网站首页>(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 gets the default ringtone selection list [article]
- 无向图的桥
- Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
- Partner cloud form strong upgrade! Pro version, more extraordinary!
- Why is the default of switch followed by break?
- We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
- [unity] using GB2312, the solution to abnormal program after packaging
- Unity skframework framework (XVI), package manager development kit Manager
- The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
- Unity SKFramework框架(十四)、Extension 扩展函数
猜你喜欢
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
Unity SKFramework框架(十四)、Extension 扩展函数
[OpenGL] notes 29. Advanced lighting (specular highlights)
最近公共祖先LCA的三种求法
Essential for operation and maintenance - Elk log analysis system
Unity skframework framework (XX), VFX lab special effects library
Redis database persistence
Research shows that "congenial" is more likely to become friends
mac(macos Monterey12.2 m1) 个人使用php开发
题解:《压缩技术》(原版、续集版)
随机推荐
2、 Frame mode MPLS operation
Quantum three body problem: Landau fall
Essential for operation and maintenance - Elk log analysis system
A better database client management tool than Navicat
Unity skframework framework (XXI), texture filter map resource filtering tool
二、帧模式 MPLS 操作
PR usage skills, how to use PR to watermark?
Partner cloud form strong upgrade! Pro version, more extraordinary!
JS reverse row query data decryption
Embedded software development
linux下清理系统缓存并释放内存
[Unity]使用GB2312,打包后程序不正常解决方案
Why is the default of switch followed by break?
[youcans' image processing learning course] general contents
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
leetcode621. 任务调度器
D如何检查null
Clean up system cache and free memory under Linux
MySQL: Invalid GIS data provided to function st_ geometryfromtext
Pocket Raider comments