Submission #284293

# Submission time Handle Problem Language Result Execution time Memory
284293 2020-08-27T07:42:11 Z BeanZ Mousetrap (CEOI17_mousetrap) C++14
45 / 100
1200 ms 72440 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);
        }
}
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 ans = 0;
        ll cnt = 0;
        while (true){
                if (m == t) break;
                ll preans = 0;
                preans = dp[m] + sz[par[m]];
                ans = max(ans, preans - (cnt > 0));
                cnt++;
                m = par[m];
        }
        cout << ans;
}
/*
13 13 1
1 2
2 3
2 4
1 5
5 6
5 7
1 8
8 9
8 10
1 11
11 12
11 13

10 1 4
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10

14 4 1
1 2
2 5
2 6
5 7
5 8
8 10
6 9
1 3
3 14
3 11
11 12
11 13
1 4

12 12 1
1 2
1 3
3 4
4 5
4 6
3 7
7 8
7 9
3 10
10 11
10 12
*/

Compilation message

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:41:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   41 |                 freopen("MOUSETRAP.INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:42:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   42 |                 freopen("MOUSETRAP.OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23936 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 16 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 16 ms 23928 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 16 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 407 ms 69228 KB Output is correct
2 Correct 374 ms 64632 KB Output is correct
3 Correct 1150 ms 72264 KB Output is correct
4 Correct 548 ms 48236 KB Output is correct
5 Correct 1164 ms 72440 KB Output is correct
6 Correct 1200 ms 72440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23936 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 16 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 16 ms 23928 KB Output is correct
8 Correct 17 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 Incorrect 17 ms 23936 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23936 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 16 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 16 ms 23928 KB Output is correct
8 Correct 17 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 407 ms 69228 KB Output is correct
12 Correct 374 ms 64632 KB Output is correct
13 Correct 1150 ms 72264 KB Output is correct
14 Correct 548 ms 48236 KB Output is correct
15 Correct 1164 ms 72440 KB Output is correct
16 Correct 1200 ms 72440 KB Output is correct
17 Incorrect 17 ms 23936 KB Output isn't correct
18 Halted 0 ms 0 KB -