Submission #116634

# Submission time Handle Problem Language Result Execution time Memory
116634 2019-06-13T09:15:24 Z roseanne_pcy Race (IOI11_race) C++14
100 / 100
1093 ms 38756 KB
#include <bits/stdc++.h>
#include "race.h"
#define ii pair<int, int>
#define X first
#define Y second
#define pb push_back
#define vi vector<int>
#define vii vector< pair<int, int> >
#define iii pair< ii, int >
typedef long long ll;
using namespace std;
vector< ii > adj[200005];
int use[200005];
int deg[200005];
int maxnow[200005];
int mic[200005];
void memsetNode(int u, int par)
{
    mic[u] = -1;
    maxnow[u] = 0;
    //printf("set 0 at %d\n", u);
    for(int i = 0; i< (int) adj[u].size(); i++)
    {
        int v = adj[u][i].X;
        if(use[v]) continue;
        if(v == par) continue;
        memsetNode(v, u);
    }
}
int cnt(int u, int par)
{
    if(mic[u] != -1) return mic[u];
    int sum = 1;
    for(int i = 0; i< (int) adj[u].size(); i++)
    {
        int v = adj[u][i].X;
        if(use[v]) continue;
        if(v == par) continue;
        sum += cnt(v, u);
    }
    //printf("computed %d %d\n", u, par);
    return mic[u] = sum;
}
void update(int u, int par, int book)
{
    //printf("update %d %d %d\n", u, par, book);
    maxnow[u] = max(maxnow[u], book);
    int sm = 1;
    for(int i = 0; i< (int) adj[u].size(); i++)
    {
        ii k = adj[u][i];
        if(k.X == par) continue;
        if(use[k.X]) continue;
        maxnow[k.X] = max(maxnow[k.X], 1+book);
        maxnow[u] = max(maxnow[u], cnt(k.X, u));
        //printf("added (%d, %d)", k.X, u);
        sm += cnt(k.X, u);
    }
    sm += book;
    //printf("sm is %d for %d\n", sm, u);
    for(int i = 0; i< (int) adj[u].size(); i++)
    {
        ii k = adj[u][i];
        if(k.X == par) continue;
        if(use[k.X]) continue;
        update(k.X, u, sm - cnt(k.X, u));
    }
}
int findBest(int u, int par)
{
    int best = -1;
    //printf("%d: %d\n", u, maxnow[u]);
    for(int i = 0; i< (int) adj[u].size(); i++)
    {
        int v = adj[u][i].X;
        if(use[v]) continue;
        if(v == par) continue;
        int here = findBest(v, u);
        if(best == -1 || maxnow[here]< maxnow[best]) best = here;
    }
    int res;
    if(best == -1) res = u;
    else if(maxnow[best]< maxnow[u]) res = best;
    else res = u;
    //printf("of size %d\n", maxnow[res]);
    return res;
}
int memsetcnt = 0;
void findNode(int u, int par)
{
    //printf("its me\n");
    int st = u;
    //printf("--------memsetting %d %d\n", u, par);
    memsetcnt++;
    //if(memsetcnt % 10000 == 0) printf("%d\n", memsetcnt/1000);
    memsetNode(u, par);
    update(st, par, 0);
    //printf("kuy\n");
}
int bestPath[1000005];
vii joe;
void diep(int u, int par, int dist, int len)
{
    if(dist > 1000000) return;
    joe.pb(ii(dist, len));
    //printf("%d %d %d %d\n", u, par, dist, len);
    for(int i = 0; i< (int) adj[u].size(); i++)
    {
        int v = adj[u][i].X;
        int w = adj[u][i].Y;
        if(v == par) continue;
        if(use[v]) continue;
        diep(v, u, dist+w, len+1);
    }
}
int k;
int n;
int solve(int u, int par)
{
    //memset maxnow 0
    //memset mic
    //printf("hello\n");
    findNode(u, par);
    int root = findBest(u, par);
    //printf("--------------root is %d\n", root);
    vector<int> Set;
    int best = 1e9;
    bestPath[0] = 0;
    for(int i = 0; i< (int) adj[root].size(); i++)
    {
        joe.clear();
        int v = adj[root][i].X;
        if(v == par) continue;
        if(use[v]) continue;
        diep(v, root, adj[root][i].Y, 1);
        for(int j = 0; j< (int) joe.size(); j++)
        {
            Set.pb(joe[j].X);
            if(k-joe[j].X >= 0 && bestPath[k-joe[j].X] != -1)
            {
                best = min(best, bestPath[k-joe[j].X] + joe[j].Y);
            }
        }
        for(int j = 0; j< (int) joe.size(); j++)
        {
            if(bestPath[joe[j].X] == -1 || bestPath[joe[j].X] > joe[j].Y) bestPath[joe[j].X] = joe[j].Y;
            //lprintf("%d\n", joe[j].X);
        }
    }
    use[root] = 1;
    for(int i = 0; i< (int) Set.size(); i++)
    {
        bestPath[Set[i]] = -1;
    }
    for(int i = 0; i< (int) adj[root].size(); i++)
    {
        int v = adj[root][i].X;
        if(v == par) continue;
        if(use[v]) continue;
        best = min(best, solve(v, root));
    }
    return best;
}
int best_path(int N, int K, int H[][2], int L[])
{
    memset(bestPath, -1, sizeof bestPath);
    memset(mic, -1, sizeof mic);
    //freopen("grader.in.16", "r", stdin);
	n = N; k = K;
    for(int i = 0; i< n-1; i++)
    {
        int a, b, w;
		a = H[i][0], b = H[i][1], w = L[i];
        adj[a].pb(ii(b, w));
        adj[b].pb(ii(a, w));
        deg[a]++; deg[b]++;
    }
    int res = solve(0, -1);
    if(res == 1e9) return -1;
    return res;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 9 ms 9728 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 11 ms 9728 KB Output is correct
11 Correct 9 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 9 ms 9728 KB Output is correct
15 Correct 9 ms 9728 KB Output is correct
16 Correct 9 ms 9728 KB Output is correct
17 Correct 9 ms 9728 KB Output is correct
18 Correct 9 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 9 ms 9728 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 11 ms 9728 KB Output is correct
11 Correct 9 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 9 ms 9728 KB Output is correct
15 Correct 9 ms 9728 KB Output is correct
16 Correct 9 ms 9728 KB Output is correct
17 Correct 9 ms 9728 KB Output is correct
18 Correct 9 ms 9728 KB Output is correct
19 Correct 9 ms 9728 KB Output is correct
20 Correct 9 ms 9728 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 11 ms 9728 KB Output is correct
23 Correct 10 ms 9728 KB Output is correct
24 Correct 10 ms 9856 KB Output is correct
25 Correct 11 ms 9856 KB Output is correct
26 Correct 11 ms 9856 KB Output is correct
27 Correct 11 ms 9728 KB Output is correct
28 Correct 10 ms 9856 KB Output is correct
29 Correct 10 ms 9856 KB Output is correct
30 Correct 10 ms 9856 KB Output is correct
31 Correct 11 ms 9856 KB Output is correct
32 Correct 15 ms 9856 KB Output is correct
33 Correct 11 ms 9856 KB Output is correct
34 Correct 11 ms 9856 KB Output is correct
35 Correct 11 ms 9856 KB Output is correct
36 Correct 11 ms 9848 KB Output is correct
37 Correct 10 ms 9856 KB Output is correct
38 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 9 ms 9728 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 11 ms 9728 KB Output is correct
11 Correct 9 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 9 ms 9728 KB Output is correct
15 Correct 9 ms 9728 KB Output is correct
16 Correct 9 ms 9728 KB Output is correct
17 Correct 9 ms 9728 KB Output is correct
18 Correct 9 ms 9728 KB Output is correct
19 Correct 334 ms 17360 KB Output is correct
20 Correct 310 ms 17388 KB Output is correct
21 Correct 340 ms 17384 KB Output is correct
22 Correct 453 ms 17392 KB Output is correct
23 Correct 274 ms 17396 KB Output is correct
24 Correct 137 ms 17012 KB Output is correct
25 Correct 352 ms 21004 KB Output is correct
26 Correct 163 ms 24780 KB Output is correct
27 Correct 381 ms 23436 KB Output is correct
28 Correct 1008 ms 37948 KB Output is correct
29 Correct 1093 ms 36804 KB Output is correct
30 Correct 375 ms 23456 KB Output is correct
31 Correct 405 ms 23540 KB Output is correct
32 Correct 520 ms 23544 KB Output is correct
33 Correct 841 ms 24028 KB Output is correct
34 Correct 780 ms 21512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 9 ms 9728 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 11 ms 9728 KB Output is correct
11 Correct 9 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 9 ms 9728 KB Output is correct
15 Correct 9 ms 9728 KB Output is correct
16 Correct 9 ms 9728 KB Output is correct
17 Correct 9 ms 9728 KB Output is correct
18 Correct 9 ms 9728 KB Output is correct
19 Correct 9 ms 9728 KB Output is correct
20 Correct 9 ms 9728 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 11 ms 9728 KB Output is correct
23 Correct 10 ms 9728 KB Output is correct
24 Correct 10 ms 9856 KB Output is correct
25 Correct 11 ms 9856 KB Output is correct
26 Correct 11 ms 9856 KB Output is correct
27 Correct 11 ms 9728 KB Output is correct
28 Correct 10 ms 9856 KB Output is correct
29 Correct 10 ms 9856 KB Output is correct
30 Correct 10 ms 9856 KB Output is correct
31 Correct 11 ms 9856 KB Output is correct
32 Correct 15 ms 9856 KB Output is correct
33 Correct 11 ms 9856 KB Output is correct
34 Correct 11 ms 9856 KB Output is correct
35 Correct 11 ms 9856 KB Output is correct
36 Correct 11 ms 9848 KB Output is correct
37 Correct 10 ms 9856 KB Output is correct
38 Correct 10 ms 9728 KB Output is correct
39 Correct 334 ms 17360 KB Output is correct
40 Correct 310 ms 17388 KB Output is correct
41 Correct 340 ms 17384 KB Output is correct
42 Correct 453 ms 17392 KB Output is correct
43 Correct 274 ms 17396 KB Output is correct
44 Correct 137 ms 17012 KB Output is correct
45 Correct 352 ms 21004 KB Output is correct
46 Correct 163 ms 24780 KB Output is correct
47 Correct 381 ms 23436 KB Output is correct
48 Correct 1008 ms 37948 KB Output is correct
49 Correct 1093 ms 36804 KB Output is correct
50 Correct 375 ms 23456 KB Output is correct
51 Correct 405 ms 23540 KB Output is correct
52 Correct 520 ms 23544 KB Output is correct
53 Correct 841 ms 24028 KB Output is correct
54 Correct 780 ms 21512 KB Output is correct
55 Correct 58 ms 10648 KB Output is correct
56 Correct 25 ms 10496 KB Output is correct
57 Correct 138 ms 17292 KB Output is correct
58 Correct 48 ms 17260 KB Output is correct
59 Correct 173 ms 24620 KB Output is correct
60 Correct 1055 ms 38756 KB Output is correct
61 Correct 373 ms 23544 KB Output is correct
62 Correct 366 ms 23412 KB Output is correct
63 Correct 531 ms 23504 KB Output is correct
64 Correct 1086 ms 24684 KB Output is correct
65 Correct 710 ms 22520 KB Output is correct
66 Correct 1036 ms 33528 KB Output is correct
67 Correct 197 ms 24548 KB Output is correct
68 Correct 557 ms 23836 KB Output is correct
69 Correct 579 ms 23984 KB Output is correct
70 Correct 514 ms 23408 KB Output is correct