7 // cheat - pre-examined input to learn max name length (7+NUL), children (7)
11 typedef struct node node
;
16 char childname
[MAXCHILD
][MAXNAME
];
17 node
*next
; // global list form
18 node
*children
[MAXCHILD
]; // tree form
23 static int compute(node
*n
)
25 for (int i
= 0; i
< n
->nchildren
; i
++)
26 for (node
*iter
= list
; iter
; iter
= iter
->next
)
27 if (!strcmp(n
->childname
[i
], iter
->name
)) {
28 n
->children
[i
] = iter
;
29 n
->total
+= compute(iter
);
32 if (n
->nchildren
&& n
->total
!= n
->children
[0]->total
* n
->nchildren
) {
33 printf("node %s is bad, with %d children\n", n
->name
, n
->nchildren
);
34 for (int i
= 0; i
< n
->nchildren
; i
++) {
35 int w1
= n
->children
[(i
+1) % n
->nchildren
]->total
;
36 int w2
= n
->children
[(i
+2) % n
->nchildren
]->total
;
38 printf(" child %d: %s %d %d vs. %d %d\n", i
, n
->children
[i
]->name
,
39 n
->children
[i
]->weight
, n
->children
[i
]->total
, w1
, w2
);
40 if (n
->children
[i
]->total
!= w1
&& n
->children
[i
]->total
!= w2
) {
41 printf("fix by adjusting %s weight to %d\n", n
->children
[i
]->name
,
42 n
->children
[i
]->weight
- n
->children
[i
]->total
+ w1
);
47 n
->total
+= n
->weight
;
54 // ugly, but works because our data is sane
56 node
*item
= calloc(sizeof *item
, 1);
57 item
->nchildren
= scanf(" %8[a-z] (%d) -> %8[a-z], %8[a-z], %8[a-z], "
58 "%8[a-z], %8[a-z], %8[a-z], %8[a-z]",
59 item
->name
, &item
->weight
, item
->childname
[0],
60 item
->childname
[1], item
->childname
[2],
61 item
->childname
[3], item
->childname
[4],
62 item
->childname
[5], item
->childname
[6]) - 2;
63 if (item
->nchildren
< 0) {
68 printf("%s (%d) -> %d: %s %s %s %s %s %s %s\n", item
->name
, item
->weight
,
69 item
->nchildren
, item
->childname
[0], item
->childname
[1],
70 item
->childname
[2], item
->childname
[3], item
->childname
[4],
71 item
->childname
[5], item
->childname
[6]);
76 printf("parsed %d lines\n", count
);
81 for (node
*iter
= list
; iter
; iter
= iter
->next
)
82 for (int i
= 0; i
< iter
->nchildren
; i
++)
83 if (!strcmp(root
->name
, iter
->childname
[i
])) {
86 printf("%s is parent of %s\n", iter
->name
, root
->name
);
90 printf("root is %s\n", root
->name
);
91 printf("root weight is %d\n", compute(root
));