# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
229201 | DodgeBallMan | Evacuation plan (IZhO18_plan) | C++14 | 646 ms | 63600 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pii pair<long long, long long>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
long long n, m, dp[21][2*N], d[2*N], mn[2*N], dep[2*N], k, Q, par[2*N], cnt;
vector<long long> t[2*N], pred;
vector<pii> g[N], all;
priority_queue<pii> q;
void dijk() {
while( !q.empty() ) {
pii temp = q.top(); q.pop();
for( pii i : g[temp.y] ) {
int v = i.x, di = i.y - temp.x;
if( di < d[v] ) { d[v] = di; q.emplace( -1*di, v ); }
}
}
}
void dfs( int u, int p ) {
dp[0][u] = p;
dep[u] = dep[p] + 1;
for( int i = 1 ; i < 20 ; i++ ) dp[i][u] = dp[i-1][dp[i-1][u]];
for( int v : t[u] ) if( v != p ) dfs( v, u );
}
int lca( int a, int b ) {
if( dep[a] < dep[b] ) swap( a, b );
for( int i = 19 ; i >= 0 ; i-- ) if( dep[dp[i][a]] >= dep[b] ) a = dp[i][a];
if( a == b ) return a;
for( int i = 19 ; i >= 0 ; i-- ) if( dp[i][a] != dp[i][b] ) a = dp[i][a], b = dp[i][b];
return dp[0][a];
}
int findroot( int a ) {
if( a == par[a] ) return a;
else return par[a] = findroot( par[a] );
}
int main()
{
scanf("%lld %lld",&n,&m);
for( int i = 0, a ; i < m ; i++ ) {
long long b, c;
scanf("%d %lld %lld",&a,&b,&c);
g[a].emplace_back( pii( b, c ) ), g[b].emplace_back( pii( a, c ) );
}
scanf("%lld",&k);
fill( d, d+2*N, 1e17 );
for( int i = 0 ; i < k ; i++ ) {
long long a;
scanf("%lld",&a);
d[a] = 0;
q.emplace( 0, a );
}
dijk();
//printf("HEY");
for( int i = 1 ; i <= n ; i++ ) all.emplace_back( pii( d[i], i ) );
sort( all.begin(), all.end(), greater<pii>() );
for( int i = 1 ; i <= 2*n ; i++ ) par[i] = i;
cnt = n;
//printf("NODE : ");
for( pii i : all ) {
int u = i.y, di = i.x;
for( pii x : g[u] ) {
int v = x.x;
if( d[v] > d[u] ) {
u = findroot( u ), v = findroot( v );
if( u == v ) continue ;
par[v] = u;
t[u].emplace_back( v ), t[v].emplace_back( u );
}
}
}
fill( mn, mn+2*N, 1e17 );
dfs( all.back().y, 0 );
scanf("%d",&Q);
for( int i = 0, a, b ; i < Q ; i++ ) {
scanf("%d %d",&a,&b);
int c = lca( a, b );
printf("%lld\n",d[c] );
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |