답안 #284538

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
284538 2020-08-27T15:20:30 Z BeanZ Mousetrap (CEOI17_mousetrap) C++14
25 / 100
1152 ms 85392 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 << mid << endl << 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--){
                        if ((val[i] + 1  + cost + used + i) > mid){
                                if (turn){
                                        turn--;
                                        used++;
                                } else {
                                        return false;
                                }
                        }
                }
                turn++;
                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;
}
/*
*/

Compilation message

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:69:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   69 |                 freopen("MOUSETRAP.INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:70:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   70 |                 freopen("MOUSETRAP.OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 23876 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 409 ms 82296 KB Output is correct
2 Correct 366 ms 76280 KB Output is correct
3 Correct 1152 ms 85112 KB Output is correct
4 Correct 539 ms 54268 KB Output is correct
5 Correct 1142 ms 85392 KB Output is correct
6 Correct 1133 ms 85040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 23876 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 23876 KB Output isn't correct
2 Halted 0 ms 0 KB -