답안 #846678

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
846678 2023-09-08T08:52:27 Z vjudge1 Torrent (COI16_torrent) C++17
100 / 100
372 ms 36916 KB
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
const ll nmax=3e5+5;
int n, a, b, dp[nmax], now, ans = 1e9;
vector<int> g[nmax], vt[nmax], res;
bool vis[nmax];
pair<int, int> p;

bool kt(int u, int v)
{
    if(u == p.fi && v == p.se)
        return false;
    if(u == p.se && v == p.fi)
        return false;
    return true;
}

void dis(int u)
{
    vis[u] = true;
    if(u == b)
    {
        dp[b] = 1;
        res.push_back(b);
        return;
    }
    for(auto v: g[u])
    {
        if(!vis[v])
        {
            dis(v);
            dp[u] += dp[v];
        }
    }
    if(dp[u] != 0) res.push_back(u);
}

void dfs(int u)
{
    vt[u].clear();
    dp[u]=1;
    for(auto v:g[u])
    {
        if(dp[v] == 0 && kt(u, v))
        {
            dfs(v);
            vt[u].push_back(dp[v]);
        }
    }
    sort(vt[u].begin(), vt[u].end(), greater<int>());
    for(int i=0; i<vt[u].size(); i++)
        dp[u]=max(dp[u], vt[u][i]+1+i);
}

void sub1()
{
    for(int i=0; i<res.size()-1; i++)
    {
        p = {res[i], res[i+1]};
        memset(dp, 0, sizeof(dp));
        dfs(a), dfs(b);
        now = max(dp[a]-1, dp[b]-1);
        ans = min(ans, now);
    }
    cout<<ans;
}

void sub2()
{
    int l=0, r=res.size()-2, mid;
    while(l <= r)
    {
        mid = (l+r)/2;
        memset(dp, 0, sizeof(dp));
        p = {res[mid], res[mid+1]};
        dfs(a), dfs(b);
        if(dp[a] > dp[b]) r = mid-1;
        else l = mid+1;
        now = max(dp[a]-1, dp[b]-1);
        ans = min(ans, now);
    }
    cout<<ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>n>>a>>b;
    for(int x, y, i=1; i<n; i++)
    {
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dis(a);
    if(n <= 1000) sub1();
    else sub2();
}

Compilation message

torrent.cpp: In function 'void dfs(int)':
torrent.cpp:54:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |     for(int i=0; i<vt[u].size(); i++)
      |                  ~^~~~~~~~~~~~~
torrent.cpp: In function 'void sub1()':
torrent.cpp:60:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for(int i=0; i<res.size()-1; i++)
      |                  ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15708 KB Output is correct
2 Correct 6 ms 15708 KB Output is correct
3 Correct 9 ms 15708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 323 ms 31564 KB Output is correct
2 Correct 316 ms 32968 KB Output is correct
3 Correct 372 ms 34640 KB Output is correct
4 Correct 252 ms 34172 KB Output is correct
5 Correct 316 ms 31992 KB Output is correct
6 Correct 242 ms 32396 KB Output is correct
7 Correct 223 ms 36916 KB Output is correct