Problem Description
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)
| |
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?
| |
Problem links:
HDU 1325 - Is It A Tree?
POJ 1308 - Is It A Tree?
HDU 1272 - Xiaoxi’s Labyrinth