Submission #846677

# Submission time Handle Problem Language Result Execution time Memory
846677 2023-09-08T08:52:13 Z vjudge1 Torrent (COI16_torrent) C++17
69 / 100
344 ms 27208 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());
 if (n <= 1000)
     {
         int ans = 1e9;
         for (int mid = 1; mid < trace.size(); mid++)
         {
             check(trace[mid - 1], trace[mid]);
             ans = min(ans, max(dp1[a], dp2[b]));
         }
         cout << ans;
         return 0;
     }
    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++)
      |                     ~~^~~~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:107:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |          for (int mid = 1; mid < trace.size(); mid++)
      |                            ~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 8792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 263 ms 21740 KB Output is correct
2 Correct 330 ms 23600 KB Output is correct
3 Correct 282 ms 26008 KB Output is correct
4 Correct 296 ms 24284 KB Output is correct
5 Correct 344 ms 21736 KB Output is correct
6 Correct 283 ms 22168 KB Output is correct
7 Correct 241 ms 27208 KB Output is correct