Submission #674299

# Submission time Handle Problem Language Result Execution time Memory
674299 2022-12-23T16:44:07 Z lam Race (IOI11_race) C++14
100 / 100
1781 ms 75872 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<long long,long long> ii;
#define ff first
#define ss second
const long long maxn = 1e6 + 10;
map<long long,long long> mp;
long long n;
long long k;
vector <ii> adj[maxn];
long long s[maxn],dau[maxn];
long long ans = 1e9;

long long get_sz(long long x, long long p)
{
    s[x]=1;
    for (ii i:adj[x])
        if (i.ff!=p&&!dau[i.ff]) s[x]+=get_sz(i.ff,x);
    return s[x];
}

long long find_centroid(long long x, long long p, long long sz)
{
    for (ii i:adj[x])
        if (i.ff!=p&&!dau[i.ff]&&s[i.ff]*2>sz) return find_centroid(i.ff,x,sz);
    return x;
}

void dfs(long long x, long long p, long long val, long long depth)
{
    if (val<=k&&mp[k-val]) ans=min(ans,depth+mp[k-val]);
    if (val==k) ans=min(ans,depth);
    for (ii i:adj[x])
        if (i.ff!=p&&!dau[i.ff]) dfs(i.ff,x,1LL*val+i.ss,depth+1);
}
void dfs2(long long x, long long p, long long val, long long depth)
{
    if (val<=k)
    {
        if (!mp[val]) mp[val] = depth;
        else mp[val]=min(mp[val],depth);
    }
    for (ii i:adj[x])
        if (i.ff!=p&&!dau[i.ff]) dfs2(i.ff,x,1LL*val+i.ss,depth+1);
}

void decompose(long long x)
{
    x=find_centroid(x,x,get_sz(x,x));
    dau[x]=1;
    mp.clear();
    for (ii i:adj[x])
        if (!dau[i.ff])
    {
        dfs(i.ff,x,1LL*i.ss,1);
        dfs2(i.ff,x,1LL*i.ss,1);
    }
    for (ii i:adj[x])
        if (!dau[i.ff]) decompose(i.ff);
}

ii e[maxn];

