답안 #705588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705588 2023-03-04T17:23:57 Z Kahou Mousetrap (CEOI17_mousetrap) C++14
65 / 100
844 ms 171400 KB
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 1e6 + 50;
int n, t, m, val[N], dp[N], ps[N], k; 
bool mark[N];
vector<int> adj[N];
vector<int> vp;
vector<pii> vc;

void dfs(int u, int p=0) {
        if (u == t) return;
        int mx1 = 0, mx2 = 0;
        for (int v:adj[u]) {
                if (v == p) continue;
                dfs(v, u);
                if (mark[v]) {
                        mark[u] = 1;
                        val[u] = val[v] + adj[u].size()-1-(p>0);
                }
                else {
                        if (dp[v] > mx1) {
                                mx2 = mx1;
                                mx1 = dp[v];
                        }
                        else if (dp[v] > mx2) {
                                mx2 = dp[v];
                        }
                }
        }
        if (!mark[u]) {
                dp[u] = adj[u].size()-(p>0) + mx2;
        }
        else {
                vp.push_back(u);
                for (int v:adj[u]) {
                        if (v == p) continue;
                        if (mark[v]) continue;
                        vc.push_back({u, dp[v] + val[u]});
                }
        }
}

bool isval(int x) {
        int prv = 0;
        int tmp = 0;
        for (pii p:vc) {
                if (p.F != prv) {
                        ps[p.F] = ps[prv];
                        tmp = ps[prv];
                        prv = p.F;
                }
                if (tmp+p.S > x) {
                        ps[p.F]++;
                }
                if (ps[p.F] > min(p.F, x)) return 0;
        }
        return 1;
}
void solve() {
        cin >> n >> t >> m;
        for (int i = 1; i <= n-1; i++) {
                int u, v;
                cin >> u >> v;
                adj[u].push_back(v);
                adj[v].push_back(u);
        }
        mark[t] = 1;
        dfs(m);
        reverse(vp.begin(), vp.end());
        reverse(vc.begin(), vc.end());
        for (pii &x : vc) {
                if (x.F != vp[k]) k++;
                x.F = k+1;
        }
        k = vp.size();
        int dw = -1, up = 1e9;
        while (up-dw > 1) {
                int md = (up+dw)>>1;
                if (isval(md)) up = md;
                else dw = md;
        }
        cout << up << endl;
}

int main() {
        ios::sync_with_stdio(0), cin.tie(0);
        solve();
        return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23792 KB Output is correct
2 Correct 15 ms 23788 KB Output is correct
3 Correct 16 ms 23816 KB Output is correct
4 Correct 14 ms 23764 KB Output is correct
5 Correct 15 ms 23804 KB Output is correct
6 Correct 14 ms 23780 KB Output is correct
7 Correct 14 ms 23720 KB Output is correct
8 Correct 15 ms 23812 KB Output is correct
9 Correct 14 ms 23792 KB Output is correct
10 Correct 18 ms 23716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 340 ms 64028 KB Output is correct
2 Correct 303 ms 60496 KB Output is correct
3 Correct 844 ms 65496 KB Output is correct
4 Correct 387 ms 46408 KB Output is correct
5 Correct 726 ms 66800 KB Output is correct
6 Correct 793 ms 66740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23792 KB Output is correct
2 Correct 15 ms 23788 KB Output is correct
3 Correct 16 ms 23816 KB Output is correct
4 Correct 14 ms 23764 KB Output is correct
5 Correct 15 ms 23804 KB Output is correct
6 Correct 14 ms 23780 KB Output is correct
7 Correct 14 ms 23720 KB Output is correct
8 Correct 15 ms 23812 KB Output is correct
9 Correct 14 ms 23792 KB Output is correct
10 Correct 18 ms 23716 KB Output is correct
11 Correct 16 ms 23764 KB Output is correct
12 Correct 14 ms 23820 KB Output is correct
13 Correct 17 ms 23812 KB Output is correct
14 Correct 14 ms 23892 KB Output is correct
15 Correct 13 ms 23892 KB Output is correct
16 Correct 17 ms 23764 KB Output is correct
17 Correct 21 ms 23844 KB Output is correct
18 Correct 14 ms 23764 KB Output is correct
19 Correct 13 ms 23860 KB Output is correct
20 Correct 16 ms 23892 KB Output is correct
21 Correct 15 ms 23764 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23792 KB Output is correct
2 Correct 15 ms 23788 KB Output is correct
3 Correct 16 ms 23816 KB Output is correct
4 Correct 14 ms 23764 KB Output is correct
5 Correct 15 ms 23804 KB Output is correct
6 Correct 14 ms 23780 KB Output is correct
7 Correct 14 ms 23720 KB Output is correct
8 Correct 15 ms 23812 KB Output is correct
9 Correct 14 ms 23792 KB Output is correct
10 Correct 18 ms 23716 KB Output is correct
11 Correct 340 ms 64028 KB Output is correct
12 Correct 303 ms 60496 KB Output is correct
13 Correct 844 ms 65496 KB Output is correct
14 Correct 387 ms 46408 KB Output is correct
15 Correct 726 ms 66800 KB Output is correct
16 Correct 793 ms 66740 KB Output is correct
17 Correct 16 ms 23764 KB Output is correct
18 Correct 14 ms 23820 KB Output is correct
19 Correct 17 ms 23812 KB Output is correct
20 Correct 14 ms 23892 KB Output is correct
21 Correct 13 ms 23892 KB Output is correct
22 Correct 17 ms 23764 KB Output is correct
23 Correct 21 ms 23844 KB Output is correct
24 Correct 14 ms 23764 KB Output is correct
25 Correct 13 ms 23860 KB Output is correct
26 Correct 16 ms 23892 KB Output is correct
27 Correct 15 ms 23764 KB Output is correct
28 Correct 13 ms 23764 KB Output is correct
29 Correct 16 ms 23792 KB Output is correct
30 Correct 316 ms 72284 KB Output is correct
31 Correct 302 ms 72444 KB Output is correct
32 Correct 387 ms 171400 KB Output is correct
33 Correct 363 ms 166352 KB Output is correct
34 Correct 702 ms 73360 KB Output is correct
35 Correct 688 ms 73476 KB Output is correct
36 Correct 810 ms 85812 KB Output is correct
37 Correct 790 ms 85844 KB Output is correct
38 Correct 605 ms 87500 KB Output is correct
39 Incorrect 645 ms 87204 KB Output isn't correct
40 Halted 0 ms 0 KB -