답안 #210667

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
210667 2020-03-18T01:29:45 Z robinrst 경주 (Race) (IOI11_race) C++14
100 / 100
2447 ms 100076 KB
#include        "algorithm"
#include        "iostream"
#include        "numeric"
#include        "iomanip"
#include        "cstring"
#include        "math.h"
#include        "bitset"
#include        "string"
#include        "vector"
#include        "ctime"
#include        "queue"
#include        "stack"
#include        "map"
#include        "set"
 
#include        "ext/pb_ds/assoc_container.hpp" // Common file
#include        "ext/pb_ds/tree_policy.hpp" // Including tree_order_statistics_node_update
#include        "ext/pb_ds/detail/standard_policies.hpp"
 
using namespace std;
using namespace __gnu_pbds;
 
 
#define          f first
#define          lgn 25
#define          endl '\n'
#define          sc second
#define          NN (int)3e5+5
#define          pb push_back
#define          mod 1000000007
#define          ld long double
#define          vi vector<int>
#define          eb emplace_back
#define          vpii vector<pii>
#define          ll long long 
#define          mii map<ll,ll>
#define          mp make_pair
#define          pii pair<int,int>
#define          pq priority_queue
#define          BLOCK (int)sqrt(N)
#define          test(x) while(x--)
#define          all(x) begin(x),end(x)
#define          allr(x) rbegin(x),rend(x)
#define          fo(i,a,n) for(int i=a;i<n;i++)
#define          rfo(i,n,a) for(int i=n;i>=a;i--)
#define          FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define          time() cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n"
#define          PI acos(-1.0)
#define 		 bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
 
typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update > 
OS ;
 
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}
 
inline void INP()
{
	#ifndef ONLINE_JUDGE
	    freopen("input.txt","r",stdin);   
	    freopen("output.txt","w",stdout);
	#endif 
}
 
const ll inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
 
ll n,m,k,q,ans;
string s;
vpii adj[NN];
ll in[NN],out[NN],sz[NN],Dep[NN],sumNow[NN],id[NN];
bool big[NN];
mii path;
 
void dfsSize(int  i = 0, int p = -1)
{
	static int clk = 0;
	sz[i] = 1;
	in[i] = ++clk;
	id[clk] = i;
 
	for( auto j : adj[i] )
	{
		if( j.f != p )
		{
			sumNow[j.f] = sumNow[i] + j.sc;
			Dep[j.f] = Dep[i] + 1;
			dfsSize(j.f , i);
			sz[i] += sz[j.f];
		}
	}
	out[i] = clk;
}
 
#define z id[k]
 
void add(int i, int x)
{
	ll sum = sumNow[i];
	if( x == -1 ) path[sum] = 0;
	else
	{
		ll xx = path[sum];
		if( xx == 0 ) xx = inf;
		path[sum] = min( Dep[i] , xx );
	}
}
 
void dfs(int i = 0 , int p = -1, bool keep = 0)
{
	int mx = -1, bigChild = -1;
 
	for( auto j : adj[i] )
	{
		if( j.f != p and mx < sz[j.f] ) mx = sz[j.f] , bigChild = j.f;
	}
 
	for( auto j : adj[i] ) 
	{
		if( j.f != p and j.f != bigChild ) dfs(j.f , i , 0);
	}
 
	if( ~bigChild ) dfs(bigChild , i , 1) , big[bigChild] = 1;
 
	for( auto j : adj[i] )
	{
		if( j.f != p and !big[j.f] )
		{
			fo(k,in[j.f],out[j.f]+1)
			{
				ll now =  m + 2*sumNow[i] - sumNow[z];
				if( path[now] )
				{
					ans = min( ans , path[now] + (Dep[z] - Dep[i]) - Dep[i] );
					// Minus Dep[i] in the end is because we have that added in path[now - k]
				}
			}
			fo(k,in[j.f],out[j.f]+1) add( z , 1);
		}
	}
 
	add(i ,1);
 
	// for( auto j : path ) cout << j.f << '-' << j.sc << endl;
	// cout << endl;
 
	if( path[ sumNow[i] + m ] ) ans = min( ans , path[ sumNow[i] + m ] - Dep[i] ); // Minus Dep[i] beacuse that is already added in path[ sumNow[i] + k]
 	if( ~bigChild ) big[bigChild] = 0;
	if( !keep )
	{
		fo(k,in[i],out[i]+1) add(z , -1);
	}
}
 
