Submission #1028608

# Submission time Handle Problem Language Result Execution time Memory
1028608 2024-07-20T06:01:53 Z MMihalev Torrent (COI16_torrent) C++14
31 / 100
2000 ms 21116 KB
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<tuple>
#include<set>
#include<map>
#include<random>
#include<chrono>
#include<array>
using namespace std;
const int MAX_N=3e5+5;
vector<int>g[MAX_N];
int a,b;
int n;
int parent[MAX_N];
bool used[MAX_N];
void initdfs(int u,int par)
{
    for(int v:g[u])
    {
        if(v==par)continue;
        parent[v]=u;
        initdfs(v,u);
    }
}
void paint(int u,bool col)
{
    used[u]=col;
    for(int v:g[u])
    {
        if(v==parent[u])continue;
        paint(v,col);
    }
}
int dfs(int u,int par,bool wh)
{
    vector<int>mins;
    for(int v:g[u])
    {
        if(v==par or used[v]!=wh)continue;
        mins.push_back(dfs(v,u,wh));
    }
    if(mins.size()==0)return 0;
    sort(mins.rbegin(),mins.rend());
    int ans=0;
    for(int i=0;i<mins.size();i++)
    {
        ans=max(ans,mins[i]+i+1);
    }
    return ans;
}
pair<int,bool>get_ans(int u)
{
    paint(u,1);
    pair<int,bool>res;
    if(dfs(a,0,0)>dfs(b,0,1))
    {
        res.first=dfs(a,0,0);
        res.second=0;
    }
    else
    {
        res.first=dfs(b,0,1);
        res.second=1;
    }
    paint(u,0);
    return res;
}
signed main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    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);
    }

    initdfs(a,0);

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

    bool f;
    int ans;

    tie(ans,f)=get_ans(path[0]);

    for(int i=1;i<path.size();i++)
    {
        if(get_ans(path[i]).second!=get_ans(path[i-1]).second)
        {
            ans=min(ans,get_ans(path[i]).first);
            ans=min(ans,get_ans(path[i-1]).first);
            break;
        }
    }

    cout<<ans<<"\n";
    return 0;
}

Compilation message

torrent.cpp: In function 'int dfs(int, int, bool)':
torrent.cpp:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(int i=0;i<mins.size();i++)
      |                 ~^~~~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:103:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |     for(int i=1;i<path.size();i++)
      |                 ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8796 KB Output is correct
2 Correct 8 ms 8848 KB Output is correct
3 Correct 11 ms 8852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2012 ms 21116 KB Time limit exceeded
2 Halted 0 ms 0 KB -