第三题,部分代码如下,只通过了10%的测试用例,自己写了几个例子, 还是没发现问题在哪,希望大家指点。 int val[5000][3], relation[5000]; vector<int> records(5000, -1); int n; ll maxHappy = -1, happy = -1; cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < 3; ++j) cin >> val[i][j]; for (int i = 1; i < n; ++i) { // 1-n int a, b; cin >> a >> b; relation[b] = a; } for (int j = 0; j < 3; ++j) { records[0] = j; happy = val[0][j]; for (int i = 1; i < n; ++i) { int tmpMax = -1; for (int k = 0; k < 3; ++k) { if (k == j || k == records[relation[i]]) continue; if (val[i][k] > tmpMax) { records[i] = k; tmpMax = val[i][k]; } } // printf("%d-%d: %d\n", j, i, tmpMax); happy += tmpMax; } if (happy > maxHappy) maxHappy = happy; }