当前位置:网站首页>HDOJ - Is It A Tree?
HDOJ - Is It A Tree?
2022-06-22 01:01:00 【WinnieJiang】
HDOJ - Is It A Tree? Answer key
This question mainly investigates “ Union checking set ” This knowledge .
Existing problems
- because HDOJ The test data of this question is weak , Online Some answers No need to search the set , Just checking the penetration can AC
- Some answers Put and search the relevant codes , But it doesn't work at all (if Judge the condition )
1、2 Our algorithm can easily find counterexamples ( A ring + A tree ), Because they do not judge connectivity or do not judge enough
- The single point case is generally not considered ( Only a single point or a graph + One point )
Pit of test data
HDOJ Incomplete input of test data on , There are test cases 0 0 It is truncated without input
The verification code is as follows :
#include <bits/stdc++.h>
using namespace std;
int rudu[100005];
int count1 = 1;
void z()
{
int f = 0;
for (int i = 1; i <= 100004; i++)
{
if (rudu[i] == 0)
f++;
if (rudu[i] > 1)
f = 2;
}
if (f == 1)
printf("Case %d is a tree.\n", count1);
else
printf("Case %d is not a tree.\n", count1);
count1++;
}
int main()
{
int aa, b;
while (cin >> aa >> b)
{
if (aa == -1 && b == -1)
break;
if (aa == 0 && b == 0)
{
printf("Case %d is a tree.\n", count1);
count1++;
continue;
}
fill(rudu, rudu + 100005, -1);
rudu[aa] = 0;
rudu[b] = 0;
if (aa != b)
rudu[b]++;
while (cin >> aa >> b)
{
if (rudu[aa] == -1)
rudu[aa] = 0;
if (rudu[b] == -1)
rudu[b] = 0;
if (aa != b)
rudu[b]++;
if (aa == 0 && b == 0)
{
z();
break;
}
}
}
}
If the 41 That's ok while Change it to :
while (1)
{
cin >> aa >> b;
Can not AC, If it's changed to :
while (1)
{
if (!(cin >> aa >> b))
break;
Then you can AC
Correct code
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
int node[maxn]; // node ( The default is 0, Indicates that the point does not exist )
int in[maxn]; // The degree of ( The default is -1, Indicates that the point does not exist )
int find(int x)
{
// Enable this point , Initialize to itself
if (node[x] == 0)
{
node[x] = x;
return x;
}
while (node[x] != x)
x = node[x];
return x;
}
void merge(int a, int b)
{
a = find(a);
b = find(b);
node[a] = b;
}
int main()
{
int a, b;
int count = 1;
while (cin >> a >> b)
{
if (a == -1 && b == -1)
break;
if (a == 0 && b == 0)
{
printf("Case %d is a tree.\n", count);
count++;
continue;
}
memset(node, 0, sizeof(node));
fill(in, in + maxn, -1);
merge(a, b);
in[a] = 0;
in[b] = 0;
if (a != b)
in[b]++;
while (cin >> a >> b)
{
merge(a, b);
if (in[a] == -1)
in[a] = 0;
if (in[b] == -1)
in[b] = 0;
if (a != b)
in[b]++;
if (a == 0 && b == 0)
{
int sum = 0;
int flag = -1;
for (int i = 1; i < maxn; i++)
{
if (node[i] == i)
sum++;
if (in[i] == 0)
flag++;
if (in[i] > 1)
flag = 1;
}
if (sum == 1 && flag == 0)
printf("Case %d is a tree.\n", count);
else
printf("Case %d is not a tree.\n", count);
count++;
break;
}
}
}
}
边栏推荐
- Is it safe to open an account for futures in Huishang futures?
- Lecture 3 of Data Engineering Series: characteristic engineering of data centric AI
- Go技术日报(2022-06-20)——Go:简单的优化笔记
- 【环境踩坑】使用FastDFS测试上传文件时报错
- 【应试技巧】格林公式记忆方法及简单推导
- Mendix公司新任CFO Tom Ellison通过领导团队转型推动公司下一阶段高速增长
- Two popular architectures for web application system development
- 数字化转型的下一个目标:提供准时制信息
- Pytorch learning 05: indexing and slicing
- cygwin下面用mysql client连接服务器的问题
猜你喜欢
随机推荐
【DailyFresh】发送激活邮件遇到的问题
【DailyFresh】课程记录3--商品搜索相关
如何让自己的网站快速被搜索引擎找到
Leetcode content
如何使用物联网低代码平台进行报表管理?
lvgl使用demo示例及说明1
Mendix公司新任CFO Tom Ellison通过领导团队转型推动公司下一阶段高速增长
高德地图--根据地理位置获取经纬度
pytorch学习03:张量数据类型和一些操作
pytorch学习09:矩阵基本运算
[2023 approved in advance] Qingdao Dingxin Technology
【环境踩坑】pycharm使用qt时报错
三种文件句柄之间的转换
Assembly language example
AcWing 第 56 場周賽
Install easyx-vc2019
Enterprises can improve database security in four ways
yolov3 3D 语义点云paper阅读
均线策略专测
答应我, 不要再用 if (obj != null) 判空了

![Chapter VIII exercises (45A) [microcomputer principles] [exercises]](/img/79/8311a409113331e72f650a83351b46.png)







