# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
206663 |
2020-03-04T11:56:23 Z |
gratus907 |
None (KOI18_robot) |
C++17 |
|
186 ms |
62860 KB |
#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 {par[x][0], 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
62840 KB |
Output is correct |
2 |
Incorrect |
141 ms |
62840 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
59896 KB |
Output is correct |
2 |
Incorrect |
138 ms |
62840 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
3320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
59936 KB |
Output is correct |
2 |
Correct |
186 ms |
59896 KB |
Output is correct |
3 |
Correct |
170 ms |
62856 KB |
Output is correct |
4 |
Incorrect |
146 ms |
62860 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |