답안 #496464

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
496464 2021-12-21T09:21:07 Z vinnipuh01 Valley (BOI19_valley) C++17
100 / 100
270 ms 52544 KB
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
#define int long long

using namespace std;

const long long oo = 1000000000000000000;

long long  sum, ans = 0, mx = 0, mn = 1000000000, num, pos;


/*
    ViHHiPuh

   (( `'-""``""-'` ))
     )-__-_.._-__-(
   / --- (o _ o) --- \
   \ .-* ( .0. ) *-. /
   _'-. ,_ '=' _, .-'_
  / `;#'#'# - #'#'#;` \
 \_)) -----'#'----- ((_/
      # --------- #
  '# ------- ------ #'
  /..-'# ------- #'-.\
  _\...-\'# -- #'/-.../_
  ((____)- '#' -(____))


    cout << fixed << setprecision(6) << x;


    freopen ( "sum.in", "r", stdin )
*/

int n, m, q, st, par[ 100001 ][ 21 ], lg = 20, mnn[ 100001 ][ 21 ], d[ 100001 ], prr[ 100001 ], b[ 100001 ];
vector <pair<int, int> > v[ 100001 ];
queue <int> vv;
vector <int> v1;
int tmr, tin[ 100001 ], tout[ 100001 ], col[ 100001 ], t[ 400001 ], p[ 400001 ], tt[ 400001 ];
pair <int, int> e[ 100001 ];

void dfs( int u, int pr ) {
	par[ u ][ 0 ] = pr;
	tin[ u ] = ++tmr;
	v1.push_back( u );
	
	for ( int i = 1; i <= lg; i ++ ) {
		par[ u ][ i ] = par[ par[ u ][ i  - 1 ] ][ i - 1 ];
	}
	for ( auto to : v[ u ] ) {
		if ( to.first != pr ) {
			prr[ to.first ] = to.second;
			d[ to.first ] = d[ u ] + to.second;
			dfs( to.first, u );
		}
	}
	tout[ u ] = tmr;
}

bool ok( int a, int b ) {
	return ( tin[  a] <= tin[ b ] && tout[ a ] >= tout[ b ] );
}

int lca( int a, int b ) {
	if ( ok( a, b ) )
		return a;
	if ( ok( b, a ) )
		return b;
	for ( int i = lg; i >= 0; i -- ) {
		if ( !ok( par[ a ][ i ], b ) )
			a = par[ a ][ i ];
	}
	return par[ a ][ 0 ];
}

void pus( int v ) {
	if ( p[ v ] ) {
		p[ v + v ] += p[ v ];
		p[ v + v + 1 ] += p[ v ];
		t[ v + v ] += p[ v ];
		t[ v + v + 1 ] += p[ v ];
	}
}

int gett( int a, int b ) {
	mn = oo;
	if ( ok( a, b ) )
		return col[ a ];
	for ( int i = lg; i >= 0; i -- ) {
		if ( !ok( par[ a ][ i ], b )  ) {
			mn = min( mn, mnn[ a ][ i ] );
			a = par[  a][ i ];
		}
	}
	return min( mn, mnn[ a ][ 0 ] );
}


void dfs1( int u, int pr ) {
	mnn[ u ][ 0 ] = min( col[ u ], col[ pr ] );
	for ( int i = 1; i <= lg; i ++ )
		mnn[ u ][ i ] = min( mnn[ u ][ i - 1 ], mnn[ par[ u ][ i - 1 ] ][ i - 1 ]  );
	for ( auto to : v[ u ] ) {
		if ( to.first != pr ) {
			dfs1( to.first, u );
		}
	}
}

void build( int v, int tl, int tr ) {
	if ( tl == tr ) {
		t[ v ] = col[ tl ];
		tt[ v ] = tl;
		return;
	}
	int mid = ( tl + tr ) / 2;
	build(  v + v, tl, mid );
	build( v + v + 1, mid + 1, tr );
	t[ v ] = min( t[ v + v ], t[ v + v + 1 ] );
	if ( t[ v ] == t[ v + v ] )
		tt[ v ] = tt[ v + v ];
	else
		tt[ v ] = tt[ v + v + 1 ];
}

main () {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
	cin >> n >> m >> q >> st;
	int x, y, z;
	for ( int i = 1; i < n; i ++ ) {
		cin >> x >> y >> z;
		v[ x ].push_back( { y, z } );
		v[ y ].push_back( { x, z } );
		e[ i ] = { x, y };
	}
	for ( int i = 1; i <= n; i ++ )
		col[ i ] = oo;
	dfs( st, st );
	for ( int i = 1; i <= m; i ++ ) {
		cin >> x;
		col[ x ] = 0;
		b[ x ] = 1;
		vv.push( x );
	}
	while ( vv.size() ) {
		if ( col[ par[ vv.front() ][ 0 ] ] > col[ vv.front() ] + prr[ vv.front() ] ) {
			col[ par[ vv.front() ][ 0 ] ] = min( col[ vv.front() ] + prr[ vv.front() ], col[ par[ vv.front() ][ 0 ] ] );
			vv.push( par[ vv.front() ][ 0 ] ); 
		}
		vv.pop();
	}
	for ( int i = 1; i <= n; i ++ ) {
		if ( col[ i ] != oo )
			col[ i ] -= d[ i ];
	}
	dfs1( st, st );
	int l, r, ll, rr;
	while ( q -- ) {
		cin >> l >> r;
//		cout << l << " " << r << "     ";
		sum = ans = num = 0;
		if ( d[ e[ l ].first ] < d[ e[ l ].second ] ) {
			num = e[ l ].first;
			sum = e[ l ].second;
		}
		else {
			num = e[ l ].second;
			sum = e[ l ].first;
		}
		ans = lca( r, sum );
//		cout << ans << " " << sum << " - ";
		if ( ans != sum ) {
			cout << "escaped\n";
		}
		else {
			if ( col[ sum ] == oo ) {
				cout << "oo\n";
			}
			else {
				if ( !b[ r ] ) {
					num = gett( r, sum );
					num += d[ r ];
				}
				else
					num = 0;
				cout << num << "\n";
			}
		}
	}
}

/*
8 8 10 2
1 2 1
2 3 1
3 4 1
2 5 1
5 8 1
3 6 1
7 4 1
1 2 3 4 5 6 7 8
2 7
1 1
4 8
2 6
6 6
3 3
7 6
2 2
5 5
1 1
1 1
1 1
1 1
1 1
*/

Compilation message

valley.cpp:135:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  135 | main () {
      | ^~~~
valley.cpp: In function 'int main()':
valley.cpp:167:12: warning: unused variable 'll' [-Wunused-variable]
  167 |  int l, r, ll, rr;
      |            ^~
valley.cpp:167:16: warning: unused variable 'rr' [-Wunused-variable]
  167 |  int l, r, ll, rr;
      |                ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2764 KB Output is correct
2 Correct 3 ms 2764 KB Output is correct
3 Correct 4 ms 2764 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2764 KB Output is correct
2 Correct 3 ms 2764 KB Output is correct
3 Correct 4 ms 2764 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 2 ms 3148 KB Output is correct
6 Correct 2 ms 3148 KB Output is correct
7 Correct 2 ms 3148 KB Output is correct
8 Correct 2 ms 3148 KB Output is correct
9 Correct 2 ms 3148 KB Output is correct
10 Correct 3 ms 3148 KB Output is correct
11 Correct 2 ms 3148 KB Output is correct
12 Correct 2 ms 3148 KB Output is correct
13 Correct 3 ms 3148 KB Output is correct
14 Correct 3 ms 3148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 169 ms 49156 KB Output is correct
2 Correct 182 ms 48988 KB Output is correct
3 Correct 198 ms 49196 KB Output is correct
4 Correct 225 ms 50688 KB Output is correct
5 Correct 222 ms 50868 KB Output is correct
6 Correct 234 ms 52480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2764 KB Output is correct
2 Correct 3 ms 2764 KB Output is correct
3 Correct 4 ms 2764 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 2 ms 3148 KB Output is correct
6 Correct 2 ms 3148 KB Output is correct
7 Correct 2 ms 3148 KB Output is correct
8 Correct 2 ms 3148 KB Output is correct
9 Correct 2 ms 3148 KB Output is correct
10 Correct 3 ms 3148 KB Output is correct
11 Correct 2 ms 3148 KB Output is correct
12 Correct 2 ms 3148 KB Output is correct
13 Correct 3 ms 3148 KB Output is correct
14 Correct 3 ms 3148 KB Output is correct
15 Correct 169 ms 49156 KB Output is correct
16 Correct 182 ms 48988 KB Output is correct
17 Correct 198 ms 49196 KB Output is correct
18 Correct 225 ms 50688 KB Output is correct
19 Correct 222 ms 50868 KB Output is correct
20 Correct 234 ms 52480 KB Output is correct
21 Correct 159 ms 47700 KB Output is correct
22 Correct 168 ms 47592 KB Output is correct
23 Correct 185 ms 47800 KB Output is correct
24 Correct 215 ms 49456 KB Output is correct
25 Correct 253 ms 51900 KB Output is correct
26 Correct 153 ms 48060 KB Output is correct
27 Correct 177 ms 47964 KB Output is correct
28 Correct 186 ms 48184 KB Output is correct
29 Correct 224 ms 49336 KB Output is correct
30 Correct 270 ms 50816 KB Output is correct
31 Correct 159 ms 48480 KB Output is correct
32 Correct 179 ms 48448 KB Output is correct
33 Correct 217 ms 48840 KB Output is correct
34 Correct 244 ms 50200 KB Output is correct
35 Correct 262 ms 52544 KB Output is correct
36 Correct 238 ms 50752 KB Output is correct