int best_path(int N, int K, int H[][2], int L[])
{
    n=N; k=K;
    for (long long i=1; i<=n; i++)
    {
        adj[i].clear();
        dau[i]=0;
    }
    ans=1e9;
    for (long long i=0; i<n-1; i++)
    {
        e[i].ff=H[i][0];
        e[i].ss=H[i][1];
    }
    for (long long i=0; i<n-1; i++)
    {
        long long w=L[i];
        long long u=e[i].ff; long long v=e[i].ss;
        u++; v++;
        adj[u].push_back({v,w});
        adj[v].push_back({u,w});
    }
    decompose(1);
    if (ans==1e9) ans=-1;
    return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 12 ms 23832 KB Output is correct
3 Correct 13 ms 23892 KB Output is correct
4 Correct 14 ms 23892 KB Output is correct
5 Correct 13 ms 23724 KB Output is correct
6 Correct 13 ms 23836 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 14 ms 23828 KB Output is correct
9 Correct 15 ms 23760 KB Output is correct
10 Correct 14 ms 23764 KB Output is correct
11 Correct 14 ms 23732 KB Output is correct
12 Correct 15 ms 23772 KB Output is correct
13 Correct 13 ms 23828 KB Output is correct
14 Correct 13 ms 23764 KB Output is correct
15 Correct 11 ms 23704 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23780 KB Output is correct
18 Correct 15 ms 23728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 12 ms 23832 KB Output is correct
3 Correct 13 ms 23892 KB Output is correct
4 Correct 14 ms 23892 KB Output is correct
5 Correct 13 ms 23724 KB Output is correct
6 Correct 13 ms 23836 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 14 ms 23828 KB Output is correct
9 Correct 15 ms 23760 KB Output is correct
10 Correct 14 ms 23764 KB Output is correct
11 Correct 14 ms 23732 KB Output is correct
12 Correct 15 ms 23772 KB Output is correct
13 Correct 13 ms 23828 KB Output is correct
14 Correct 13 ms 23764 KB Output is correct
15 Correct 11 ms 23704 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23780 KB Output is correct
18 Correct 15 ms 23728 KB Output is correct
19 Correct 12 ms 23732 KB Output is correct
20 Correct 12 ms 23820 KB Output is correct
21 Correct 14 ms 23948 KB Output is correct
22 Correct 15 ms 23892 KB Output is correct
23 Correct 13 ms 23892 KB Output is correct
24 Correct 12 ms 23928 KB Output is correct
25 Correct 17 ms 24028 KB Output is correct
26 Correct 13 ms 23892 KB Output is correct
27 Correct 17 ms 23884 KB Output is correct
28 Correct 13 ms 23944 KB Output is correct
29 Correct 13 ms 23892 KB Output is correct
30 Correct 14 ms 24000 KB Output is correct
31 Correct 14 ms 24020 KB Output is correct
32 Correct 13 ms 23980 KB Output is correct
33 Correct 14 ms 23944 KB Output is correct
34 Correct 13 ms 23928 KB Output is correct
35 Correct 14 ms 23872 KB Output is correct
36 Correct 13 ms 23892 KB Output is correct
37 Correct 18 ms 23960 KB Output is correct
38 Correct 15 ms 24028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 12 ms 23832 KB Output is correct
3 Correct 13 ms 23892 KB Output is correct
4 Correct 14 ms 23892 KB Output is correct
5 Correct 13 ms 23724 KB Output is correct
6 Correct 13 ms 23836 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 14 ms 23828 KB Output is correct
9 Correct 15 ms 23760 KB Output is correct
10 Correct 14 ms 23764 KB Output is correct
11 Correct 14 ms 23732 KB Output is correct
12 Correct 15 ms 23772 KB Output is correct
13 Correct 13 ms 23828 KB Output is correct
14 Correct 13 ms 23764 KB Output is correct
15 Correct 11 ms 23704 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23780 KB Output is correct
18 Correct 15 ms 23728 KB Output is correct
19 Correct 312 ms 33196 KB Output is correct
20 Correct 317 ms 33200 KB Output is correct
21 Correct 313 ms 33216 KB Output is correct
22 Correct 302 ms 33128 KB Output is correct
23 Correct 153 ms 33584 KB Output is correct
24 Correct 110 ms 32800 KB Output is correct
25 Correct 243 ms 37492 KB Output is correct
26 Correct 143 ms 38972 KB Output is correct
27 Correct 272 ms 43124 KB Output is correct
28 Correct 544 ms 54268 KB Output is correct
29 Correct 509 ms 53364 KB Output is correct
30 Correct 266 ms 43116 KB Output is correct
31 Correct 296 ms 43120 KB Output is correct
32 Correct 342 ms 43108 KB Output is correct
33 Correct 470 ms 41928 KB Output is correct
34 Correct 348 ms 41864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 12 ms 23832 KB Output is correct
3 Correct 13 ms 23892 KB Output is correct
4 Correct 14 ms 23892 KB Output is correct
5 Correct 13 ms 23724 KB Output is correct
6 Correct 13 ms 23836 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 14 ms 23828 KB Output is correct
9 Correct 15 ms 23760 KB Output is correct
10 Correct 14 ms 23764 KB Output is correct
11 Correct 14 ms 23732 KB Output is correct
12 Correct 15 ms 23772 KB Output is correct
13 Correct 13 ms 23828 KB Output is correct
14 Correct 13 ms 23764 KB Output is correct
15 Correct 11 ms 23704 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23780 KB Output is correct
18 Correct 15 ms 23728 KB Output is correct
19 Correct 12 ms 23732 KB Output is correct
20 Correct 12 ms 23820 KB Output is correct
21 Correct 14 ms 23948 KB Output is correct
22 Correct 15 ms 23892 KB Output is correct
23 Correct 13 ms 23892 KB Output is correct
24 Correct 12 ms 23928 KB Output is correct
25 Correct 17 ms 24028 KB Output is correct
26 Correct 13 ms 23892 KB Output is correct
27 Correct 17 ms 23884 KB Output is correct
28 Correct 13 ms 23944 KB Output is correct
29 Correct 13 ms 23892 KB Output is correct
30 Correct 14 ms 24000 KB Output is correct
31 Correct 14 ms 24020 KB Output is correct
32 Correct 13 ms 23980 KB Output is correct
33 Correct 14 ms 23944 KB Output is correct
34 Correct 13 ms 23928 KB Output is correct
35 Correct 14 ms 23872 KB Output is correct
36 Correct 13 ms 23892 KB Output is correct
37 Correct 18 ms 23960 KB Output is correct
38 Correct 15 ms 24028 KB Output is correct
39 Correct 312 ms 33196 KB Output is correct
40 Correct 317 ms 33200 KB Output is correct
41 Correct 313 ms 33216 KB Output is correct
42 Correct 302 ms 33128 KB Output is correct
43 Correct 153 ms 33584 KB Output is correct
44 Correct 110 ms 32800 KB Output is correct
45 Correct 243 ms 37492 KB Output is correct
46 Correct 143 ms 38972 KB Output is correct
47 Correct 272 ms 43124 KB Output is correct
48 Correct 544 ms 54268 KB Output is correct
49 Correct 509 ms 53364 KB Output is correct
50 Correct 266 ms 43116 KB Output is correct
51 Correct 296 ms 43120 KB Output is correct
52 Correct 342 ms 43108 KB Output is correct
53 Correct 470 ms 41928 KB Output is correct
54 Correct 348 ms 41864 KB Output is correct
55 Correct 36 ms 24916 KB Output is correct
56 Correct 31 ms 24660 KB Output is correct
57 Correct 207 ms 33584 KB Output is correct
58 Correct 54 ms 32704 KB Output is correct
59 Correct 444 ms 47936 KB Output is correct
60 Correct 1635 ms 75872 KB Output is correct
61 Correct 373 ms 43724 KB Output is correct
62 Correct 417 ms 43896 KB Output is correct
63 Correct 488 ms 43900 KB Output is correct
64 Correct 1781 ms 55152 KB Output is correct
65 Correct 368 ms 46552 KB Output is correct
66 Correct 1111 ms 55048 KB Output is correct
67 Correct 149 ms 45396 KB Output is correct
68 Correct 608 ms 55604 KB Output is correct
69 Correct 680 ms 55300 KB Output is correct
70 Correct 570 ms 54312 KB Output is correct