[HDU] [1325] Is It A Tree?

Problem Description

Loading...

Problem Analysis

This involves knowledge about trees

But at first I got stuck on the termination condition for the input data
Then I read a few articles
and roughly understood it

That is, each test case ends with 0 0
and the program terminates with -1 -1

So the idea:
When the conditions for not being a tree are met
record that it is not a tree
and just wait until all input for the current case has been read
then it can be concluded that it is not a tree
otherwise continue processing

When the input data is 0 0, check whether it is a tree
Because an empty tree is also a tree
we can initialize the flag to true

The pseudocode should be written as:
(I think this way of thinking is still acceptable)

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
bool flag=true;
int kase=0;
while(scanf("%d %d",&n,&m)!=EOF&&n>=0&&m>=0){
if(!flag&&!(n==0&&m==0))
continue;
if(n==0&&m==0){
……
if(flag)
printf("Case %d is a tree.",++kase);
else
printf("Case %d is not a tree.",++kase);
}
……
}

Forget it…
I still can’t grasp the essence of pseudocode

The conditions for being a tree:
Every node has only one node pointing to it
The roots of all nodes are the same number
and the root of this number is itself

The phrasing seems a bit nonstandard
but that’s roughly it
There are also some special cases
just consider them

But with the previous way of writing it, you’d find that the total number of nodes isn’t given
so I used an array to record them

Then I wrote it pretty quickly
but because the approach wasn’t standard
and for various other reasons
the debugging process was exceptionally difficult
and there really were cases where it passed on some OJs but not on others…

HDU is said to have cases like 1 2 3 2 0 0
With my previous judging method, that would indeed be WA
POJ is said to have inputs where n and m are equal

Anyway, this problem…
was indeed a bit difficult for me
Fortunately, I did get AC~
But I might come back and improve this code later

This is written too…
amateurishly?

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<string.h>
int par[10200];
int rank[10200];
int x[10200];
bool flag;
int find(int m) {
	if(m==par[m])
		return m;
	else
		return par[m]=find(par[m]);
}
void unite(int x,int y) {
	x=find(x);
	y=find(y);
	if(x==y)
		return;
	if(rank[x]<rank[y])
		par[x]=y;
	else {
		par[y]=x;
		if(rank[x]==rank[y])
			rank[x]++;
	}
}
void chu() {
	for(int i=1; i<10200; i++) {
		par[i]=i;
		rank[i]=0;
	}
	memset(x,0,sizeof(x));
	flag=true;
}
int main() {
	int n,m;
	int kase=0;
	chu();
	while(scanf("%d %d",&n,&m)!=EOF&&n>=0&&m>=0) {
		if(!flag&&!(n==0&&m==0))
			continue;
		if(n==0&&m==0) {
			int t;
			bool fir=true;
			for(int i=1; i<10200; i++) {
				if(!x[i])
					continue;
				if(fir) {
					t=find(i);
					fir=false;
				}
				if(find(i)!=t) {
					flag=false;
					break;
				}
			}
			if(flag&&find(t)!=t)
				flag=false;
			if(flag)
				printf("Case %d is a tree.\n",++kase);
			else
				printf("Case %d is not a tree.\n",++kase);
			chu();
		}
		if(!(n==0&&m==0)&&(n==m||(n!=m&&find(n)==find(m)))) {
			flag=false;
		} else {
			if(x[m]==2) {
				flag=false;
			} else {
				x[n]=1;
				x[m]=2;
				unite(n,m);
			}
		}
	}
	return 0;
}

Problem links:
HDU 1325 - Is It A Tree?
POJ 1308 - Is It A Tree?
HDU 1272 - Xiaoxi’s Labyrinth

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...