답안 #284539

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
284539 2020-08-27T15:28:00 Z BeanZ Mousetrap (CEOI17_mousetrap) C++14
100 / 100
1991 ms 209560 KB
#include <bits/stdc++.h>

using namespace std;

#define ll int
#define endl '\n'
const int N = 3e6 + 5;
const int mod = 998244353;
ll n, t, m, ban = 0;
vector<ll> node[1000006];
ll dp[1000006], par[1000006], f[1000006], sz[1000006];
void dfsFirst(ll u, ll v){
        par[u] = v;
        for (auto j : node[u]){
                if (j == v) continue;
                dfsFirst(j, u);
                f[u] |= f[j];
        }
        if (u == m) f[u] = 1;
}
void dfs(ll u, ll p, ll v){
        dp[u] = 0;
        sz[u] = v;
        vector<ll> val;
        for (auto j : node[u]){
                if (j == p) continue;
                dfs(j, u, v + node[j].size() - 2);
                if (!f[j]) val.push_back(dp[j] + 1);
        }
        if (val.size()){
                sort(val.begin(), val.end());
                dp[u]++;
                if (val.size() > 1) dp[u] += val[val.size() - 2];
                dp[u] += max(0, (ll)val.size() - 2);
        }
}
bool chk(ll mid){
        //cout << endl << endl << endl;
        //cout << mid << endl;
        ll cur = m;
        ll turn = 1, used = 0;
        ll ban = 0;
        while (cur != t){
                ll cost = sz[par[cur]];
                vector<ll> val;
                for (auto j : node[cur]){
                        if (j == par[cur] || j == ban) continue;
                        val.push_back(dp[j]);
                }
                sort(val.begin(), val.end());
                for (int i = val.size() - 1; i >= 0; i--){
                        //cout << (val[i] + 1  + cost + used + i) << " " << cur << endl;
                        if ((val[i] + 1  + cost + used + i) > mid){
                                if ((used + 1) > mid) return false;
                                if (turn){
                                        turn--;
                                        used++;
                                } else {
                                        return false;
                                }
                        }
                }
                turn++;
                ban = cur;
                cur = par[cur];
        }
        return true;
}
int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        if (fopen("MOUSETRAP.INP", "r")){
                freopen("MOUSETRAP.INP", "r", stdin);
                freopen("MOUSETRAP.OUT", "w", stdout);
        }
        cin >> n >> t >> m;
        for (int i = 1; i < n; i++){
                ll u, v;
                cin >> u >> v;
                node[u].push_back(v);
                node[v].push_back(u);
        }
        dfsFirst(t, t);
        dfs(t, t, 0);
        ll l = 0, h = 1e9;
        while (l <= h){
                ll mid = (l + h) >> 1;
                if (chk(mid)) h = mid - 1;
                else l = mid + 1;
        }
        cout << l;
}
/*
10 2 10
2 1
3 2
4 2
5 3
6 3
7 4
8 4
9 5
10 5
*/

Compilation message

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:73:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   73 |                 freopen("MOUSETRAP.INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:74:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   74 |                 freopen("MOUSETRAP.OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 18 ms 23936 KB Output is correct
7 Correct 16 ms 23928 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 16 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 398 ms 69092 KB Output is correct
2 Correct 365 ms 64548 KB Output is correct
3 Correct 1156 ms 72068 KB Output is correct
4 Correct 542 ms 47988 KB Output is correct
5 Correct 1134 ms 72184 KB Output is correct
6 Correct 1170 ms 72056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 18 ms 23936 KB Output is correct
7 Correct 16 ms 23928 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 16 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 17 ms 23936 KB Output is correct
13 Correct 17 ms 23936 KB Output is correct
14 Correct 17 ms 24064 KB Output is correct
15 Correct 17 ms 23936 KB Output is correct
16 Correct 18 ms 23936 KB Output is correct
17 Correct 17 ms 23936 KB Output is correct
18 Correct 17 ms 23936 KB Output is correct
19 Correct 17 ms 23936 KB Output is correct
20 Correct 18 ms 23936 KB Output is correct
21 Correct 19 ms 23936 KB Output is correct
22 Correct 17 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 18 ms 23936 KB Output is correct
7 Correct 16 ms 23928 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 16 ms 23808 KB Output is correct
11 Correct 398 ms 69092 KB Output is correct
12 Correct 365 ms 64548 KB Output is correct
13 Correct 1156 ms 72068 KB Output is correct
14 Correct 542 ms 47988 KB Output is correct
15 Correct 1134 ms 72184 KB Output is correct
16 Correct 1170 ms 72056 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 17 ms 23936 KB Output is correct
19 Correct 17 ms 23936 KB Output is correct
20 Correct 17 ms 24064 KB Output is correct
21 Correct 17 ms 23936 KB Output is correct
22 Correct 18 ms 23936 KB Output is correct
23 Correct 17 ms 23936 KB Output is correct
24 Correct 17 ms 23936 KB Output is correct
25 Correct 17 ms 23936 KB Output is correct
26 Correct 18 ms 23936 KB Output is correct
27 Correct 19 ms 23936 KB Output is correct
28 Correct 17 ms 23936 KB Output is correct
29 Correct 16 ms 23808 KB Output is correct
30 Correct 405 ms 82228 KB Output is correct
31 Correct 403 ms 82368 KB Output is correct
32 Correct 676 ms 209560 KB Output is correct
33 Correct 495 ms 209528 KB Output is correct
34 Correct 1178 ms 85212 KB Output is correct
35 Correct 1159 ms 85240 KB Output is correct
36 Correct 1839 ms 97652 KB Output is correct
37 Correct 1991 ms 97784 KB Output is correct
38 Correct 1100 ms 97248 KB Output is correct
39 Correct 1068 ms 97400 KB Output is correct
40 Correct 1063 ms 97436 KB Output is correct