답안 #506123

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
506123 2022-01-11T16:05:54 Z Jomnoi Torrent (COI16_torrent) C++17
100 / 100
343 ms 22272 KB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int N = 3e5 + 10;
const int INF = 1e9 + 7;

vector <int> arr;
vector <int> adj[N];

bool find(int u, int p, int x) {
    if(u == x) {
        arr.push_back(u);
        return 1;
    }

    for(auto v : adj[u]) {
        if(v == p) { 
            continue;
        }

        if(find(v, u, x) == true) {
            arr.emplace_back(u);
            return true;
        }
    }
    return false;
}

int dfs(int u, int p, int x) {
    vector <int> child;
    for(auto v : adj[u]) {
        if(v == p || v == x) {
            continue;
        }

        child.push_back(dfs(v, u, x));
    }

    sort(child.rbegin(), child.rend());
    int res = 0;
    for(int i = 1; i <= child.size(); i++) {
        res = max(res, child[i - 1] + i);
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n, a, b;
    cin >> n >> a >> b;
    for(int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }

    find(a, -1, b);

    if(DEBUG) {
        cout << "Debugging : \n";
        for(auto v : arr) {
            cout << v << " ";
        }
        cout << '\n';
    }

    int l = 0, r = arr.size() - 2;
    int ans = INF;
    while(l <= r) {
        int mid = (l + r) / 2;

        int L = dfs(b, -1, arr[mid + 1]);
        int R = dfs(a, -1, arr[mid]);
        ans = min(ans, max(L, R));
        if(L < R) {
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    cout << ans;
    return 0;
}

Compilation message

torrent.cpp: In function 'int dfs(int, int, int)':
torrent.cpp:42:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for(int i = 1; i <= child.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 5 ms 7364 KB Output is correct
3 Correct 5 ms 7372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 339 ms 19160 KB Output is correct
2 Correct 340 ms 20528 KB Output is correct
3 Correct 337 ms 21972 KB Output is correct
4 Correct 343 ms 21352 KB Output is correct
5 Correct 314 ms 18640 KB Output is correct
6 Correct 296 ms 19200 KB Output is correct
7 Correct 312 ms 22272 KB Output is correct