Source code for submission s708

fr.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. int conn[2000][2000];
  7. int nodes;
  8.  
  9. int count(int node, int from) {
  10. int i, inputc= 1000000000, childc = 0;
  11. //printf("count node %d from %d\n", node, from);
  12. for (i=1; i<nodes+1; i++) {
  13. if (i==from) {
  14. inputc = conn[node][i];
  15. } else {
  16. //childc += conn[node][i];
  17. if (conn[node][i] > 0) childc += count(i, node);
  18. }
  19. }
  20. //printf("node %d from %d input %d child %d\n", node, from, inputc, childc);
  21. if (childc == 0) return inputc;
  22. return childc > inputc ? inputc : childc;
  23. }
  24.  
  25. int main(int argc, char **argv)
  26. {
  27. int rv, root, i, n1, n2, w;
  28. while (1) {
  29. rv = scanf("%d%d", &nodes, &root);
  30. if (rv != 2) break;
  31. memset(conn, 0, sizeof(conn));
  32. for (i=0; i<nodes-1; i++) {
  33. scanf("%d%d%d", &n1, &n2, &w);
  34. conn[n1][n2] = w;
  35. conn[n2][n1] = w;
  36. }
  37. printf("%d\n", count(root, -1));
  38. }
  39. return 0;
  40. }
  41.