int go()
{
	//cin >> n >> m;
	ans = inf;
	/*fo(i,0,n-1)
	{
		int u , v , w;
		cin >> u >> v >> w;
		adj[u].pb({ v, w} );
		adj[v].pb({ u , w} );
	}
    */
	dfsSize();
	dfs();
 
	if( ans == inf ) ans = -1;
	
	return ans;
 
}
 
int best_path (int N, int K, int H[][2], int L[])
{
    n = N, m = K;
    fo(i,0,n-1)
    {
        adj[H[i][0]].pb(mp(H[i][1], L[i]));
        adj[H[i][1]].pb(mp(H[i][0], L[i]));
    }
    return go();
}
 
// int32_t main()
// {
// 	INP(); 
// 	FAST;     
// 	int t=1; 
// 	// cin>>t;
// 	test(t) go();
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 9 ms 7416 KB Output is correct
6 Correct 9 ms 7416 KB Output is correct
7 Correct 9 ms 7416 KB Output is correct
8 Correct 9 ms 7416 KB Output is correct
9 Correct 9 ms 7416 KB Output is correct
10 Correct 9 ms 7416 KB Output is correct
11 Correct 9 ms 7416 KB Output is correct
12 Correct 9 ms 7416 KB Output is correct
13 Correct 9 ms 7416 KB Output is correct
14 Correct 9 ms 7416 KB Output is correct
15 Correct 9 ms 7416 KB Output is correct
16 Correct 10 ms 7416 KB Output is correct
17 Correct 9 ms 7416 KB Output is correct
18 Correct 9 ms 7416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 9 ms 7416 KB Output is correct
6 Correct 9 ms 7416 KB Output is correct
7 Correct 9 ms 7416 KB Output is correct
8 Correct 9 ms 7416 KB Output is correct
9 Correct 9 ms 7416 KB Output is correct
10 Correct 9 ms 7416 KB Output is correct
11 Correct 9 ms 7416 KB Output is correct
12 Correct 9 ms 7416 KB Output is correct
13 Correct 9 ms 7416 KB Output is correct
14 Correct 9 ms 7416 KB Output is correct
15 Correct 9 ms 7416 KB Output is correct
16 Correct 10 ms 7416 KB Output is correct
17 Correct 9 ms 7416 KB Output is correct
18 Correct 9 ms 7416 KB Output is correct
19 Correct 9 ms 7416 KB Output is correct
20 Correct 9 ms 7416 KB Output is correct
21 Correct 11 ms 7672 KB Output is correct
22 Correct 11 ms 7800 KB Output is correct
23 Correct 11 ms 7800 KB Output is correct
24 Correct 12 ms 7800 KB Output is correct
25 Correct 11 ms 7800 KB Output is correct
26 Correct 11 ms 7800 KB Output is correct
27 Correct 10 ms 7544 KB Output is correct
28 Correct 11 ms 7800 KB Output is correct
29 Correct 11 ms 7800 KB Output is correct
30 Correct 11 ms 7800 KB Output is correct
31 Correct 11 ms 7800 KB Output is correct
32 Correct 11 ms 7800 KB Output is correct
33 Correct 12 ms 7800 KB Output is correct
34 Correct 10 ms 7672 KB Output is correct
35 Correct 10 ms 7676 KB Output is correct
36 Correct 10 ms 7672 KB Output is correct
37 Correct 10 ms 7672 KB Output is correct
38 Correct 10 ms 7672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 9 ms 7416 KB Output is correct
6 Correct 9 ms 7416 KB Output is correct
7 Correct 9 ms 7416 KB Output is correct
8 Correct 9 ms 7416 KB Output is correct
9 Correct 9 ms 7416 KB Output is correct
10 Correct 9 ms 7416 KB Output is correct
11 Correct 9 ms 7416 KB Output is correct
12 Correct 9 ms 7416 KB Output is correct
13 Correct 9 ms 7416 KB Output is correct
14 Correct 9 ms 7416 KB Output is correct
15 Correct 9 ms 7416 KB Output is correct
16 Correct 10 ms 7416 KB Output is correct
17 Correct 9 ms 7416 KB Output is correct
18 Correct 9 ms 7416 KB Output is correct
19 Correct 211 ms 17144 KB Output is correct
20 Correct 215 ms 17276 KB Output is correct
21 Correct 206 ms 17272 KB Output is correct
22 Correct 206 ms 17272 KB Output is correct
23 Correct 308 ms 17528 KB Output is correct
24 Correct 225 ms 17400 KB Output is correct
25 Correct 118 ms 23160 KB Output is correct
26 Correct 72 ms 27676 KB Output is correct
27 Correct 285 ms 27388 KB Output is correct
28 Correct 331 ms 72696 KB Output is correct
29 Correct 337 ms 71772 KB Output is correct
30 Correct 285 ms 27384 KB Output is correct
31 Correct 281 ms 27384 KB Output is correct
32 Correct 375 ms 27640 KB Output is correct
33 Correct 368 ms 26232 KB Output is correct
34 Correct 1399 ms 82808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 9 ms 7416 KB Output is correct
6 Correct 9 ms 7416 KB Output is correct
7 Correct 9 ms 7416 KB Output is correct
8 Correct 9 ms 7416 KB Output is correct
9 Correct 9 ms 7416 KB Output is correct
10 Correct 9 ms 7416 KB Output is correct
11 Correct 9 ms 7416 KB Output is correct
12 Correct 9 ms 7416 KB Output is correct
13 Correct 9 ms 7416 KB Output is correct
14 Correct 9 ms 7416 KB Output is correct
15 Correct 9 ms 7416 KB Output is correct
16 Correct 10 ms 7416 KB Output is correct
17 Correct 9 ms 7416 KB Output is correct
18 Correct 9 ms 7416 KB Output is correct
19 Correct 9 ms 7416 KB Output is correct
20 Correct 9 ms 7416 KB Output is correct
21 Correct 11 ms 7672 KB Output is correct
22 Correct 11 ms 7800 KB Output is correct
23 Correct 11 ms 7800 KB Output is correct
24 Correct 12 ms 7800 KB Output is correct
25 Correct 11 ms 7800 KB Output is correct
26 Correct 11 ms 7800 KB Output is correct
27 Correct 10 ms 7544 KB Output is correct
28 Correct 11 ms 7800 KB Output is correct
29 Correct 11 ms 7800 KB Output is correct
30 Correct 11 ms 7800 KB Output is correct
31 Correct 11 ms 7800 KB Output is correct
32 Correct 11 ms 7800 KB Output is correct
33 Correct 12 ms 7800 KB Output is correct
34 Correct 10 ms 7672 KB Output is correct
35 Correct 10 ms 7676 KB Output is correct
36 Correct 10 ms 7672 KB Output is correct
37 Correct 10 ms 7672 KB Output is correct
38 Correct 10 ms 7672 KB Output is correct
39 Correct 211 ms 17144 KB Output is correct
40 Correct 215 ms 17276 KB Output is correct
41 Correct 206 ms 17272 KB Output is correct
42 Correct 206 ms 17272 KB Output is correct
43 Correct 308 ms 17528 KB Output is correct
44 Correct 225 ms 17400 KB Output is correct
45 Correct 118 ms 23160 KB Output is correct
46 Correct 72 ms 27676 KB Output is correct
47 Correct 285 ms 27388 KB Output is correct
48 Correct 331 ms 72696 KB Output is correct
49 Correct 337 ms 71772 KB Output is correct
50 Correct 285 ms 27384 KB Output is correct
51 Correct 281 ms 27384 KB Output is correct
52 Correct 375 ms 27640 KB Output is correct
53 Correct 368 ms 26232 KB Output is correct
54 Correct 1399 ms 82808 KB Output is correct
55 Correct 35 ms 9464 KB Output is correct
56 Correct 20 ms 8568 KB Output is correct
57 Correct 135 ms 17400 KB Output is correct
58 Correct 79 ms 17388 KB Output is correct
59 Correct 154 ms 39928 KB Output is correct
60 Correct 399 ms 71544 KB Output is correct
61 Correct 390 ms 30840 KB Output is correct
62 Correct 283 ms 27640 KB Output is correct
63 Correct 376 ms 27640 KB Output is correct
64 Correct 2080 ms 54776 KB Output is correct
65 Correct 2447 ms 100076 KB Output is correct
66 Correct 421 ms 70264 KB Output is correct
67 Correct 277 ms 27620 KB Output is correct
68 Correct 1138 ms 63480 KB Output is correct
69 Correct 1159 ms 64204 KB Output is correct
70 Correct 1087 ms 61268 KB Output is correct