Can a tree be formed .. How does it feel like graph theory? Put it in the string topic.
<Code>
# Include <stdio. h>
# Include <iostream>
# Include <string. h>
# Define PR printf
Using namespace STD;
Int main ()
{
Int I, J, K, M, N, T, CA, E, V, indeg, zero, root;
Int num, a [2] [20], S, T, p, q;
Int d [20] [20], degin [20], Degout [20], have [20];
CA = 0;
While (1)
{
For (I = 1; I <20; I ++)
{
Degin [I] = Degout [I] = 0; // inbound and outbound
Have [I] = 0;
For (j = 1; j <20; j ++)
D [I] [J] = 0; // I-> J edge
}
Scanf ("% d", & I, & J );
If (I <0 & J <0) break;
CA ++; E = 0; V = 0; // Number of e edge entries, number of V points
While (1)
{
If (I = 0 & J = 0) break;
E ++;
D [I] [J] = 1;
Degout [I] ++;
Degin [J] ++;
If (have [I] = 0) V ++; // number of points
If (have [J] = 0) V ++;
Have [I] = 1;
Have [J] = 1;
Scanf ("% d", & I, & J );
}
If (E = 0) {PR ("case % d is a tree./N", CA); continue ;}
If (E! = V-1) {PR ("case % d is not a tree./N", CA); continue ;}
Zero = 0; indeg = 0; root = 0;
For (I = 1; I <20; I ++)
{
If (have [I])
{
If (degin [I] = 0) {zero ++; root = I;} // find the root node
If (degin [I]> 1) indeg = 2; // more than one point pointing to I, not a tree
// PR ("I = % d in = % d out = % d/N", I, degin [I], Degout [I]);
}
}
If (zero! = 1 | indeg = 2) {PR ("case % d is not a tree./N", CA); continue ;}
Num = 1;
A [0] [1] = root;
S = 0;
P = 0; q = 1; t = 1;
While (1)
{
P = 0;
For (I = 1; I <= Q; I ++)
{
For (j = 1; j <20; j ++)
{
If (d [A [s] [I] [J] = 1) {P ++; A [T] [p] = J ;}
}
}
If (P = 0) break; // All are leaves
Q = P ;//
Num + = P; // record the number of points added to this tree
S = 1-s; t = 1-T;
}
If (num = V) {PR ("case % d is a tree./N", CA); continue ;}
Else {PR ("case % d is not a tree./N", CA); continue ;}
}
Return 0;
}