当前位置:网站首页>HDOJ - Is It A Tree?
HDOJ - Is It A Tree?
2022-06-21 23:53:00 【WinnieJiang】
该题主要考察“并查集”这一知识点。
现存问题
1、2 的算法能很容易找到反例(一个环+一个树),因为他们没有对连通性进行判断或判断不足
- 普遍没有考虑单点情况(仅单点或一个图+一个点)
测试数据的坑
HDOJ上的测试数据存在输入不完全的情况,即存在测试用例0 0都没输入就截断了
验证代码如下:
#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;
}
}
}
}
若将41行while改为:
while (1)
{
cin >> aa >> b;
则不能AC,若改为:
while (1)
{
if (!(cin >> aa >> b))
break;
则可以AC
正确代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
int node[maxn]; // 节点(默认为0, 表示点不存在)
int in[maxn]; // 入度(默认为-1, 表示点不存在)
int find(int x)
{
// 启用该点, 初始化为自身
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;
}
}
}
}
边栏推荐
- Div set scrolling and monitor scrolling distance
- Document.readyState 如何使用和侦听
- [2023 approval in advance] China Singapore SECCO
- Go Technology Daily (June 20, 2022) -- go: simple optimization notes
- 怎么读一篇论文
- Error in jsonobject getting date type (getsqldate)
- The data magician tells you how far is the integer programming copt5.0 from CPLEX?
- 答应我, 不要再用 if (obj != null) 判空了
- 版本动态 | Exchangis 1.0.0-RC1 版本发布
- AcWing 第 56 場周賽
猜你喜欢

花了2小时,搭建了一个物联网项目,值了 ~

滴滴工程效能平台建设之路

Meetup03期回顾:Linkis新版本介绍以及DSS的应用实践

How the conductive slip ring works

比特運算比特與

pytorch学习12:自动求导

Tom Ellison, the new CFO of mendix, promoted the next stage of rapid growth of the company through the transformation of the leadership team

Ns32f103vbt6 hardware and software replace stm32f103vbt6

小小协议大威力,数字化转型为何缺不了NVMe全闪存?

It took 2 hours to build an Internet of things project, which is worth~
随机推荐
pytorch学习06:Tensor维度变换
The tangled truth about NFT and copyright
MySQL 8.0 新特性梳理汇总
Leetcode 279. Carré parfait carré complet (moyen)
0x00007ffff3d3ecd0 in _IO_vfprintf_internal (s=0x7ffff40b5620 <_IO_2_1_stdout_>
Eslint: error
find 查找不同扩展名的文件
位运算位或
Introduction and use of pytest fixture, confitest and mark
Copy and paste in terminal, 0~ and 1~ characters are added
mysql数据库高版本 低版本
Lecture 3 of Data Engineering Series: characteristic engineering of data centric AI
唐太宗把微服务的“心跳机制”玩到了极致!
如何使用物联网低代码平台进行报表管理?
怎么读一篇论文
The importance of rational selection of seal clearance of hydraulic slip ring
小小协议大威力,数字化转型为何缺不了NVMe全闪存?
合理选择液压滑环密封间隙的重要性
[examination skills] memory method and simple derivation of Green formula
Bit operation bit or