답안 #846502

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
846502 2023-09-07T16:28:57 Z voi Torrent (COI16_torrent) C++17
0 / 100
93 ms 33520 KB
#include <bits/stdc++.h>
#define pll pair <long long, long long>
#define fi first
#define se second

using namespace std;
#define task "code"
#define all(s) s.begin(), s.end()

typedef long long ll;

const int ar = 3e5+2;
const ll mod = 998244353;
const int oo = 1e9;

int n, a, b, child[ar], dp[ar], pre[ar], suf[ar], f[ar];
vector <int> g[ar], trace, tg[ar];

void caltrace(int u, int p) {
    if(trace.empty() || trace.back() != b) {
        if(trace.size()) child[trace.back()] = u;
        trace.push_back(u);
    }
    for(auto v : g[u]) if(v != p)
        caltrace(v, u);
    if(trace.back() != b)
        trace.pop_back(), child[trace.back()] = 0;
}

void dfs(int u, int p) {
    for(auto v : g[u]) if(v != p) {
        dfs(v, u);
//        if(u == 3) cout << v << " 3333\n";
        if(v != child[u]) {
            tg[u].push_back(dp[v]);
        }
    }
    sort(all(tg[u]), greater<int>());
//    if(u == 3) cout << tg.size() << '\n';
    for(int i = 0; i < tg[u].size(); ++i)
        dp[u] = max(dp[u], tg[u][i] + i + 1);
    for(int i = 0; i < tg[u].size(); ++i)
        if(dp[u] == tg[u][i] + i + 1) f[u] = i;
//    cout << u << ' ' << dp[u] << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    if(fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }

    cin >> n >> a >> b;
    for(int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    caltrace(a, 0);
    dfs(a, 0);
    if(a == b) return cout << dp[a], 0;
    int m = trace.size();
//    for(auto x : trace) cout << x << ' '; cout << '\n';
    if(m == 2)
        return cout << max(dp[a], dp[b]), 0;

    pre[0] = dp[trace[0]];
    for(int i = 1; i < m - 1; ++i) {
        int u = trace[i - 1], v = trace[i];
        int j = 0;
        for(; j < tg[u].size(); ++j)
            if(tg[u][j] < dp[v]) break;
        if(j <= f[u]) pre[i] = max(pre[i - 1] + 1, dp[v] + j + i);
        else pre[i] = max(pre[i - 1], dp[v] + j + i);
//        pre[i] = min(max(pre[i - 1], dp[v]) + i, max(pre[i - 1], dp[v] + f[u] + i));
    }

    suf[m - 1] = dp[trace.back()];
    for(int i = m - 2; i > 0; --i) {
        int u = trace[i + 1], v = trace[i];
        int j = 0;
        for(; j < tg[u].size(); ++j)
            if(tg[u][j] < dp[v]) break;
        if(j <= f[u]) suf[i] = max(suf[i + 1] + 1, dp[v] + j + m - i - 1);
        else suf[i] = max(suf[i + 1], dp[v] + j + m - i - 1);
//        suf[i] = min(max(suf[i + 1], dp[v]) + m - i - 1, max(suf[i + 1], dp[v] + f[u] + m - i - 1));
    }

//    cout << '\n';
    int res = oo;
    for(int i = 1; i < m; ++i)
//        cout << pre[i - 1] << ' ' << suf[i] << '\n',
        res = min(res, max(pre[i - 1], suf[i]));
    cout << res;

    return 0;
}

Compilation message

torrent.cpp: In function 'void dfs(int, int)':
torrent.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for(int i = 0; i < tg[u].size(); ++i)
      |                    ~~^~~~~~~~~~~~~~
torrent.cpp:42:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for(int i = 0; i < tg[u].size(); ++i)
      |                    ~~^~~~~~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:74:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |         for(; j < tg[u].size(); ++j)
      |               ~~^~~~~~~~~~~~~~
torrent.cpp:85:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for(; j < tg[u].size(); ++j)
      |               ~~^~~~~~~~~~~~~~
torrent.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 16984 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 93 ms 33520 KB Output isn't correct
2 Halted 0 ms 0 KB -