#include<stdio.h>
#define MAX 100005
#define NIL -1
   
struct Node{
    int p;
    int l;
    int r;
};
struct Node np[MAX];
int depth = 0;
int hileft = 0;
int hiright = 0;
   
   
int get_depth(int u){
   
    if(np[u].p == NIL) return 0;
    if(np[u].p != NIL){
        depth++;
        get_depth(np[u].p);
    }
    return depth;
}
   
int degree(int u){
    if (np[u].l == NIL && np[u].r == NIL) return 0;
    else if(np[u].l != NIL && np[u].r != NIL)  return 2;
    else  return 1;
}
   
int sibling(int u){
   
    if(np[u].p == NIL) {
        return NIL;
    }
    else if(np[np[u].p].l == u) {
        return np[np[u].p].r;
    }
    else {
        return np[np[u].p].l;
    }
}
   
int hight(int u){
   
    int hileft = 0;
    int hiright = 0;
   
    if(np[u].l != -1) {
        hileft = hight(np[u].l) + 1;
    }
    if(np[u].r != -1){
        hiright = hight(np[u].r) + 1;
    }
    if(hileft < hiright){
        return hiright;
    }
    else {
        return hileft;
    }
}
   
void print(int u){
   
    printf("node %d: ",u);
    printf("parent = %d, ",np[u].p);
    printf("sibling = %d, ",sibling(u));
    printf("degree = %d, ",degree(u));
   
    depth = 0;
   
    printf("depth = %d, ",get_depth(u));
    printf("height = %d, ",hight(u));
   
    if(np[u].p == NIL)printf("root\n");
    else if(np[u].l == NIL && np[u].r == NIL) {
        printf("leaf\n");
    }
    else {
        printf("internal node\n");
    }
}
   
   
int main(){
    int i, j;
    int d, v, c, l;
    int n,tmp;
   
    scanf("%d", &n);
   
    for (i = 0; i < n; i++) {
        np[i].p = np[i].l = np[i].r = NIL;      // ?????????
    }
    for (i = 0; i <n ; i++){
        scanf("%d %d %d", &v, &d, &c);
        np[d].p = np[c].p = v;
        np[v].l = d;
        np[v].r = c;
    }
   
    for (i = 0; i < n; i++ ) {
        print(i);
    }
    return 0;
}

Compile Error Logs:
You are not authorized to see the message.

???????????????
Judge: 8/8	C	CPU: 00.00 sec	Memory: 612 KB	Length: 1847 B	2015-06-11 14:28	2015-06-11 14:28
???????????±?????????????????????
Case #	Verdict	CPU Time	Memory	In	Out	Case Name
Case #1:	: Accepted	00.00 sec	612 KB	38 B	376 B	1.in
Case #2:	: Accepted	00.00 sec	612 KB	10 B	75 B	2.in
Case #3:	: Accepted	00.00 sec	612 KB	31 B	315 B	3.in
Case #4:	: Accepted	00.00 sec	612 KB	52 B	531 B	4.in
Case #5:	: Accepted	00.00 sec	612 KB	66 B	686 B	5.in
Case #6:	: Accepted	00.00 sec	612 KB	52 B	553 B	6.in
Case #7:	: Accepted	00.00 sec	612 KB	164 B	1594 B	7.in
Case #8:	: Accepted	00.00 sec	608 KB	199 B	1955 B	8.in

< prev |	1	/	8	| next >	 		 : Accepted        00.00 sec        612 KB
 
Judge Input #1 ( in1.txt | 38 B)		Judge Output #1 ( out1.txt | 376 B)
5
3 4 0
4 -1 -1
1 -1 -1
2 -1 -1
0 1 2
node 0: parent = 3, sibling = 4, degree = 2, depth = 1, height = 1, internal node
node 1: parent = 0, sibling = 2, degree = 0, depth = 2, height = 0, leaf
node 2: parent = 0, sibling = 1, degree = 0, depth = 2, height = 0, leaf
node 3: parent = -1, sibling = -1, degree = 2, depth = 0, height = 2, root
node 4: parent = 3, sibling = 0, degree = 0, depth = 1, height = 0, leaf
Comments
 
 ??\????????§??????
 
Categories
 
 
Free Tags
 
 