Submission #511011

# Submission time Handle Problem Language Result Execution time Memory
511011 2022-01-15T04:21:52 Z Monarchuwu Mousetrap (CEOI17_mousetrap) C++17
100 / 100
850 ms 166732 KB
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const int N = 1e6 + 16;
int n, t, m;
vector<int> g[N];

bool checksubtask2() { // It is guaranteed that a passage between rooms m and t exists.
    for (int v : g[m])
        if (v == t) return true;
    return false;
}
namespace subtask2 {
    int dfs(int u, int p) {
        if (g[u].size() == 1) return 0; // hết đường
        if (g[u].size() == 2) return 1; // còn 1 đường nhưng sẽ bị chặn
        vector<int> res;
        for (int v : g[u]) if (v != p) {
            res.push_back(dfs(v, u) + 1); // nếu chuột chạy vào v thì min bước là ? (+1 lượt dọn phân khi quay lại)
        }
        sort(all(res), greater<int>());
        return g[u].size() - 2 + res[1]; // chặn đường thứ nhất, chuột đi vào đường thứ nhì
    }
    void solve() {
        cout << dfs(m, t) << '\n';
    }
}

namespace subtask4 {
    // par   : để đi từ m->t
    // child : để đi từ t->m
    int par[N], child[N];
    bool dfs_find(int u) { // có phải subtree chứa m ?
        for (int v : g[u]) if (v != par[u]) {
            par[v] = u;
            if (dfs_find(v)) child[u] = v;
        }
        return child[u] || u == m;
    }

    int dfs_dp(int u, int p) {
        if (g[u].size() == 1) return 0; // hết đường
        if (g[u].size() == 2) return 1; // còn 1 đường nhưng sẽ bị chặn
        vector<int> res;
        for (int v : g[u]) if (v != p) {
            res.push_back(dfs_dp(v, u) + 1); // nếu chuột chạy vào v thì min bước là ? (+1 lượt dọn phân khi quay lại)
        }
        sort(all(res), greater<int>());
        return g[u].size() - 2 + res[1]; // chặn đường thứ nhất, chuột đi vào đường thứ nhì
    }
    vector<int> dp[N], path;
    void getPath() {
        int u = child[t], cnt(0), cnttmp(0);
        while (u) {
            cnttmp = cnt + max(0, (int)g[u].size() - 2 - (child[u] != 0)); //chặn nhưng chừa 1 đường chui vào
            path.push_back(u);

            for (int v : g[u]) if (v != par[u] && v != child[u]) {
                dp[u].push_back(dfs_dp(v, u) + 1 + cnttmp);
            }
            sort(all(dp[u]), greater<int>());

            cnt += max(0, (int)g[u].size() - 1 - (child[u] != 0)); // chặn hết
            u = child[u];
        }

        //path.push_back(m);
        //dp[m].push_back(dfs_dp(m, par[m]) + cnt);
    }

    bool check(int k) { // bắt được chuột trong k lượt?
        int cnt(0), used;
        for (int u : path) {
            ++cnt; // tăng 1 lượt đặt chặn

            priority_queue<int> q;
            for (int v : dp[u]) {
                q.push(v);
            }

            used = 0;
            while (!q.empty() && q.top() - used > k) {
                q.pop();
                if (cnt == 0 || k == 0) return false; // không thể chặn
                --cnt, --k; // chặn và tốn 1 lượt
                ++used;
            }
        }
        return true;
    }
    void solve() {
        dfs_find(t);
        getPath();
        reverse(all(path)); // m->child[t]

        int lo(0), hi(n * 2), mid;
        while (lo <= hi) {
            mid = (lo + hi) >> 1;
            if (check(mid))
                hi = mid - 1;
            else lo = mid + 1;
        }
        cout << hi + 1 << '\n';
    }
}

