답안 #284290

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
284290 2020-08-27T07:30:04 Z BeanZ Mousetrap (CEOI17_mousetrap) C++14
45 / 100
1140 ms 81400 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];
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){
        dp[u] = 0;
        vector<ll> val;
        for (auto j : node[u]){
                if (j == p || j == ban) continue;
                dfs(j, u);
                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);
        ll ans = 0;
        ll cnt = 0;
        while (true){
                if (m == t) break;
                ll preans = 0;
                dfs(m, par[m]);
                ll cur = par[m];
                preans = dp[m];
                while (cur != t){
                        preans = preans + node[cur].size() - 2;
                        cur = par[cur];
                }
                //cout << preans << endl;
                ans = max(ans, preans - (cnt > 0));
                cnt++;
                ban = m;
                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:40:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   40 |                 freopen("MOUSETRAP.INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 17 ms 23936 KB Output is correct
5 Correct 16 ms 23936 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23808 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 401 ms 65712 KB Output is correct
2 Correct 359 ms 61636 KB Output is correct
3 Correct 1140 ms 68944 KB Output is correct
4 Correct 551 ms 46632 KB Output is correct
5 Correct 1132 ms 81272 KB Output is correct
6 Correct 1109 ms 81400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 17 ms 23936 KB Output is correct
5 Correct 16 ms 23936 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23808 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 Incorrect 16 ms 23808 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 17 ms 23936 KB Output is correct
5 Correct 16 ms 23936 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23808 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 401 ms 65712 KB Output is correct
12 Correct 359 ms 61636 KB Output is correct
13 Correct 1140 ms 68944 KB Output is correct
14 Correct 551 ms 46632 KB Output is correct
15 Correct 1132 ms 81272 KB Output is correct
16 Correct 1109 ms 81400 KB Output is correct
17 Incorrect 16 ms 23808 KB Output isn't correct
18 Halted 0 ms 0 KB -