#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,q;
int p[20][50001],d[50001],level[50001];
vector<pii> adj[50001];
/*
void dfs(int x, int par) {
p[x][0]=par;
level[x]=level[par]+1;
for (int i=1; i<=16; ++i) {
p[x][i]=p[p[x][i-1]][i-1];
}
for (auto s : adj[x]) {
if (s.first==par) continue;
d[s.first]=d[x]+s.second;
dfs(s.first,x);
}
} */
int lca(int x, int y) {
if (level[x]<level[y]) swap (x,y);
int dif=level[x]-level[y];
for (int i=0; i<=16; ++i) {
if (dif&(1<<i)) x=p[x][i];
}
if (x==y) return x;
for (int i=16; i>=0; --i) {
if (p[x][i]!=p[y][i]) {
x=p[x][i];
y=p[y][i];
}
}
return p[x][0];
}
int dis(int x, int y) {
return d[x]+d[y]-2*d[lca(x,y)];
}
void solve() {
int a[5],ans;
for (int i=1; i<=5; ++i) cin>>a[i];
/*
ans=dis(a[1],a[2]), top=lca(a[1],a[2]);
for (int i=3; i<=5; ++i) {
bool have=false;
for (int j=1; j<i; ++j) {
for (int k=j+1; k<i; ++k) {
if (dis(a[j],a[k])==dis(a[j],a[i])+dis(a[i],a[k])) have=true;
}
}
if (!have) {
int mmin=INT_MAX;
for (int j=1; j<i; ++j) {
if (lca(a[i],a[j])==a[j]) {
mmin=min(ans,dis(a[i],a[j]));
have=true;
break;
}
}
if (have) ans+=mmin;
}
if (!have) {
if (level[lca(a[i],a[1])]<level[top]) {
ans+=dis(top,a[i]);
top=lca(a[i],a[1]);
have=true;
}
}
if (!have) {
int mindis=INT_MAX;
for (int j=1; j<i; ++j) {
mindis=min(mindis,dis(a[i],lca(a[i],a[j])));
}
ans+=mindis;
}
} */
cout<<ans<<"\n";
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for (int i=0; i<n-1; ++i) {
int a,b,w;
cin>>a>>b>>w;
adj[a].push_back(pii(b,w));
adj[b].push_back(pii(a,w));
}
cin>>q;
while (q--) solve();
}
Compilation message
roadsideadverts.cpp: In function 'void solve()':
roadsideadverts.cpp:88:16: warning: 'ans' is used uninitialized in this function [-Wuninitialized]
88 | cout<<ans<<"\n";
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
3084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
3252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |