Submission #1087057

# Submission time Handle Problem Language Result Execution time Memory
1087057 2024-09-12T06:57:29 Z FucKanh Torrent (COI16_torrent) C++14
100 / 100
266 ms 31760 KB
#include<bits/stdc++.h>
#define ll long long
#define int ll
#define pii pair<int,int>

using namespace std;

const int maxn = 3e5 + 2;

vector<int> a[maxn];
int vst[maxn], pa[maxn];
pii del;
vector<pii> E;

void dfs (int u) {
    vst[u] = 1;
    for (int v : a[u]) {
        if (v == pa[u]) continue;
        if (!vst[v]) {
            pa[v] = u;
            dfs(v);
        }
    }
}

int dp(int u, int cnt) {
    vst[u] = cnt;
    vector<int> vec;
//    cerr << u << endl;
    for (int v : a[u]) {
        if ( make_pair( min(u,v),max(u,v) ) == del) continue;
        if (pa[u] == v) continue;
        if (vst[v] != cnt) {
            pa[v] = u;
            vec.push_back(dp(v,cnt));
        }
    }
    int ma = 0;
    sort(vec.begin(), vec.end(), greater<int>());
    for (int i = 0; i < vec.size(); i++) {
        ma = max(ma, i + 1 + vec[i]);
    }
    return ma;
}

void solve() {
    int n,m1,m2; cin >> n >> m1 >> m2;
    for (int i = 1; i < n; i++) {
        int x,y; cin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }

    dfs(m1);
    int x = m2;
    while (x != m1) {
        E.push_back( { min(x,pa[x]), max(x,pa[x]) } );
        x = pa[x];
    }

    int l = 0;
    int r = E.size() - 1;
    int cnt = 2;
    int ans = LLONG_MAX;

    if (E.size() <= 1000) {
        for (int mid= 0; mid < E.size(); mid++) {

            del = E[mid];
            pa[m1] = 0;
            int du = dp(m1,++cnt);
            pa[m2] = 0;
            int dv = dp(m2,++cnt);
    //        cerr << del.first << " " << del.second << " du " << du << " dv " << dv << endl;
            ans = min(ans, max(du,dv));
//            if (du > dv) r = mid - 1;
//            else l = mid + 1;
        }
        cout << ans;
        return;
    }

    while (l <= r) {
        int mid = l + r >> 1;

        del = E[mid];
        pa[m1] = 0;
        int du = dp(m1,++cnt);
        pa[m2] = 0;
        int dv = dp(m2,++cnt);
//        cerr << del.first << " " << del.second << " du " << du << " dv " << dv << endl;
        ans = min(ans, max(du,dv));
        if (du > dv) r = mid - 1;
        else l = mid + 1;
    }
    cout << ans;
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    solve();
    return 0;
}

Compilation message

torrent.cpp: In function 'long long int dp(long long int, long long int)':
torrent.cpp:40:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for (int i = 0; i < vec.size(); i++) {
      |                     ~~^~~~~~~~~~~~
torrent.cpp: In function 'void solve()':
torrent.cpp:67:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |         for (int mid= 0; mid < E.size(); mid++) {
      |                          ~~~~^~~~~~~~~~
torrent.cpp:84:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   84 |         int mid = l + r >> 1;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7516 KB Output is correct
2 Correct 5 ms 7516 KB Output is correct
3 Correct 7 ms 7516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 216 ms 25876 KB Output is correct
2 Correct 266 ms 27596 KB Output is correct
3 Correct 228 ms 30148 KB Output is correct
4 Correct 242 ms 28368 KB Output is correct
5 Correct 216 ms 25296 KB Output is correct
6 Correct 220 ms 26192 KB Output is correct
7 Correct 239 ms 31760 KB Output is correct