답안 #1028578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1028578 2024-07-20T05:22:39 Z vjudge1 Mousetrap (CEOI17_mousetrap) C++17
100 / 100
532 ms 187464 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
// #define double long double
 
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
 
// mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
 
const int NM = 1e6 + 5;
const int mod = 1e6 + 3;
const int inf = 1e9 + 1;

vector<int> g[NM];
int n, t, m, w[NM], W[NM], dep[NM], par[NM];
vector<pair<int, int>> L;

void dfs(int u = t, int p = 0)
{
    if (u != t)
        W[u] = W[par[u]] + max(0, (int)g[u].size() - 2);
    int be = 0, sb = 0;
    for (auto v : g[u])
    {
        if (v == p)
            continue;
        dep[v] = dep[u] + 1;
        par[v] = u;
        dfs(v, u);
        if (be < w[v])
        {
            sb = be;
            be = w[v];
        }
        else if (sb < w[v])
            sb = w[v];
    }
    if (g[u].size() - !!p == 0)
        w[u] = 0;
    else if (g[u].size() - !!p == 1)
        w[u] = 1;
    else 
        w[u] += sb + g[u].size() - !!p;
}

bool check(int x)
{
    int B = 0;
    for (auto p : L)
        if (p.se + B > x)
        {
            if (B > p.fi)
                return false;
            B++;
        }
    if (B > x)
        return false;
    return true;
}

signed main()
{
    if (fopen("in.txt", "r")) {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    fastIO
    cin >> n >> t >> m;
    for (int i = 2; i <= n; i++)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs();
    int u = m, prv = 0;
    while (u != t)
    {
        for (auto v : g[u])
            if (v != prv && v != par[u])
                L.emplace_back(dep[m] - dep[u], w[v] + W[par[v]] + 1 - !!prv);
        prv = u;
        u = par[u];
    }
    // for (auto t : L)
    //     cout << t.fi << ' ' << t.se << endl;
    sort(L.begin(), L.end());
    int l = -1, r = 2 * n;
    while (l < r - 1)
    {
        int mid = l + r >> 1;
        if (check(mid))
            r = mid;
        else
            l = mid;
    }
    cout << r;
}

Compilation message

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:101:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  101 |         int mid = l + r >> 1;
      |                   ~~^~~
mousetrap.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23900 KB Output is correct
2 Correct 12 ms 23768 KB Output is correct
3 Correct 12 ms 23900 KB Output is correct
4 Correct 12 ms 23848 KB Output is correct
5 Correct 12 ms 23900 KB Output is correct
6 Correct 13 ms 23896 KB Output is correct
7 Correct 13 ms 23900 KB Output is correct
8 Correct 13 ms 23832 KB Output is correct
9 Correct 12 ms 23900 KB Output is correct
10 Correct 12 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 188 ms 81584 KB Output is correct
2 Correct 171 ms 75516 KB Output is correct
3 Correct 491 ms 82420 KB Output is correct
4 Correct 191 ms 53328 KB Output is correct
5 Correct 526 ms 82504 KB Output is correct
6 Correct 469 ms 82412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23900 KB Output is correct
2 Correct 12 ms 23768 KB Output is correct
3 Correct 12 ms 23900 KB Output is correct
4 Correct 12 ms 23848 KB Output is correct
5 Correct 12 ms 23900 KB Output is correct
6 Correct 13 ms 23896 KB Output is correct
7 Correct 13 ms 23900 KB Output is correct
8 Correct 13 ms 23832 KB Output is correct
9 Correct 12 ms 23900 KB Output is correct
10 Correct 12 ms 23900 KB Output is correct
11 Correct 12 ms 23900 KB Output is correct
12 Correct 12 ms 23896 KB Output is correct
13 Correct 12 ms 23896 KB Output is correct
14 Correct 12 ms 23900 KB Output is correct
15 Correct 12 ms 23896 KB Output is correct
16 Correct 13 ms 24020 KB Output is correct
17 Correct 12 ms 23900 KB Output is correct
18 Correct 13 ms 23900 KB Output is correct
19 Correct 12 ms 23860 KB Output is correct
20 Correct 12 ms 23900 KB Output is correct
21 Correct 12 ms 23900 KB Output is correct
22 Correct 12 ms 24016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23900 KB Output is correct
2 Correct 12 ms 23768 KB Output is correct
3 Correct 12 ms 23900 KB Output is correct
4 Correct 12 ms 23848 KB Output is correct
5 Correct 12 ms 23900 KB Output is correct
6 Correct 13 ms 23896 KB Output is correct
7 Correct 13 ms 23900 KB Output is correct
8 Correct 13 ms 23832 KB Output is correct
9 Correct 12 ms 23900 KB Output is correct
10 Correct 12 ms 23900 KB Output is correct
11 Correct 188 ms 81584 KB Output is correct
12 Correct 171 ms 75516 KB Output is correct
13 Correct 491 ms 82420 KB Output is correct
14 Correct 191 ms 53328 KB Output is correct
15 Correct 526 ms 82504 KB Output is correct
16 Correct 469 ms 82412 KB Output is correct
17 Correct 12 ms 23900 KB Output is correct
18 Correct 12 ms 23896 KB Output is correct
19 Correct 12 ms 23896 KB Output is correct
20 Correct 12 ms 23900 KB Output is correct
21 Correct 12 ms 23896 KB Output is correct
22 Correct 13 ms 24020 KB Output is correct
23 Correct 12 ms 23900 KB Output is correct
24 Correct 13 ms 23900 KB Output is correct
25 Correct 12 ms 23860 KB Output is correct
26 Correct 12 ms 23900 KB Output is correct
27 Correct 12 ms 23900 KB Output is correct
28 Correct 12 ms 24016 KB Output is correct
29 Correct 16 ms 23900 KB Output is correct
30 Correct 190 ms 81320 KB Output is correct
31 Correct 192 ms 81220 KB Output is correct
32 Correct 265 ms 187464 KB Output is correct
33 Correct 250 ms 187280 KB Output is correct
34 Correct 473 ms 79056 KB Output is correct
35 Correct 521 ms 78676 KB Output is correct
36 Correct 516 ms 91804 KB Output is correct
37 Correct 532 ms 91796 KB Output is correct
38 Correct 366 ms 93384 KB Output is correct
39 Correct 319 ms 93384 KB Output is correct
40 Correct 344 ms 93388 KB Output is correct