#include "race.h"
#include <vector>
#include <queue>
#include <set>
#include <cstdio>
#include <algorithm>
using namespace std;
static const int MAXN=262144;
static set<int> conn[MAXN];
static int H[MAXN][2];
static int L[MAXN];
static int N,K;
//N: number of city K: length of course, H, L:city and cost
static int size[MAXN];
static int maxchild[MAXN];
vector<int> ele;
int dfs(int v,int pa)
{
ele.push_back(v);
size[v]=1;
maxchild[v]=0;
for(int Eno:conn[v])
{
int target=H[Eno][0]^H[Eno][1]^v;
if(target==pa) continue;
int ret=dfs(target,v);
maxchild[v]=max(maxchild[v],ret);
size[v]+=ret;
}
return size[v];
}
int findcentroid(int v)
{
ele.clear();
int SZ=dfs(v,-1);
int minv=987654321;
int mini=v;
for(int a:ele)
{
int asize=max(maxchild[a],SZ-size[a]);
if(minv>asize)
{
minv=asize;
mini=a;
}
}
return mini; //amolang
}
static int ans=987654231;
//saving first and second minimum value, while first and second color is different;
int fminv[1010101];
int fminc[1010101];
int sminv[1010101];
int sminc[1010101];
vector<int> dists;
void bktk(int a,int pa,int dist,int height,int color)
{
if(dist>K) return; //do not have to think about it
dists.push_back(dist);
if(fminv[dist]>height)
{
if(fminc[dist]!=color)
{
sminv[dist]=fminv[dist];
sminc[dist]=fminc[dist];
}
fminv[dist]=height;
fminc[dist]=color;
}
else if(sminv[dist]>height && fminc[dist]!=color)
{
sminv[dist]=height;
sminc[dist]=color;
}
for(int Eno: conn[a])
{
int target=H[Eno][0]^H[Eno][1]^a;
int distadd=L[Eno];
if(target==pa) continue;
bktk(target,a,dist+distadd,height+1,color);
}
}
void work(int v)
{
dists.clear();
for(int Eno: conn[v])
{
int target=H[Eno][0]^H[Eno][1]^v;
int dist=L[Eno];
bktk(target,v,dist,1,target);
}
sort(dists.begin(),dists.end());
dists.resize(unique(dists.begin(),dists.end())-dists.begin());
for(int x:dists)
{
if(fminc[x]!=fminc[K-x])
ans=min(ans,fminv[x]+fminv[K-x]);
ans=min(ans,fminv[x]+sminv[K-x]);
}
ans=min(ans,fminv[K]);
for(int i:dists)
{
fminv[i]=sminv[i]=0x3f3f3f3f;
fminc[i]=-1,sminc[i]=-2;
}
}
int best_path(int N, int K, int H[][2], int L[])
{
::N=N, ::K=K;
for(int i=0;i<N-1;i++)
{
::H[i][0]=H[i][0];
::H[i][1]=H[i][1];
::L[i]=L[i];
conn[H[i][0]].insert(i);
conn[H[i][1]].insert(i);
}
for(int i=0;i<=K;i++)
{
fminv[i]=sminv[i]=0x3f3f3f3f;
fminc[i]=-1,sminc[i]=-2;
}
queue<int> Q;
Q.push(0);
while(!Q.empty())
{
int v=Q.front();
Q.pop();
int centroid=findcentroid(v);
work(centroid);
for(int v:conn[centroid])
{
int target=H[v][0]^H[v][1]^centroid;
conn[target].erase(v);
Q.push(target);
}
conn[centroid].clear();
}
if(ans>N) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12664 KB |
Output is correct |
2 |
Correct |
10 ms |
12776 KB |
Output is correct |
3 |
Correct |
12 ms |
12776 KB |
Output is correct |
4 |
Correct |
11 ms |
12824 KB |
Output is correct |
5 |
Correct |
11 ms |
12824 KB |
Output is correct |
6 |
Correct |
11 ms |
12824 KB |
Output is correct |
7 |
Correct |
12 ms |
12852 KB |
Output is correct |
8 |
Correct |
12 ms |
12956 KB |
Output is correct |
9 |
Correct |
12 ms |
12956 KB |
Output is correct |
10 |
Correct |
12 ms |
12956 KB |
Output is correct |
11 |
Correct |
11 ms |
13012 KB |
Output is correct |
12 |
Correct |
12 ms |
13012 KB |
Output is correct |
13 |
Correct |
11 ms |
13012 KB |
Output is correct |
14 |
Correct |
12 ms |
13012 KB |
Output is correct |
15 |
Correct |
11 ms |
13012 KB |
Output is correct |
16 |
Correct |
11 ms |
13028 KB |
Output is correct |
17 |
Correct |
12 ms |
13036 KB |
Output is correct |
18 |
Correct |
11 ms |
13036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12664 KB |
Output is correct |
2 |
Correct |
10 ms |
12776 KB |
Output is correct |
3 |
Correct |
12 ms |
12776 KB |
Output is correct |
4 |
Correct |
11 ms |
12824 KB |
Output is correct |
5 |
Correct |
11 ms |
12824 KB |
Output is correct |
6 |
Correct |
11 ms |
12824 KB |
Output is correct |
7 |
Correct |
12 ms |
12852 KB |
Output is correct |
8 |
Correct |
12 ms |
12956 KB |
Output is correct |
9 |
Correct |
12 ms |
12956 KB |
Output is correct |
10 |
Correct |
12 ms |
12956 KB |
Output is correct |
11 |
Correct |
11 ms |
13012 KB |
Output is correct |
12 |
Correct |
12 ms |
13012 KB |
Output is correct |
13 |
Correct |
11 ms |
13012 KB |
Output is correct |
14 |
Correct |
12 ms |
13012 KB |
Output is correct |
15 |
Correct |
11 ms |
13012 KB |
Output is correct |
16 |
Correct |
11 ms |
13028 KB |
Output is correct |
17 |
Correct |
12 ms |
13036 KB |
Output is correct |
18 |
Correct |
11 ms |
13036 KB |
Output is correct |
19 |
Correct |
12 ms |
13052 KB |
Output is correct |
20 |
Correct |
11 ms |
13052 KB |
Output is correct |
21 |
Correct |
14 ms |
13096 KB |
Output is correct |
22 |
Correct |
24 ms |
27388 KB |
Output is correct |
23 |
Correct |
22 ms |
27388 KB |
Output is correct |
24 |
Correct |
26 ms |
27388 KB |
Output is correct |
25 |
Correct |
28 ms |
27388 KB |
Output is correct |
26 |
Correct |
18 ms |
27388 KB |
Output is correct |
27 |
Correct |
24 ms |
27388 KB |
Output is correct |
28 |
Correct |
16 ms |
27388 KB |
Output is correct |
29 |
Correct |
19 ms |
27388 KB |
Output is correct |
30 |
Correct |
20 ms |
27388 KB |
Output is correct |
31 |
Correct |
23 ms |
27388 KB |
Output is correct |
32 |
Correct |
25 ms |
27388 KB |
Output is correct |
33 |
Correct |
25 ms |
27388 KB |
Output is correct |
34 |
Correct |
22 ms |
27388 KB |
Output is correct |
35 |
Correct |
23 ms |
27388 KB |
Output is correct |
36 |
Correct |
31 ms |
27660 KB |
Output is correct |
37 |
Correct |
25 ms |
27660 KB |
Output is correct |
38 |
Correct |
22 ms |
27660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12664 KB |
Output is correct |
2 |
Correct |
10 ms |
12776 KB |
Output is correct |
3 |
Correct |
12 ms |
12776 KB |
Output is correct |
4 |
Correct |
11 ms |
12824 KB |
Output is correct |
5 |
Correct |
11 ms |
12824 KB |
Output is correct |
6 |
Correct |
11 ms |
12824 KB |
Output is correct |
7 |
Correct |
12 ms |
12852 KB |
Output is correct |
8 |
Correct |
12 ms |
12956 KB |
Output is correct |
9 |
Correct |
12 ms |
12956 KB |
Output is correct |
10 |
Correct |
12 ms |
12956 KB |
Output is correct |
11 |
Correct |
11 ms |
13012 KB |
Output is correct |
12 |
Correct |
12 ms |
13012 KB |
Output is correct |
13 |
Correct |
11 ms |
13012 KB |
Output is correct |
14 |
Correct |
12 ms |
13012 KB |
Output is correct |
15 |
Correct |
11 ms |
13012 KB |
Output is correct |
16 |
Correct |
11 ms |
13028 KB |
Output is correct |
17 |
Correct |
12 ms |
13036 KB |
Output is correct |
18 |
Correct |
11 ms |
13036 KB |
Output is correct |
19 |
Correct |
593 ms |
27660 KB |
Output is correct |
20 |
Correct |
576 ms |
27660 KB |
Output is correct |
21 |
Correct |
660 ms |
27660 KB |
Output is correct |
22 |
Correct |
517 ms |
27660 KB |
Output is correct |
23 |
Correct |
327 ms |
27660 KB |
Output is correct |
24 |
Correct |
160 ms |
27660 KB |
Output is correct |
25 |
Correct |
390 ms |
29312 KB |
Output is correct |
26 |
Correct |
212 ms |
32888 KB |
Output is correct |
27 |
Correct |
666 ms |
38900 KB |
Output is correct |
28 |
Correct |
947 ms |
51640 KB |
Output is correct |
29 |
Correct |
971 ms |
51640 KB |
Output is correct |
30 |
Correct |
707 ms |
51640 KB |
Output is correct |
31 |
Correct |
625 ms |
51640 KB |
Output is correct |
32 |
Correct |
816 ms |
51640 KB |
Output is correct |
33 |
Correct |
935 ms |
51640 KB |
Output is correct |
34 |
Correct |
911 ms |
51640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12664 KB |
Output is correct |
2 |
Correct |
10 ms |
12776 KB |
Output is correct |
3 |
Correct |
12 ms |
12776 KB |
Output is correct |
4 |
Correct |
11 ms |
12824 KB |
Output is correct |
5 |
Correct |
11 ms |
12824 KB |
Output is correct |
6 |
Correct |
11 ms |
12824 KB |
Output is correct |
7 |
Correct |
12 ms |
12852 KB |
Output is correct |
8 |
Correct |
12 ms |
12956 KB |
Output is correct |
9 |
Correct |
12 ms |
12956 KB |
Output is correct |
10 |
Correct |
12 ms |
12956 KB |
Output is correct |
11 |
Correct |
11 ms |
13012 KB |
Output is correct |
12 |
Correct |
12 ms |
13012 KB |
Output is correct |
13 |
Correct |
11 ms |
13012 KB |
Output is correct |
14 |
Correct |
12 ms |
13012 KB |
Output is correct |
15 |
Correct |
11 ms |
13012 KB |
Output is correct |
16 |
Correct |
11 ms |
13028 KB |
Output is correct |
17 |
Correct |
12 ms |
13036 KB |
Output is correct |
18 |
Correct |
11 ms |
13036 KB |
Output is correct |
19 |
Correct |
12 ms |
13052 KB |
Output is correct |
20 |
Correct |
11 ms |
13052 KB |
Output is correct |
21 |
Correct |
14 ms |
13096 KB |
Output is correct |
22 |
Correct |
24 ms |
27388 KB |
Output is correct |
23 |
Correct |
22 ms |
27388 KB |
Output is correct |
24 |
Correct |
26 ms |
27388 KB |
Output is correct |
25 |
Correct |
28 ms |
27388 KB |
Output is correct |
26 |
Correct |
18 ms |
27388 KB |
Output is correct |
27 |
Correct |
24 ms |
27388 KB |
Output is correct |
28 |
Correct |
16 ms |
27388 KB |
Output is correct |
29 |
Correct |
19 ms |
27388 KB |
Output is correct |
30 |
Correct |
20 ms |
27388 KB |
Output is correct |
31 |
Correct |
23 ms |
27388 KB |
Output is correct |
32 |
Correct |
25 ms |
27388 KB |
Output is correct |
33 |
Correct |
25 ms |
27388 KB |
Output is correct |
34 |
Correct |
22 ms |
27388 KB |
Output is correct |
35 |
Correct |
23 ms |
27388 KB |
Output is correct |
36 |
Correct |
31 ms |
27660 KB |
Output is correct |
37 |
Correct |
25 ms |
27660 KB |
Output is correct |
38 |
Correct |
22 ms |
27660 KB |
Output is correct |
39 |
Correct |
593 ms |
27660 KB |
Output is correct |
40 |
Correct |
576 ms |
27660 KB |
Output is correct |
41 |
Correct |
660 ms |
27660 KB |
Output is correct |
42 |
Correct |
517 ms |
27660 KB |
Output is correct |
43 |
Correct |
327 ms |
27660 KB |
Output is correct |
44 |
Correct |
160 ms |
27660 KB |
Output is correct |
45 |
Correct |
390 ms |
29312 KB |
Output is correct |
46 |
Correct |
212 ms |
32888 KB |
Output is correct |
47 |
Correct |
666 ms |
38900 KB |
Output is correct |
48 |
Correct |
947 ms |
51640 KB |
Output is correct |
49 |
Correct |
971 ms |
51640 KB |
Output is correct |
50 |
Correct |
707 ms |
51640 KB |
Output is correct |
51 |
Correct |
625 ms |
51640 KB |
Output is correct |
52 |
Correct |
816 ms |
51640 KB |
Output is correct |
53 |
Correct |
935 ms |
51640 KB |
Output is correct |
54 |
Correct |
911 ms |
51640 KB |
Output is correct |
55 |
Correct |
30 ms |
51640 KB |
Output is correct |
56 |
Correct |
31 ms |
51640 KB |
Output is correct |
57 |
Correct |
203 ms |
51640 KB |
Output is correct |
58 |
Correct |
126 ms |
51640 KB |
Output is correct |
59 |
Correct |
279 ms |
51640 KB |
Output is correct |
60 |
Correct |
1430 ms |
67932 KB |
Output is correct |
61 |
Correct |
710 ms |
67932 KB |
Output is correct |
62 |
Correct |
691 ms |
67932 KB |
Output is correct |
63 |
Correct |
982 ms |
67932 KB |
Output is correct |
64 |
Correct |
1724 ms |
67932 KB |
Output is correct |
65 |
Correct |
1086 ms |
67932 KB |
Output is correct |
66 |
Correct |
1493 ms |
67932 KB |
Output is correct |
67 |
Correct |
332 ms |
67932 KB |
Output is correct |
68 |
Correct |
1103 ms |
67932 KB |
Output is correct |
69 |
Correct |
1091 ms |
67932 KB |
Output is correct |
70 |
Correct |
972 ms |
67932 KB |
Output is correct |