Submission #259841

# Submission time Handle Problem Language Result Execution time Memory
259841 2020-08-08T16:50:25 Z Kastanda Mousetrap (CEOI17_mousetrap) C++11
100 / 100
1312 ms 229240 KB
// M
#include<bits/stdc++.h>
using namespace std;
const int N = 1000006;
int n, root, st, SM, P[N], M[N], dp[N];
vector < int > Adj[N], V[N];
void DFS(int v, int p = 0)
{
    P[v] = p;
    for (int i = 0; i < (int)Adj[v].size(); i ++)
        if (Adj[v][i] == p)
            Adj[v].erase(Adj[v].begin() + i);
    if (st == v) M[v] = 1;
    for (int u : Adj[v])
        DFS(u, v), M[v] |= M[u];
    if (v == root)
    {
        int w = -1;
        for (int u : Adj[v])
            if (M[u]) w = u;
        Adj[v] = {w};
    }
}
void DFS2(int v, int p = 0)
{
    if (v != root)
        SM += (int)Adj[v].size() - (M[v] && v != st);
    for (int u : Adj[v])
        DFS2(u, v);
    if (v != root)
        SM -= (int)Adj[v].size() - (M[v] && v != st);
    for (int u : Adj[v])
        if (!M[u])
            V[v].push_back(dp[u]);
    sort(V[v].begin(), V[v].end());
    reverse(V[v].begin(), V[v].end());
    if (M[v])
        return ;
    if (!Adj[v].size())
    {
        dp[v] = SM;
        return ;
    }
    if (Adj[v].size() == 1)
    {
        dp[v] = SM + 1;
        return ;
    }
    dp[v] = V[v][1];
}
inline bool Solve(int md)
{
    int nw = st, cnt = 1;
    while (nw != root)
    {
        int c = 0;
        for (int val : V[nw])
            if (val > md)
                cnt --, c ++;
        md -= c;
        if (cnt < 0 || md < 0)
            return 0;
        nw = P[nw];
        cnt ++;
    }
    return 1;
}
int main()
{
    scanf("%d%d%d", &n, &root, &st);
    for (int i = 1; i < n; i ++)
    {
        int v, u;
        scanf("%d%d", &v, &u);
        Adj[v].push_back(u);
        Adj[u].push_back(v);
    }
    DFS(root);
    DFS2(root);
    int le = -1, ri = n, md;
    while (ri - le > 1)
    {
        md = (le + ri) >> 1;
        if (Solve(md))
            ri = md;
        else
            le = md;
    }
    return !printf("%d\n", ri);
}

Compilation message

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &n, &root, &st);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:74:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &v, &u);
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 34 ms 47352 KB Output is correct
2 Correct 28 ms 47360 KB Output is correct
3 Correct 28 ms 47352 KB Output is correct
4 Correct 28 ms 47224 KB Output is correct
5 Correct 28 ms 47360 KB Output is correct
6 Correct 28 ms 47352 KB Output is correct
7 Correct 28 ms 47352 KB Output is correct
8 Correct 28 ms 47360 KB Output is correct
9 Correct 28 ms 47360 KB Output is correct
10 Correct 29 ms 47360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 529 ms 117496 KB Output is correct
2 Correct 466 ms 110328 KB Output is correct
3 Correct 1279 ms 120916 KB Output is correct
4 Correct 594 ms 84076 KB Output is correct
5 Correct 1272 ms 120976 KB Output is correct
6 Correct 1312 ms 120924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 47352 KB Output is correct
2 Correct 28 ms 47360 KB Output is correct
3 Correct 28 ms 47352 KB Output is correct
4 Correct 28 ms 47224 KB Output is correct
5 Correct 28 ms 47360 KB Output is correct
6 Correct 28 ms 47352 KB Output is correct
7 Correct 28 ms 47352 KB Output is correct
8 Correct 28 ms 47360 KB Output is correct
9 Correct 28 ms 47360 KB Output is correct
10 Correct 29 ms 47360 KB Output is correct
11 Correct 28 ms 47352 KB Output is correct
12 Correct 28 ms 47340 KB Output is correct
13 Correct 29 ms 47360 KB Output is correct
14 Correct 28 ms 47480 KB Output is correct
15 Correct 28 ms 47352 KB Output is correct
16 Correct 29 ms 47360 KB Output is correct
17 Correct 35 ms 47352 KB Output is correct
18 Correct 29 ms 47352 KB Output is correct
19 Correct 28 ms 47360 KB Output is correct
20 Correct 31 ms 47360 KB Output is correct
21 Correct 28 ms 47352 KB Output is correct
22 Correct 28 ms 47352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 47352 KB Output is correct
2 Correct 28 ms 47360 KB Output is correct
3 Correct 28 ms 47352 KB Output is correct
4 Correct 28 ms 47224 KB Output is correct
5 Correct 28 ms 47360 KB Output is correct
6 Correct 28 ms 47352 KB Output is correct
7 Correct 28 ms 47352 KB Output is correct
8 Correct 28 ms 47360 KB Output is correct
9 Correct 28 ms 47360 KB Output is correct
10 Correct 29 ms 47360 KB Output is correct
11 Correct 529 ms 117496 KB Output is correct
12 Correct 466 ms 110328 KB Output is correct
13 Correct 1279 ms 120916 KB Output is correct
14 Correct 594 ms 84076 KB Output is correct
15 Correct 1272 ms 120976 KB Output is correct
16 Correct 1312 ms 120924 KB Output is correct
17 Correct 28 ms 47352 KB Output is correct
18 Correct 28 ms 47340 KB Output is correct
19 Correct 29 ms 47360 KB Output is correct
20 Correct 28 ms 47480 KB Output is correct
21 Correct 28 ms 47352 KB Output is correct
22 Correct 29 ms 47360 KB Output is correct
23 Correct 35 ms 47352 KB Output is correct
24 Correct 29 ms 47352 KB Output is correct
25 Correct 28 ms 47360 KB Output is correct
26 Correct 31 ms 47360 KB Output is correct
27 Correct 28 ms 47352 KB Output is correct
28 Correct 28 ms 47352 KB Output is correct
29 Correct 28 ms 47352 KB Output is correct
30 Correct 512 ms 117496 KB Output is correct
31 Correct 528 ms 117368 KB Output is correct
32 Correct 588 ms 194040 KB Output is correct
33 Correct 620 ms 229240 KB Output is correct
34 Correct 1293 ms 120948 KB Output is correct
35 Correct 1279 ms 121132 KB Output is correct
36 Correct 1262 ms 129400 KB Output is correct
37 Correct 1272 ms 129504 KB Output is correct
38 Correct 965 ms 121156 KB Output is correct
39 Correct 1006 ms 121212 KB Output is correct
40 Correct 955 ms 121040 KB Output is correct