#include <bits/stdc++.h>
using namespace std;
int n, u, v, w, a, b, c, d, e, q, vistos, ans;
vector <pair <int, int>> grafo[50005];
bool visto[50005];
int dfs(int nodo, int suma){
if (nodo==a || nodo==b || nodo==c || nodo==d || nodo==e) {
vistos++;
ans+=max(suma, 0);
suma=0;
}
visto[nodo]=true;
for (auto i:grafo[nodo]) if (visto[i.first]==false) dfs(i.first, suma+i.second);
return suma=-100000;
}
void SOLVE(){
cin >> n;
for (int i=0; i<n-1; i++){
cin >> u >> v >> w;
grafo[u].push_back({v, w});
grafo[v].push_back({u, w});
}
cin >> q;
while (q--){
cin >> a >> b >> c >> d >> e;
memset(visto, false, sizeof visto);
vistos=0;
ans=0;
dfs(a, 0);
cout << ans << "\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
//cin >> t;
while(t--){
SOLVE();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1066 ms |
5436 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
127 ms |
3396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Execution timed out |
1066 ms |
5436 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |