Submission #62911

# Submission time Handle Problem Language Result Execution time Memory
62911 2018-07-30T19:18:57 Z Benq Torrent (COI16_torrent) C++11
100 / 100
201 ms 53220 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 300001;

int n,a,b,par[MX];
vi adj[MX];
bool inPath[MX];

void dfs1(int x) {
    for (int i: adj[x]) if (i != par[x]) {
        par[i] = x;
        dfs1(i);
    }
}

int solve(int x, int y) {
    vi al;
    for (int i: adj[x]) if (!inPath[i] && i != y) al.pb(solve(i,x));
    sort(al.rbegin(),al.rend());
    int mx = 0;
    F0R(i,sz(al)) mx = max(mx,al[i]+i+1);
    return mx;
}

int ans = 0;
vi v;
vector<vi> tmp;

bool canDecrease() {
    vi dist(sz(v));
    F0R(i,sz(v)) dist[i] = MOD;
    priority_queue<pi,vpi,greater<pi>> p;
    p.push({dist[0] = 0, 0});
    p.push({dist[sz(v)-1] = 0, sz(v)-1});
    while (sz(p)) {
        auto a = p.top(); p.pop();
        if (a.f > dist[a.s]) continue;
        if (dist[a.s] > ans) return 0;
        int lst = -1;
        F0R(i,sz(tmp[a.s])) {
            if (dist[a.s]+i+1+tmp[a.s][i] > ans-1) return 0;
            if (dist[a.s]+i+1+tmp[a.s][i] == ans-1) lst = i;
        }
        if (a.s && a.f+lst+2 < dist[a.s-1]) p.push({dist[a.s-1] = a.f+lst+2, a.s-1});
        if (a.s+1 < sz(dist) && a.f+lst+2 < dist[a.s+1]) p.push({dist[a.s+1] = a.f+lst+2, a.s+1});
    }
    return 1;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> a >> b;
    F0R(i,n-1) {
        int x,y; cin >> x >> y;
        adj[x].pb(y), adj[y].pb(x);
    }
    dfs1(a);
    for (; b != a; b = par[b]) v.pb(b); 
    v.pb(b); for (int i: v) inPath[i] = 1;
    for (int i: v) {
        vi z;
        for (int j: adj[i]) if (!inPath[j]) z.pb(solve(j,i));
        sort(z.rbegin(),z.rend());;
        tmp.pb(z);
    }
    F0R(i,sz(tmp)) {
        int z = min(i,sz(tmp)-1-i);
        int mx = z;
        if (sz(tmp[i])) {
            F0R(j,sz(tmp[i])) mx = max(mx,z+j+1+tmp[i][j]);
            if (abs(2*i-sz(tmp)+1) > 1) {
                mx ++;
                // cout << "BLAH " << i << " " << n << "\n";
            }
        }
        ans = max(ans,mx);
    }
    if (canDecrease()) ans --;
    cout << ans;
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# Verdict Execution time Memory Grader output
1 Correct 10 ms 7416 KB Output is correct
2 Correct 10 ms 7564 KB Output is correct
3 Correct 10 ms 7728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 25316 KB Output is correct
2 Correct 183 ms 30988 KB Output is correct
3 Correct 190 ms 36300 KB Output is correct
4 Correct 180 ms 40264 KB Output is correct
5 Correct 169 ms 41324 KB Output is correct
6 Correct 165 ms 45888 KB Output is correct
7 Correct 201 ms 53220 KB Output is correct