답안 #846691

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
846691 2023-09-08T09:06:29 Z noot_coder Torrent (COI16_torrent) C++17
69 / 100
295 ms 45248 KB
#include <bits/stdc++.h>
#define task ""
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<long long, long long>

using namespace std;
using ll = long long;

const ll mod = 998244353;
const int inf = 1e9 + 1;
const int arr = 1000001;

ll n, a, b, dp[arr], child[arr];
vector<ll> adj[arr];
vector<ll> trace;
void findpath(ll u, ll p) {
    if(trace.empty() || trace.back() != b){
        trace.push_back(u);
    }
    for(auto v : adj[u]) if(v != p)
        findpath(v, u);
    if(trace.back() != b)
        trace.pop_back();
}
// bool cmp(ll a, ll b){
//     return a > b;
// }
void dfs(ll u, ll p){
    vector<ll> time;
    dp[u] = 0;
    for(auto v : adj[u]){
        if(v != p && v != child[u]){
            dfs(v, u);
            time.push_back(dp[v]);
        }
    }
    sort(time.begin(), time.end(), greater<ll>());
    for(ll i = 0; i < time.size(); i++){
        dp[u] = max(dp[u], time[i] + i + 1); 
    }
}
ll res = 1e18;
bool check(ll mid){
    child[trace[mid]] = trace[mid - 1];
    child[trace[mid - 1]] = trace[mid];
    dfs(a, 0);
    dfs(b, 0);

    child[trace[mid]] = child[trace[mid - 1]] = 0;

    res = min(res, max(dp[a], dp[b]));
    return dp[a] < dp[b];
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    // freopen(task".inp" , "r" , stdin);  
    // freopen(task".out" , "w" , stdout);
    cin >> n >> a >> b;
    for(ll i = 1; i <= n; i++){
        ll x, y; cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    findpath(a, 0);
    ll l = 1, r = trace.size() - 1;
    while(l <= r){
        ll mid = (l + r) / 2;
        if(check(mid))l = mid + 1;
        else r = mid - 1;
    }
    cout << res << '\n';
}

Compilation message

torrent.cpp: In function 'void dfs(ll, ll)':
torrent.cpp:40:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for(ll i = 0; i < time.size(); i++){
      |                   ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 27224 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 283 ms 42292 KB Output is correct
2 Correct 288 ms 43720 KB Output is correct
3 Correct 287 ms 44744 KB Output is correct
4 Correct 295 ms 44240 KB Output is correct
5 Correct 281 ms 41556 KB Output is correct
6 Correct 272 ms 42632 KB Output is correct
7 Correct 263 ms 45248 KB Output is correct