답안 #474554

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
474554 2021-09-19T02:56:40 Z hoangtung_pro Torrent (COI16_torrent) C++14
100 / 100
439 ms 24660 KB
#include<bits/stdc++.h>

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define bit(s, i) (s & (1<<i))
#define rall(v) v.rbegin(), v.rend()

using namespace std;

const int N = 3e5 + 5;
const int mod = 1e9+7;

typedef long long ll;
typedef pair < int, int > ii;

int n, A, B, dp[N];

vector < int > adj[N];
vector < int > tung, goo;
void dfs(int u, int p) {
    tung.pb(u);

    if(u == B) goo = tung;

    for(int v : adj[u]) if(v != p) dfs(v, u);

    tung.pop_back();
}

void Dfs(int u, int p, int dap) {
    dp[u] = 0;
    if(u == dap) return;

    for(int v : adj[u]) if(v != p && v != dap) Dfs(v, u, dap);

    tung.clear();

    for(int v : adj[u]) if(v != p && v != dap) tung.pb(dp[v]);

    sort(rall(tung));
    for(int i=0;i<tung.size();++i) dp[u] = max(dp[u], tung[i] + i + 1);
}

int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//    freopen("trash.inp","r",stdin);
//    freopen("trash.out","w",stdout);

    cin >> n >> A >> B;
    for(int i=1, u, v;i<n;++i)
    {
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(A, 0);
    Dfs(A, 0, 0);
    int res = dp[A];
    int l = 0, r = goo.size() - 2;
    while(l <= r) {
        int mid = (l + r) / 2;
        Dfs(A, 0, goo[mid + 1]);
        Dfs(B, 0, goo[mid]);
        res = min(res, max(dp[A], dp[B]));
        if(dp[A] < dp[B]) l = mid + 1;
        else r = mid - 1;
    }
    cout << res;
}




Compilation message

torrent.cpp: In function 'void Dfs(int, int, int)':
torrent.cpp:43:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for(int i=0;i<tung.size();++i) dp[u] = max(dp[u], tung[i] + i + 1);
      |                 ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Correct 5 ms 7372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 439 ms 20504 KB Output is correct
2 Correct 410 ms 22108 KB Output is correct
3 Correct 400 ms 24456 KB Output is correct
4 Correct 356 ms 22964 KB Output is correct
5 Correct 381 ms 20124 KB Output is correct
6 Correct 321 ms 20680 KB Output is correct
7 Correct 299 ms 24660 KB Output is correct