Submission #916583

#TimeUsernameProblemLanguageResultExecution timeMemory
916583406Torrent (COI16_torrent)C++17
100 / 100
441 ms29480 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 3e5 + 50;
bitset<N> mark;
vector<int> adj[N];
int par[N], n, a, b, dp[N];

void dfs_path(int v) {
    mark[v] = true;
    for (auto u: adj[v])
        if (!mark[u]) {
            par[u] = v;
            dfs_path(u);
        }
}

void dfs(int v) {
    mark[v] = true;
    vector<int> vc;
    for (auto u: adj[v]) {
        if (!mark[u]) {
            dfs(u);
            vc.push_back(dp[u]);
        }
    }
    sort(vc.rbegin(), vc.rend());
    dp[v] = 0;
    for (int i = 0; i < vc.size(); i++)
        dp[v] = max(dp[v], vc[i] + i + 1);
}

int check(int a, int O) {
    mark = 0;
    mark[O] = true;
    dfs(a);
    return dp[a];
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> a >> b;
    assert(a != b);
    a--, b--;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs_path(a);

    vector<int> path{b};
    while (path.back() != a) 
        path.push_back(par[path.back()]);

    int l = 0, r = path.size() - 1;
    while (r - l > 3) {
        int mid = (r + l) / 2;
        if (check(b, path[mid + 1]) > check(a, path[mid])) 
            r = mid;
        else
            l = mid;
    }
    int ans = n;
    for (; l < r; l++)
        ans = min(ans, max(check(b, path[l + 1]), check(a, path[l])));
    cout << ans << '\n';
    return 0;
}

Compilation message (stderr)

torrent.cpp: In function 'void dfs(int)':
torrent.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i = 0; i < vc.size(); i++)
      |                     ~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...