/*
* Task: ioi11_race
* Lang: C/C++11
* Site: oj.uz
* Last Update: 12/2/2018
*/
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
using namespace std;
/* Note
----------------------------
Learned :
Bugs found & solved :
Optimizations :
----------------------------
*/
#define x first
#define y second
#define umap unordered_map
#define pqueue priority_queue
#define mset multiset
#define mp make_pair
#define mt make_tuple
#define long long long
#define MOD 1000000007
#define MAX (long)(1e16+5)
#define MIN (long)(-1e16-5)
#define FILEIN_ freopen("__in.txt","r",stdin)
#define FILEOUT_ freopen("__out.txt","w",stdout)
#define FILEIN(text) freopen(text,"r",stdin)
#define FILEOUT(text) freopen(text,"w",stdout)
//int tmp[200005][2],tmp2[200005];
vector<pair<long,long>> adl[200005];
umap<long,long> s[200005];
long ls[200005],sid[200005],lv[200005],sc,n,m,mn=MAX;
void solve(long x,long f){
long i,y,id,v,need,w;
pair<long,long> mx={0,0};
umap<long,long>::iterator it;
lv[x] = lv[f]+1;
for(i = 0; i < adl[x].size(); i++){
y = adl[x][i].x;
if(y == f) continue;
solve(y,x);
id = sid[y];
mx = max(mx,{(long)s[id].size(),y});
}
if(mx.x){
sid[x] = sid[mx.y];
}
else{
sid[x] = ++sc;
mx.y = 0;
}
for(i = 0; i < adl[x].size(); i++){
y = adl[x][i].x;
if(y == f || y == mx.y) continue;
id = sid[y];
it = s[id].begin();
while(it != s[id].end()){
v = (*it).x + ls[id];
need = m-v-ls[sid[x]];
if(s[sid[x]][need]){
mn = min(mn,s[sid[x]][need]+(*it).y-lv[x]*2);
}
else{
s[sid[x]].erase(need);
}
it++;
}
while(!s[id].empty()){
v = (*s[id].begin()).x + ls[id];
if(s[sid[x]][v-ls[sid[x]]]){
s[sid[x]][v-ls[sid[x]]] = min(s[sid[x]][v-ls[sid[x]]],(*s[id].begin()).y);
}
else{
s[sid[x]][v-ls[sid[x]]] = (*s[id].begin()).y;
}
s[id].erase(s[id].begin());
}
}
for(i = 0; i < adl[x].size(); i++){
y = adl[x][i].x;
w = adl[x][i].y;
if(y == f){
break;
}
}
need = m-w-ls[sid[x]];
if(s[sid[x]][need]){
mn = min(mn,s[sid[x]][need]-lv[f]);
}
else{
s[sid[x]].erase(need);
}
ls[sid[x]] += w;
s[sid[x]][w - ls[sid[x]]] = lv[x];
}
long best_path(int _n,int _m,int _edl[][2],int _edw[]){ // f*ck you 0-based idx
long i,j;
n = _n;
m = _m;
for(i = 0; i < n-1; i++){
adl[_edl[i][0]+1].emplace_back(_edl[i][1]+1,_edw[i]);
adl[_edl[i][1]+1].emplace_back(_edl[i][0]+1,_edw[i]);
if(_edw[i] == m){
return 1;
}
}
solve(1,0);
if(mn != MAX){
return mn;
}
else{
return -1;
}
}
/*main(){
int t,i,j,k,_n,_m;
FILEIN_;
scanf("%d %d",&_n,&_m);
for(i = 0; i < _n-1; i++){
scanf("%d %d %d",&tmp[i][0],&tmp[i][1],&tmp2[i]);
}
printf(">> %lld\n",best_path(_n,_m,tmp,tmp2));
return 0;
}*/
Compilation message
race.cpp: In function 'void solve(long long int, long long int)':
race.cpp:46:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i = 0; i < adl[x].size(); i++){
~~^~~~~~~~~~~~~~~
race.cpp:60:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i = 0; i < adl[x].size(); i++){
~~^~~~~~~~~~~~~~~
race.cpp:87:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i = 0; i < adl[x].size(); i++){
~~^~~~~~~~~~~~~~~
race.cpp: In function 'long long int best_path(int, int, int (*)[2], int*)':
race.cpp:106:9: warning: unused variable 'j' [-Wunused-variable]
long i,j;
^
race.cpp: In function 'void solve(long long int, long long int)':
race.cpp:94:10: warning: 'w' may be used uninitialized in this function [-Wmaybe-uninitialized]
need = m-w-ls[sid[x]];
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
15992 KB |
Output is correct |
2 |
Correct |
15 ms |
16232 KB |
Output is correct |
3 |
Correct |
14 ms |
16232 KB |
Output is correct |
4 |
Correct |
17 ms |
16232 KB |
Output is correct |
5 |
Correct |
15 ms |
16236 KB |
Output is correct |
6 |
Correct |
15 ms |
16236 KB |
Output is correct |
7 |
Correct |
14 ms |
16236 KB |
Output is correct |
8 |
Correct |
15 ms |
16236 KB |
Output is correct |
9 |
Correct |
16 ms |
16236 KB |
Output is correct |
10 |
Correct |
15 ms |
16236 KB |
Output is correct |
11 |
Correct |
15 ms |
16236 KB |
Output is correct |
12 |
Correct |
17 ms |
16236 KB |
Output is correct |
13 |
Correct |
14 ms |
16236 KB |
Output is correct |
14 |
Correct |
14 ms |
16236 KB |
Output is correct |
15 |
Correct |
15 ms |
16292 KB |
Output is correct |
16 |
Correct |
17 ms |
16292 KB |
Output is correct |
17 |
Correct |
14 ms |
16292 KB |
Output is correct |
18 |
Correct |
20 ms |
16292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
15992 KB |
Output is correct |
2 |
Correct |
15 ms |
16232 KB |
Output is correct |
3 |
Correct |
14 ms |
16232 KB |
Output is correct |
4 |
Correct |
17 ms |
16232 KB |
Output is correct |
5 |
Correct |
15 ms |
16236 KB |
Output is correct |
6 |
Correct |
15 ms |
16236 KB |
Output is correct |
7 |
Correct |
14 ms |
16236 KB |
Output is correct |
8 |
Correct |
15 ms |
16236 KB |
Output is correct |
9 |
Correct |
16 ms |
16236 KB |
Output is correct |
10 |
Correct |
15 ms |
16236 KB |
Output is correct |
11 |
Correct |
15 ms |
16236 KB |
Output is correct |
12 |
Correct |
17 ms |
16236 KB |
Output is correct |
13 |
Correct |
14 ms |
16236 KB |
Output is correct |
14 |
Correct |
14 ms |
16236 KB |
Output is correct |
15 |
Correct |
15 ms |
16292 KB |
Output is correct |
16 |
Correct |
17 ms |
16292 KB |
Output is correct |
17 |
Correct |
14 ms |
16292 KB |
Output is correct |
18 |
Correct |
20 ms |
16292 KB |
Output is correct |
19 |
Correct |
13 ms |
16292 KB |
Output is correct |
20 |
Correct |
16 ms |
16292 KB |
Output is correct |
21 |
Correct |
17 ms |
16336 KB |
Output is correct |
22 |
Correct |
16 ms |
16336 KB |
Output is correct |
23 |
Correct |
16 ms |
16336 KB |
Output is correct |
24 |
Correct |
16 ms |
16364 KB |
Output is correct |
25 |
Correct |
18 ms |
16364 KB |
Output is correct |
26 |
Correct |
17 ms |
16364 KB |
Output is correct |
27 |
Correct |
17 ms |
16364 KB |
Output is correct |
28 |
Correct |
17 ms |
16364 KB |
Output is correct |
29 |
Correct |
16 ms |
16364 KB |
Output is correct |
30 |
Correct |
17 ms |
16364 KB |
Output is correct |
31 |
Correct |
17 ms |
16364 KB |
Output is correct |
32 |
Correct |
18 ms |
16364 KB |
Output is correct |
33 |
Correct |
19 ms |
16364 KB |
Output is correct |
34 |
Correct |
15 ms |
16364 KB |
Output is correct |
35 |
Correct |
16 ms |
16464 KB |
Output is correct |
36 |
Correct |
15 ms |
16464 KB |
Output is correct |
37 |
Correct |
15 ms |
16464 KB |
Output is correct |
38 |
Correct |
15 ms |
16464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
15992 KB |
Output is correct |
2 |
Correct |
15 ms |
16232 KB |
Output is correct |
3 |
Correct |
14 ms |
16232 KB |
Output is correct |
4 |
Correct |
17 ms |
16232 KB |
Output is correct |
5 |
Correct |
15 ms |
16236 KB |
Output is correct |
6 |
Correct |
15 ms |
16236 KB |
Output is correct |
7 |
Correct |
14 ms |
16236 KB |
Output is correct |
8 |
Correct |
15 ms |
16236 KB |
Output is correct |
9 |
Correct |
16 ms |
16236 KB |
Output is correct |
10 |
Correct |
15 ms |
16236 KB |
Output is correct |
11 |
Correct |
15 ms |
16236 KB |
Output is correct |
12 |
Correct |
17 ms |
16236 KB |
Output is correct |
13 |
Correct |
14 ms |
16236 KB |
Output is correct |
14 |
Correct |
14 ms |
16236 KB |
Output is correct |
15 |
Correct |
15 ms |
16292 KB |
Output is correct |
16 |
Correct |
17 ms |
16292 KB |
Output is correct |
17 |
Correct |
14 ms |
16292 KB |
Output is correct |
18 |
Correct |
20 ms |
16292 KB |
Output is correct |
19 |
Correct |
173 ms |
27388 KB |
Output is correct |
20 |
Correct |
169 ms |
27388 KB |
Output is correct |
21 |
Correct |
186 ms |
27540 KB |
Output is correct |
22 |
Correct |
187 ms |
27676 KB |
Output is correct |
23 |
Correct |
80 ms |
27676 KB |
Output is correct |
24 |
Correct |
224 ms |
29820 KB |
Output is correct |
25 |
Correct |
102 ms |
33160 KB |
Output is correct |
26 |
Correct |
82 ms |
40980 KB |
Output is correct |
27 |
Correct |
314 ms |
40980 KB |
Output is correct |
28 |
Correct |
426 ms |
73408 KB |
Output is correct |
29 |
Correct |
460 ms |
73408 KB |
Output is correct |
30 |
Correct |
374 ms |
73408 KB |
Output is correct |
31 |
Correct |
341 ms |
73408 KB |
Output is correct |
32 |
Correct |
423 ms |
73408 KB |
Output is correct |
33 |
Correct |
314 ms |
73408 KB |
Output is correct |
34 |
Correct |
656 ms |
73408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
15992 KB |
Output is correct |
2 |
Correct |
15 ms |
16232 KB |
Output is correct |
3 |
Correct |
14 ms |
16232 KB |
Output is correct |
4 |
Correct |
17 ms |
16232 KB |
Output is correct |
5 |
Correct |
15 ms |
16236 KB |
Output is correct |
6 |
Correct |
15 ms |
16236 KB |
Output is correct |
7 |
Correct |
14 ms |
16236 KB |
Output is correct |
8 |
Correct |
15 ms |
16236 KB |
Output is correct |
9 |
Correct |
16 ms |
16236 KB |
Output is correct |
10 |
Correct |
15 ms |
16236 KB |
Output is correct |
11 |
Correct |
15 ms |
16236 KB |
Output is correct |
12 |
Correct |
17 ms |
16236 KB |
Output is correct |
13 |
Correct |
14 ms |
16236 KB |
Output is correct |
14 |
Correct |
14 ms |
16236 KB |
Output is correct |
15 |
Correct |
15 ms |
16292 KB |
Output is correct |
16 |
Correct |
17 ms |
16292 KB |
Output is correct |
17 |
Correct |
14 ms |
16292 KB |
Output is correct |
18 |
Correct |
20 ms |
16292 KB |
Output is correct |
19 |
Correct |
13 ms |
16292 KB |
Output is correct |
20 |
Correct |
16 ms |
16292 KB |
Output is correct |
21 |
Correct |
17 ms |
16336 KB |
Output is correct |
22 |
Correct |
16 ms |
16336 KB |
Output is correct |
23 |
Correct |
16 ms |
16336 KB |
Output is correct |
24 |
Correct |
16 ms |
16364 KB |
Output is correct |
25 |
Correct |
18 ms |
16364 KB |
Output is correct |
26 |
Correct |
17 ms |
16364 KB |
Output is correct |
27 |
Correct |
17 ms |
16364 KB |
Output is correct |
28 |
Correct |
17 ms |
16364 KB |
Output is correct |
29 |
Correct |
16 ms |
16364 KB |
Output is correct |
30 |
Correct |
17 ms |
16364 KB |
Output is correct |
31 |
Correct |
17 ms |
16364 KB |
Output is correct |
32 |
Correct |
18 ms |
16364 KB |
Output is correct |
33 |
Correct |
19 ms |
16364 KB |
Output is correct |
34 |
Correct |
15 ms |
16364 KB |
Output is correct |
35 |
Correct |
16 ms |
16464 KB |
Output is correct |
36 |
Correct |
15 ms |
16464 KB |
Output is correct |
37 |
Correct |
15 ms |
16464 KB |
Output is correct |
38 |
Correct |
15 ms |
16464 KB |
Output is correct |
39 |
Correct |
173 ms |
27388 KB |
Output is correct |
40 |
Correct |
169 ms |
27388 KB |
Output is correct |
41 |
Correct |
186 ms |
27540 KB |
Output is correct |
42 |
Correct |
187 ms |
27676 KB |
Output is correct |
43 |
Correct |
80 ms |
27676 KB |
Output is correct |
44 |
Correct |
224 ms |
29820 KB |
Output is correct |
45 |
Correct |
102 ms |
33160 KB |
Output is correct |
46 |
Correct |
82 ms |
40980 KB |
Output is correct |
47 |
Correct |
314 ms |
40980 KB |
Output is correct |
48 |
Correct |
426 ms |
73408 KB |
Output is correct |
49 |
Correct |
460 ms |
73408 KB |
Output is correct |
50 |
Correct |
374 ms |
73408 KB |
Output is correct |
51 |
Correct |
341 ms |
73408 KB |
Output is correct |
52 |
Correct |
423 ms |
73408 KB |
Output is correct |
53 |
Correct |
314 ms |
73408 KB |
Output is correct |
54 |
Correct |
656 ms |
73408 KB |
Output is correct |
55 |
Correct |
38 ms |
73408 KB |
Output is correct |
56 |
Correct |
28 ms |
73408 KB |
Output is correct |
57 |
Correct |
124 ms |
73408 KB |
Output is correct |
58 |
Correct |
107 ms |
73408 KB |
Output is correct |
59 |
Correct |
162 ms |
73408 KB |
Output is correct |
60 |
Correct |
491 ms |
73408 KB |
Output is correct |
61 |
Correct |
452 ms |
73408 KB |
Output is correct |
62 |
Correct |
325 ms |
73408 KB |
Output is correct |
63 |
Correct |
361 ms |
73408 KB |
Output is correct |
64 |
Correct |
713 ms |
73408 KB |
Output is correct |
65 |
Correct |
766 ms |
73408 KB |
Output is correct |
66 |
Correct |
519 ms |
73408 KB |
Output is correct |
67 |
Correct |
360 ms |
73408 KB |
Output is correct |
68 |
Correct |
538 ms |
73408 KB |
Output is correct |
69 |
Correct |
611 ms |
73408 KB |
Output is correct |
70 |
Correct |
478 ms |
73408 KB |
Output is correct |