int main() {
    cin.tie(NULL)->sync_with_stdio(false);
    cin >> n >> t >> m;
    for (int i = 1, u, v; i < n; ++i) {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    if (checksubtask2())
        subtask2::solve();
    else subtask4::solve();
}
/**  /\_/\
 *  (= ._.)
 *  / >0  \>1
**/
// 5 1 4 1 2 2 3 3 4 4 5
// 4 1 4 1 2 2 3 3 4 4 5
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47180 KB Output is correct
2 Correct 27 ms 47180 KB Output is correct
3 Correct 27 ms 47392 KB Output is correct
4 Correct 27 ms 47268 KB Output is correct
5 Correct 22 ms 47180 KB Output is correct
6 Correct 23 ms 47260 KB Output is correct
7 Correct 26 ms 47156 KB Output is correct
8 Correct 23 ms 47180 KB Output is correct
9 Correct 27 ms 47172 KB Output is correct
10 Correct 22 ms 47264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 305 ms 79160 KB Output is correct
2 Correct 319 ms 75920 KB Output is correct
3 Correct 719 ms 80248 KB Output is correct
4 Correct 359 ms 64052 KB Output is correct
5 Correct 659 ms 80252 KB Output is correct
6 Correct 639 ms 80252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47180 KB Output is correct
2 Correct 27 ms 47180 KB Output is correct
3 Correct 27 ms 47392 KB Output is correct
4 Correct 27 ms 47268 KB Output is correct
5 Correct 22 ms 47180 KB Output is correct
6 Correct 23 ms 47260 KB Output is correct
7 Correct 26 ms 47156 KB Output is correct
8 Correct 23 ms 47180 KB Output is correct
9 Correct 27 ms 47172 KB Output is correct
10 Correct 22 ms 47264 KB Output is correct
11 Correct 25 ms 47168 KB Output is correct
12 Correct 23 ms 47308 KB Output is correct
13 Correct 23 ms 47256 KB Output is correct
14 Correct 34 ms 47276 KB Output is correct
15 Correct 23 ms 47312 KB Output is correct
16 Correct 24 ms 47272 KB Output is correct
17 Correct 23 ms 47228 KB Output is correct
18 Correct 23 ms 47336 KB Output is correct
19 Correct 23 ms 47236 KB Output is correct
20 Correct 26 ms 47328 KB Output is correct
21 Correct 28 ms 47308 KB Output is correct
22 Correct 23 ms 47308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47180 KB Output is correct
2 Correct 27 ms 47180 KB Output is correct
3 Correct 27 ms 47392 KB Output is correct
4 Correct 27 ms 47268 KB Output is correct
5 Correct 22 ms 47180 KB Output is correct
6 Correct 23 ms 47260 KB Output is correct
7 Correct 26 ms 47156 KB Output is correct
8 Correct 23 ms 47180 KB Output is correct
9 Correct 27 ms 47172 KB Output is correct
10 Correct 22 ms 47264 KB Output is correct
11 Correct 305 ms 79160 KB Output is correct
12 Correct 319 ms 75920 KB Output is correct
13 Correct 719 ms 80248 KB Output is correct
14 Correct 359 ms 64052 KB Output is correct
15 Correct 659 ms 80252 KB Output is correct
16 Correct 639 ms 80252 KB Output is correct
17 Correct 25 ms 47168 KB Output is correct
18 Correct 23 ms 47308 KB Output is correct
19 Correct 23 ms 47256 KB Output is correct
20 Correct 34 ms 47276 KB Output is correct
21 Correct 23 ms 47312 KB Output is correct
22 Correct 24 ms 47272 KB Output is correct
23 Correct 23 ms 47228 KB Output is correct
24 Correct 23 ms 47336 KB Output is correct
25 Correct 23 ms 47236 KB Output is correct
26 Correct 26 ms 47328 KB Output is correct
27 Correct 28 ms 47308 KB Output is correct
28 Correct 23 ms 47308 KB Output is correct
29 Correct 23 ms 47288 KB Output is correct
30 Correct 321 ms 95840 KB Output is correct
31 Correct 397 ms 96128 KB Output is correct
32 Correct 426 ms 166732 KB Output is correct
33 Correct 355 ms 158552 KB Output is correct
34 Correct 840 ms 96844 KB Output is correct
35 Correct 804 ms 96856 KB Output is correct
36 Correct 850 ms 106716 KB Output is correct
37 Correct 850 ms 106764 KB Output is correct
38 Correct 804 ms 108508 KB Output is correct
39 Correct 743 ms 108592 KB Output is correct
40 Correct 734 ms 108528 KB Output is correct