답안 #846667

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
846667 2023-09-08T08:43:00 Z vjudge1 Torrent (COI16_torrent) C++17
69 / 100
377 ms 27036 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long

#define task "Torrent"

using namespace std;

const int N = 3e5 + 2;

int n, a, b, x, y, dp1[N], dp2[N];
vector<int> vt[N], trace;

void find(int u, int v)
{
    for (auto i : vt[u])
    {
        if (i == v)
            continue;
        if (i == b)
        {
            trace.push_back(i);
            trace.push_back(u);
            return;
        }
        find(i, u);
        if (trace.size() != 0 && trace.back() == i)
        {
            trace.push_back(u);
            return;
        }
    }
}

void dfs1(int u, int v)
{
    if ((u == x && v == y) || (v == x && a == y))
        return;
    vector<int> tmp;
    for (auto i : vt[u])
    {
        if ((u == x && i == y) || (i == x && a == y))
            return;
        if (i == v)
            continue;
        dfs1(i, u);
        tmp.push_back(dp1[i]);
    }
    sort(tmp.begin(), tmp.end(), greater<int>());
    dp1[u] = 0;
    for (int k = 0; k < tmp.size(); k++)
        dp1[u] = max(dp1[u], k + 1 + tmp[k]);
    return;
}

void dfs2(int u, int v)
{
    if ((u == x && v == y) || (v == x && a == y))
        return;
    vector<int> tmp;
    for (auto i : vt[u])
    {
        if ((u == x && i == y) || (i == x && a == y))
            return;
        if (i == v)
            continue;
        dfs2(i, u);
        tmp.push_back(dp2[i]);
    }
    sort(tmp.begin(), tmp.end(), greater<int>());
    dp2[u] = 0;
    for (int k = 0; k < tmp.size(); k++)
        dp2[u] = max(dp2[u], k + 1 + tmp[k]);
    return;
}

void check(int tmp1, int tmp2)
{
    x = tmp1;
    y = tmp2;
    dfs1(a, 0);
    dfs2(b, 0);
}

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

    cin >> n >> a >> b;
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        vt[u].push_back(v);
        vt[v].push_back(u);
    }
    find(a, 0);
    // trace.pop_back();
    reverse(trace.begin(), trace.end());
    int low = 1, high = trace.size() - 1, mid, ans = 1e9;
    while (low <= high)
    {
        mid = (low + high) / 2;
        check(trace[mid - 1], trace[mid]);
        if (dp1[a] > dp2[b])
        {
            ans = min(ans, max(dp1[a], dp2[b]));
            low = mid + 1;
        }
        else
        {
            ans = min(ans, max(dp1[a], dp2[b]));
            high = mid - 1;
        }
    }
    cout << ans;
    return 0;
}

Compilation message

torrent.cpp: In function 'void dfs1(int, int)':
torrent.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for (int k = 0; k < tmp.size(); k++)
      |                     ~~^~~~~~~~~~~~
torrent.cpp: In function 'void dfs2(int, int)':
torrent.cpp:73:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     for (int k = 0; k < tmp.size(); k++)
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 8796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 284 ms 21924 KB Output is correct
2 Correct 377 ms 23572 KB Output is correct
3 Correct 291 ms 26028 KB Output is correct
4 Correct 269 ms 24272 KB Output is correct
5 Correct 286 ms 21332 KB Output is correct
6 Correct 299 ms 22368 KB Output is correct
7 Correct 230 ms 27036 KB Output is correct