#include <bits/stdc++.h>
#include "race.h"
#define lp(i,a,b) for(int i=a;i<b;i++)
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define ll long long
#define sz size()
const int MAXN = 2e5+10 ;
using namespace std ;
int curTime ;
int ini[MAXN] , fim[MAXN] , pai[MAXN] , fatSomaVal[MAXN] , ta[MAXN] , resp[MAXN] ;
ll fatSomaKey[MAXN] , specialEdge[MAXN] ;
bool marc[MAXN] ;
vector< pair<int,ll> > adj[MAXN] ;
map<ll,int> mapa[MAXN] ;
map<ll,int>::iterator it ;
int bit[MAXN] ;
void add(int x) { for(int i = x ; i < MAXN ; i += (i&-i) ) bit[i] ++ ; }
int qry(int x)
{
int tot = 0 ;
for(int i = x ; i > 0 ; i -= (i&-i) )
tot += bit[i] ;
return tot ;
}
int get(int i , int j) { return qry(j) - qry(i) ; }
void dfs(int x)
{
ini[x] = ++ curTime ;
for( pair<int,ll> p : adj[x] )
if( ini[p.ff] == 0 )
{
pai[p.ff] = x ;
dfs(p.ff) ;
specialEdge[p.ff] = p.ss ;
}
fim[x] = curTime ;
}
void inserta(int idx , ll Key , int Val)
{
if( mapa[idx].find(Key) == mapa[idx].end() )
mapa[idx].insert(mk(Key,Val)) ;
mapa[idx][Key] = min( mapa[idx][Key] , Val ) ;
}
void printMap(int idx)
{
printf("Imprimindo o map %d:\n" , idx ) ;
idx = ta[idx] ;
for( it = mapa[idx].begin() ; it != mapa[idx].end() ; it++ )
printf("%lld %d\n" , it->ff, it->ss) ;
printf("\n") ;
}
int best_path(int n , int k , int h[][2] , int l[] )
{
lp(i,0,MAXN) resp[i] = MAXN ;
lp(i,0,n-1)
{
int a = h[i][0] , b = h[i][1] ;
adj[a].pb( mk( b , 1LL * l[i] ) ) ;
adj[b].pb( mk( a , 1LL * l[i] ) ) ;
}
dfs(0) ;
queue<int> q ;
lp(i,0,n)
if( ini[i] == fim[i] )
{
marc[i] = true ;
q.push(i) ;
ta[i] = q.sz ;
}
while( !q.empty() )
{
int davez = q.front() ;
q.pop() ;
add(ini[davez]) ;
int father = pai[davez] ;
if( !marc[father] && get(ini[father] , fim[father]) == (fim[father]-ini[father]) )
{
q.push(father) ;
marc[father] = true ;
}
if( ini[davez] == fim[davez] ) continue ;
int bigChild = adj[davez][0].ff ;
if( bigChild == pai[davez] ) bigChild = adj[davez][1].ff ;
for(pair<int,ll> p : adj[davez])
if( mapa[ta[bigChild]].sz < mapa[ta[p.ff]].sz && p.ff != pai[davez] )
bigChild = p.ff ;
fatSomaKey[davez] = fatSomaKey[bigChild] + specialEdge[bigChild] ;
fatSomaVal[davez] = fatSomaVal[bigChild] + 1 ;
ta[davez] = ta[bigChild] ;
//printf("%lld %d %d de %d\n" , fatSomaKey[davez] , fatSomaVal[davez] , ta[davez] , davez ) ;
inserta( ta[davez] , specialEdge[bigChild] - fatSomaKey[davez] , 1 - fatSomaVal[davez] ) ;
for( pair<int,ll> p : adj[davez] )
{
int cur = p.ff ;
if( cur == bigChild || cur == pai[davez]) continue ;
vector<pair<ll,int> > atualiza ;
inserta( ta[cur] , -fatSomaKey[cur] , -fatSomaVal[cur] ) ;
for(it = mapa[ta[cur]].begin() ; it != mapa[ta[cur]].end() ; it++ )
{
ll realKey = it->ff + fatSomaKey[cur] + specialEdge[cur] ;
int realVal = it->ss + fatSomaVal[cur] + 1 ;
ll updKey = realKey - fatSomaKey[davez] ;
int updVal = realVal - fatSomaVal[davez] ;
ll procuro = k - realKey - fatSomaKey[davez] ;
atualiza.pb(mk(updKey , updVal)) ;
if( mapa[ta[davez]].find(procuro) == mapa[ta[davez]].end() )
continue ;
resp[davez] = min(resp[davez] , mapa[ta[davez]][procuro] + fatSomaVal[davez] + realVal ) ;
}
for(pair<ll,int> p : atualiza)
inserta(ta[davez], p.ff , p.ss ) ;
}
if( mapa[ta[davez]].find( k - fatSomaKey[davez] ) != mapa[ta[davez]].end() )
resp[davez] = min(resp[davez] , mapa[ta[davez]][k-fatSomaKey[davez]] + fatSomaVal[davez]) ;
//printMap(davez) ;
}
int ans = *min_element(resp, resp+n) ;
return ans == MAXN ? -1 : ans ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
15352 KB |
Output is correct |
2 |
Correct |
15 ms |
15352 KB |
Output is correct |
3 |
Correct |
19 ms |
15352 KB |
Output is correct |
4 |
Correct |
18 ms |
15224 KB |
Output is correct |
5 |
Correct |
18 ms |
15224 KB |
Output is correct |
6 |
Correct |
16 ms |
15224 KB |
Output is correct |
7 |
Correct |
15 ms |
15224 KB |
Output is correct |
8 |
Correct |
16 ms |
15352 KB |
Output is correct |
9 |
Correct |
15 ms |
15352 KB |
Output is correct |
10 |
Correct |
15 ms |
15352 KB |
Output is correct |
11 |
Correct |
16 ms |
15352 KB |
Output is correct |
12 |
Correct |
15 ms |
15224 KB |
Output is correct |
13 |
Correct |
15 ms |
15224 KB |
Output is correct |
14 |
Correct |
15 ms |
15352 KB |
Output is correct |
15 |
Correct |
15 ms |
15224 KB |
Output is correct |
16 |
Correct |
15 ms |
15352 KB |
Output is correct |
17 |
Correct |
15 ms |
15224 KB |
Output is correct |
18 |
Correct |
15 ms |
15228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
15352 KB |
Output is correct |
2 |
Correct |
15 ms |
15352 KB |
Output is correct |
3 |
Correct |
19 ms |
15352 KB |
Output is correct |
4 |
Correct |
18 ms |
15224 KB |
Output is correct |
5 |
Correct |
18 ms |
15224 KB |
Output is correct |
6 |
Correct |
16 ms |
15224 KB |
Output is correct |
7 |
Correct |
15 ms |
15224 KB |
Output is correct |
8 |
Correct |
16 ms |
15352 KB |
Output is correct |
9 |
Correct |
15 ms |
15352 KB |
Output is correct |
10 |
Correct |
15 ms |
15352 KB |
Output is correct |
11 |
Correct |
16 ms |
15352 KB |
Output is correct |
12 |
Correct |
15 ms |
15224 KB |
Output is correct |
13 |
Correct |
15 ms |
15224 KB |
Output is correct |
14 |
Correct |
15 ms |
15352 KB |
Output is correct |
15 |
Correct |
15 ms |
15224 KB |
Output is correct |
16 |
Correct |
15 ms |
15352 KB |
Output is correct |
17 |
Correct |
15 ms |
15224 KB |
Output is correct |
18 |
Correct |
15 ms |
15228 KB |
Output is correct |
19 |
Correct |
16 ms |
15352 KB |
Output is correct |
20 |
Correct |
16 ms |
15352 KB |
Output is correct |
21 |
Correct |
17 ms |
15480 KB |
Output is correct |
22 |
Correct |
18 ms |
15664 KB |
Output is correct |
23 |
Correct |
17 ms |
15608 KB |
Output is correct |
24 |
Correct |
17 ms |
15608 KB |
Output is correct |
25 |
Correct |
18 ms |
15736 KB |
Output is correct |
26 |
Correct |
17 ms |
15608 KB |
Output is correct |
27 |
Correct |
16 ms |
15480 KB |
Output is correct |
28 |
Correct |
17 ms |
15608 KB |
Output is correct |
29 |
Correct |
18 ms |
15608 KB |
Output is correct |
30 |
Correct |
18 ms |
15580 KB |
Output is correct |
31 |
Correct |
17 ms |
15608 KB |
Output is correct |
32 |
Correct |
17 ms |
15608 KB |
Output is correct |
33 |
Correct |
18 ms |
15608 KB |
Output is correct |
34 |
Correct |
17 ms |
15480 KB |
Output is correct |
35 |
Correct |
17 ms |
15480 KB |
Output is correct |
36 |
Correct |
17 ms |
15480 KB |
Output is correct |
37 |
Correct |
17 ms |
15480 KB |
Output is correct |
38 |
Correct |
17 ms |
15608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
15352 KB |
Output is correct |
2 |
Correct |
15 ms |
15352 KB |
Output is correct |
3 |
Correct |
19 ms |
15352 KB |
Output is correct |
4 |
Correct |
18 ms |
15224 KB |
Output is correct |
5 |
Correct |
18 ms |
15224 KB |
Output is correct |
6 |
Correct |
16 ms |
15224 KB |
Output is correct |
7 |
Correct |
15 ms |
15224 KB |
Output is correct |
8 |
Correct |
16 ms |
15352 KB |
Output is correct |
9 |
Correct |
15 ms |
15352 KB |
Output is correct |
10 |
Correct |
15 ms |
15352 KB |
Output is correct |
11 |
Correct |
16 ms |
15352 KB |
Output is correct |
12 |
Correct |
15 ms |
15224 KB |
Output is correct |
13 |
Correct |
15 ms |
15224 KB |
Output is correct |
14 |
Correct |
15 ms |
15352 KB |
Output is correct |
15 |
Correct |
15 ms |
15224 KB |
Output is correct |
16 |
Correct |
15 ms |
15352 KB |
Output is correct |
17 |
Correct |
15 ms |
15224 KB |
Output is correct |
18 |
Correct |
15 ms |
15228 KB |
Output is correct |
19 |
Correct |
214 ms |
36756 KB |
Output is correct |
20 |
Correct |
230 ms |
36728 KB |
Output is correct |
21 |
Correct |
221 ms |
36728 KB |
Output is correct |
22 |
Correct |
224 ms |
36932 KB |
Output is correct |
23 |
Correct |
313 ms |
50296 KB |
Output is correct |
24 |
Correct |
239 ms |
42616 KB |
Output is correct |
25 |
Correct |
169 ms |
29304 KB |
Output is correct |
26 |
Correct |
81 ms |
31480 KB |
Output is correct |
27 |
Correct |
358 ms |
49144 KB |
Output is correct |
28 |
Correct |
404 ms |
60152 KB |
Output is correct |
29 |
Correct |
419 ms |
60288 KB |
Output is correct |
30 |
Correct |
360 ms |
49272 KB |
Output is correct |
31 |
Correct |
366 ms |
49144 KB |
Output is correct |
32 |
Correct |
487 ms |
49096 KB |
Output is correct |
33 |
Correct |
496 ms |
47608 KB |
Output is correct |
34 |
Correct |
697 ms |
79724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
15352 KB |
Output is correct |
2 |
Correct |
15 ms |
15352 KB |
Output is correct |
3 |
Correct |
19 ms |
15352 KB |
Output is correct |
4 |
Correct |
18 ms |
15224 KB |
Output is correct |
5 |
Correct |
18 ms |
15224 KB |
Output is correct |
6 |
Correct |
16 ms |
15224 KB |
Output is correct |
7 |
Correct |
15 ms |
15224 KB |
Output is correct |
8 |
Correct |
16 ms |
15352 KB |
Output is correct |
9 |
Correct |
15 ms |
15352 KB |
Output is correct |
10 |
Correct |
15 ms |
15352 KB |
Output is correct |
11 |
Correct |
16 ms |
15352 KB |
Output is correct |
12 |
Correct |
15 ms |
15224 KB |
Output is correct |
13 |
Correct |
15 ms |
15224 KB |
Output is correct |
14 |
Correct |
15 ms |
15352 KB |
Output is correct |
15 |
Correct |
15 ms |
15224 KB |
Output is correct |
16 |
Correct |
15 ms |
15352 KB |
Output is correct |
17 |
Correct |
15 ms |
15224 KB |
Output is correct |
18 |
Correct |
15 ms |
15228 KB |
Output is correct |
19 |
Correct |
16 ms |
15352 KB |
Output is correct |
20 |
Correct |
16 ms |
15352 KB |
Output is correct |
21 |
Correct |
17 ms |
15480 KB |
Output is correct |
22 |
Correct |
18 ms |
15664 KB |
Output is correct |
23 |
Correct |
17 ms |
15608 KB |
Output is correct |
24 |
Correct |
17 ms |
15608 KB |
Output is correct |
25 |
Correct |
18 ms |
15736 KB |
Output is correct |
26 |
Correct |
17 ms |
15608 KB |
Output is correct |
27 |
Correct |
16 ms |
15480 KB |
Output is correct |
28 |
Correct |
17 ms |
15608 KB |
Output is correct |
29 |
Correct |
18 ms |
15608 KB |
Output is correct |
30 |
Correct |
18 ms |
15580 KB |
Output is correct |
31 |
Correct |
17 ms |
15608 KB |
Output is correct |
32 |
Correct |
17 ms |
15608 KB |
Output is correct |
33 |
Correct |
18 ms |
15608 KB |
Output is correct |
34 |
Correct |
17 ms |
15480 KB |
Output is correct |
35 |
Correct |
17 ms |
15480 KB |
Output is correct |
36 |
Correct |
17 ms |
15480 KB |
Output is correct |
37 |
Correct |
17 ms |
15480 KB |
Output is correct |
38 |
Correct |
17 ms |
15608 KB |
Output is correct |
39 |
Correct |
214 ms |
36756 KB |
Output is correct |
40 |
Correct |
230 ms |
36728 KB |
Output is correct |
41 |
Correct |
221 ms |
36728 KB |
Output is correct |
42 |
Correct |
224 ms |
36932 KB |
Output is correct |
43 |
Correct |
313 ms |
50296 KB |
Output is correct |
44 |
Correct |
239 ms |
42616 KB |
Output is correct |
45 |
Correct |
169 ms |
29304 KB |
Output is correct |
46 |
Correct |
81 ms |
31480 KB |
Output is correct |
47 |
Correct |
358 ms |
49144 KB |
Output is correct |
48 |
Correct |
404 ms |
60152 KB |
Output is correct |
49 |
Correct |
419 ms |
60288 KB |
Output is correct |
50 |
Correct |
360 ms |
49272 KB |
Output is correct |
51 |
Correct |
366 ms |
49144 KB |
Output is correct |
52 |
Correct |
487 ms |
49096 KB |
Output is correct |
53 |
Correct |
496 ms |
47608 KB |
Output is correct |
54 |
Correct |
697 ms |
79724 KB |
Output is correct |
55 |
Correct |
39 ms |
18516 KB |
Output is correct |
56 |
Correct |
27 ms |
17144 KB |
Output is correct |
57 |
Correct |
112 ms |
34680 KB |
Output is correct |
58 |
Correct |
89 ms |
30308 KB |
Output is correct |
59 |
Correct |
136 ms |
37752 KB |
Output is correct |
60 |
Correct |
433 ms |
60160 KB |
Output is correct |
61 |
Correct |
436 ms |
52512 KB |
Output is correct |
62 |
Correct |
402 ms |
49160 KB |
Output is correct |
63 |
Correct |
434 ms |
49128 KB |
Output is correct |
64 |
Correct |
856 ms |
99360 KB |
Output is correct |
65 |
Correct |
907 ms |
99616 KB |
Output is correct |
66 |
Correct |
452 ms |
60776 KB |
Output is correct |
67 |
Correct |
295 ms |
47580 KB |
Output is correct |
68 |
Correct |
641 ms |
66552 KB |
Output is correct |
69 |
Correct |
640 ms |
66988 KB |
Output is correct |
70 |
Correct |
621 ms |
64440 KB |
Output is correct |