답안 #505754

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
505754 2022-01-11T07:33:57 Z neonaht Torrent (COI16_torrent) C++14
100 / 100
338 ms 28388 KB
#include <bits/stdc++.h>
#define st first
#define nd second
#define ll int64_t
using namespace std;
const int SZ=3e5+7;
vector <int> node[SZ];
int dp[SZ],GetA[SZ],GetB[SZ],a,b,go(0);
 
void dfs(int v,int p,int z) {
    if(node[v].size()==1 && v!=a && v!=b) return ;
    vector <int> went;
    
    for(auto x:node[v]) {
        if(x==p || x==z) continue;
        dfs(x,v,z);
        went.emplace_back(dp[x]);
    }
    int mx(0);
    sort(went.begin(),went.end(),greater<int>());
    
    for(int i=0;i<went.size();i++) mx=max(mx,went[i]+i+1);
    dp[v]=mx;
    return ;
}
 
int main(void) {
    cin.tie(0)->ios::sync_with_stdio(false);
    int n,res((int)1e9);
    cin >> n >> a >> b;
    for(int i=1,x,y;i<n;i++) {
        cin >> x >> y;
        node[x].emplace_back(y);
        node[y].emplace_back(x);
    }
    queue <int> hx;
    hx.push(a);
    GetA[a]=a;
    while(!hx.empty()) {
        auto o=hx.front();
        hx.pop();
 
        for(auto x:node[o]) {
            if(!GetA[x]) {
                GetA[x]=o;
                hx.push(x);
            }
        }
    }
    int you=b;
    while(you!=a) {
        GetB[++go]=you;
        you=GetA[you];
    }
    int l=1,h=go;
    while(l<=h) {
        int mi=(l+h)>>1;
        dfs(a,-1,GetB[mi]);
        dfs(b,-1,GetA[GetB[mi]]);
        res=min(res,max(dp[a],dp[b]));
        if(dp[a]>dp[b]) {
            l=mi+1;
        }else h=mi-1;
    }
    cout << res;
}

Compilation message

torrent.cpp: In function 'void dfs(int, int, int)':
torrent.cpp:22:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for(int i=0;i<went.size();i++) mx=max(mx,went[i]+i+1);
      |                 ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 257 ms 25056 KB Output is correct
2 Correct 274 ms 26384 KB Output is correct
3 Correct 262 ms 27832 KB Output is correct
4 Correct 314 ms 27416 KB Output is correct
5 Correct 301 ms 24824 KB Output is correct
6 Correct 290 ms 25256 KB Output is correct
7 Correct 338 ms 28388 KB Output is correct