Go to diff to previous submission
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int conn[2000][2000]; int pipes[2000]; int nodes; int children[2000][2000]; int childcount[2000]; int count(int node, int from) { int ii, i, inputc= 1000000000, childc = 0; //printf("count node %d from %d\n", node, from); if (pipes[node]==1 && from != -1) { //printf("leaf %d\n", node); return conn[node][from]; } // for (i=1; i<nodes+1; i++) { for (ii=0; ii<childcount[node]; ii++) { i = children[node][ii]; if (i==from) { inputc = conn[node][i]; } else { //childc += conn[node][i]; if (conn[node][i] > 0) childc += count(i, node); } } //printf("node %d from %d input %d child %d\n", node, from, inputc, childc); if (childc == 0) return inputc; return childc > inputc ? inputc : childc; } void addchild(int n1, int n2) { children[n1][childcount[n1]++] = n2; children[n2][childcount[n2]++] = n1; } int main(int argc, char **argv) { int rv, root, i, n1, n2, w; while (1) { if (rv != 2) break; for (i=0; i<nodes-1; i++) { conn[n1][n2] = w; conn[n2][n1] = w; pipes[n1]++; pipes[n2]++; addchild(n1, n2); } } return 0; }
--- c5.s880.cteam089.fr.c.0.fr.c +++ c5.s890.cteam089.fr.c.0.fr.c @@ -7,7 +7,9 @@ int pipes[2000]; int nodes; +int children[2000][2000]; +int childcount[2000]; int count(int node, int from) { - int i, inputc= 1000000000, childc = 0; + int ii, i, inputc= 1000000000, childc = 0; //printf("count node %d from %d\n", node, from); if (pipes[node]==1 && from != -1) { @@ -15,5 +17,7 @@ return conn[node][from]; } - for (i=1; i<nodes+1; i++) { +// for (i=1; i<nodes+1; i++) { + for (ii=0; ii<childcount[node]; ii++) { + i = children[node][ii]; if (i==from) { inputc = conn[node][i]; @@ -28,4 +32,10 @@ } +void addchild(int n1, int n2) +{ + children[n1][childcount[n1]++] = n2; + children[n2][childcount[n2]++] = n1; +} + int main(int argc, char **argv) { @@ -36,4 +46,5 @@ memset(conn, 0, sizeof(conn)); memset(pipes, 0, sizeof(pipes)); + memset(childcount, 0, sizeof(childcount)); for (i=0; i<nodes-1; i++) { scanf("%d%d%d", &n1, &n2, &w); @@ -42,4 +53,5 @@ pipes[n1]++; pipes[n2]++; + addchild(n1, n2); } printf("%d\n", count(root, -1));