Submission #206662

#TimeUsernameProblemLanguageResultExecution timeMemory
206662gratus907두 로봇 (KOI18_robot)C++17
0 / 100
207 ms64632 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...