Submission #314019

# Submission time Handle Problem Language Result Execution time Memory
314019 2020-10-17T21:22:35 Z JovanK26 Torrent (COI16_torrent) C++14
100 / 100
580 ms 25428 KB
#include <bits/stdc++.h>
 
using namespace std;
int n,a,b,x,y;
vector<int> v[300001];
int prt[300001];
int f[300001];
void dfs(int start)
{
    for(int j : v[start])
    {
        if(j==prt[start])
        {
            continue;
        }
        prt[j]=start;
        dfs(j);
    }
}
int solve(int start,int prev,int cutx,int cuty)
{
    f[start]=0;
    vector<int> temp;
    for(int j : v[start])
    {
        if(j==prev)continue;
        if(start==cutx && j==cuty)continue;
        if(j==cutx && start==cuty)continue;
        solve(j,start,cutx,cuty);
        temp.push_back(f[j]);
    }
    sort(temp.rbegin(),temp.rend());
    for(int i=0;i<temp.size();i++)
    {
        f[start]=max(f[start],temp[i]+i+1);
    }
    return f[start];
}
int delta(int cur)
{
    return solve(a,0,cur,prt[cur])-solve(b,0,cur,prt[cur]);
}
int comp(int cur)
{
    return max(solve(a,0,cur,prt[cur]),solve(b,0,cur,prt[cur]));
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> a >> b;
    for(int i=0;i<n-1;i++)
    {
       cin >> x >> y;
       v[x].push_back(y);
       v[y].push_back(x);
    }
    vector<int> parents;
    dfs(b);
    for(int i=a;i!=b;i=prt[i])
    {
        parents.push_back(i);
    }
    int l=0;
    int r=parents.size();
    int m;
    while(l<=r)
    {
        m=(l+r)/2;
        if(delta(parents[m])<0)
        {
            l=m+1;
        }
        else
        {
            r=m-1;
        }
    }
    int rez=comp(parents[r+1]);
    if(r+1)rez=min(rez,comp(parents[r]));
    //if(l+1<parents.size())rez=min(rez,comp(parents[l+1]));
    cout << rez;
    return 0;
}

Compilation message

torrent.cpp: In function 'int solve(int, int, int, int)':
torrent.cpp:33:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for(int i=0;i<temp.size();i++)
      |                 ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 6 ms 7424 KB Output is correct
3 Correct 6 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 562 ms 21880 KB Output is correct
2 Correct 575 ms 23384 KB Output is correct
3 Correct 545 ms 25124 KB Output is correct
4 Correct 580 ms 24636 KB Output is correct
5 Correct 519 ms 21116 KB Output is correct
6 Correct 478 ms 21984 KB Output is correct
7 Correct 479 ms 25428 KB Output is correct