#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
vector<vector<pair<int, int>>> E(N);
for (int i = 0; i < N - 1; i++){
int X, Y;
cin >> X >> Y;
X--;
Y--;
E[X].push_back(make_pair(i, Y));
E[Y].push_back(make_pair(i, X));
}
int M;
cin >> M;
vector<int> A(M), B(M), C(M);
for (int i = 0; i < M; i++){
cin >> A[i] >> B[i] >> C[i];
A[i]--;
B[i]--;
}
vector<int> S(N - 1, 0);
for (int i = 0; i < M; i++){
vector<int> p(N, -1);
vector<int> e(N);
queue<int> Q;
Q.push(A[i]);
while (!Q.empty()){
int v = Q.front();
Q.pop();
for (auto P : E[v]){
int w = P.second;
if (w != p[v]){
p[w] = v;
e[w] = P.first;
Q.push(w);
}
}
}
int v = B[i];
while (v != A[i]){
S[e[v]] |= 1 << i;
v = p[v];
}
}
vector<vector<bool>> ok(M, vector<bool>(M, true));
for (int i = 0; i < N - 1; i++){
for (int j = 0; j < M; j++){
for (int k = j + 1; k < M; k++){
if ((S[i] >> j & 1) == 1 && (S[i] >> k & 1) == 1){
ok[j][k] = false;
}
}
}
}
int ans = 0;
for (int i = 0; i < (1 << M); i++){
bool ok2 = true;
for (int j = 0; j < M; j++){
for (int k = j + 1; k < M; k++){
if ((i >> j & 1) == 1 && (i >> k & 1) == 1 && !ok[j][k]){
ok2 = false;
}
}
}
if (ok2){
int sum = 0;
for (int j = 0; j < M; j++){
if ((i >> j & 1) == 1){
sum += C[j];
}
}
ans = max(ans, sum);
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1085 ms |
11436 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |