#include <bits/stdc++.h>
using namespace std;
#define ll long long
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define usecppio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(x) ((x).begin()),((x).end())
#define eps 1e-7
#define int ll
using pii = pair <int, int>;
const int mod = 1000000007;
int n, a, b;
bool visited[101010];
int par[101010][21], maxedge[101010][21], minedge[101010][21];
int d[101010];
vector <pii> graph[101010]; // {destination, weight}
void dfs(int here,int depth) // run dfs(root,0)
{
visited[here] = true;
d[here] = depth;
for (auto there : graph[here])
{
if (visited[there.first])
continue;
dfs(there.first, depth + 1);
par[there.first][0] = here;
maxedge[there.first][0] = there.second;
minedge[there.first][0] = there.second;
}
}
void precomputation()
{
for (int i = 1; i<21; i++)
{
for (int j = 1; j<=n; j++)
{
par[j][i] = par[par[j][i-1]][i-1];
maxedge[j][i] = max(maxedge[j][i - 1],
maxedge[par[j][i - 1]][i - 1]);
minedge[j][i] = min(minedge[j][i - 1],
minedge[par[j][i - 1]][i - 1]);
}
}
}
pii lca(int x, int y)
{
int maxlen = INT_MIN;
int minlen = INT_MAX;
if (d[x]>d[y])
swap(x,y);
for (int i = 20; i>=0; i--)
{
if (d[y]-d[x] >= (1<<i))
{
minlen = min(minlen,minedge[y][i]);
maxlen = max(maxlen,maxedge[y][i]);
y = par[y][i];
}
}
if (x==y)
return {x, maxlen};
for (int i = 20; i>=0; i--)
{
if (par[x][i] != par[y][i])
{
minlen = min(minlen,min(minedge[x][i],minedge[y][i]));
maxlen = max(maxlen,max(maxedge[x][i],maxedge[y][i]));
x = par[x][i];
y = par[y][i];
}
}
minlen = min(minlen,min(minedge[x][0],minedge[y][0]));
maxlen = max(maxlen,max(maxedge[x][0],maxedge[y][0]));
int lca_point = par[x][0];
return {lca_point, maxlen};
}
void tobedone()
{
dfs(1,0);
precomputation();
}
int dt[101010];
bool vst[101010];
int32_t main()
{
usecppio
cin >> n;
cin >> a >> b;
for (int i = 1; i<n; i++)
{
int u, v, w;
cin >> u >> v >> w;
graph[u].push_back({v,w});
graph[v].push_back({u,w});
}
tobedone();
queue <int> q;
dt[1] = 0;
vst[1] = true;
q.push(1);
while(!q.empty())
{
int x = q.front();
q.pop();
for (auto it:graph[x])
{
if (!vst[it.first])
{
q.push(it.first);
dt[it.first] = dt[x]+it.second;
vst[it.first] = true;
}
}
}
cout << dt[a] + dt[b] - 2*dt[lca(a, b).first]-lca(a, b).second;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
64632 KB |
Output is correct |
2 |
Incorrect |
144 ms |
64376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
61304 KB |
Output is correct |
2 |
Correct |
144 ms |
64248 KB |
Output is correct |
3 |
Correct |
7 ms |
3320 KB |
Output is correct |
4 |
Correct |
7 ms |
3448 KB |
Output is correct |
5 |
Correct |
33 ms |
12152 KB |
Output is correct |
6 |
Correct |
22 ms |
12536 KB |
Output is correct |
7 |
Incorrect |
7 ms |
2684 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
3320 KB |
Output is correct |
2 |
Correct |
7 ms |
3320 KB |
Output is correct |
3 |
Correct |
8 ms |
3576 KB |
Output is correct |
4 |
Incorrect |
6 ms |
2684 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
61560 KB |
Output is correct |
2 |
Correct |
207 ms |
61320 KB |
Output is correct |
3 |
Correct |
142 ms |
64376 KB |
Output is correct |
4 |
Incorrect |
144 ms |
64376 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |