Submission #1087389

# Submission time Handle Problem Language Result Execution time Memory
1087389 2024-09-12T15:20:39 Z smile Torrent (COI16_torrent) C++14
31 / 100
2000 ms 28756 KB
#include <bits/stdc++.h>

#define smile "TDL."
#define mp make_pair

using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
const int N = (int) 3e5 + 10;

int n, m;
vector<int> g[N];
int M[3];
int f[N];
int c[N];
ii banned = mp(0,0);

void dfs(int u, int pre)
{
    vector<int> tmp;
    f[u] = 0;
    c[u] = 0;
    for (int v: g[u])
    {
        if (v == pre || mp(min(u, v), max(u, v)) == banned) continue;
        dfs(v, u);
        c[u]++;
        tmp.push_back(f[v]);
    }
    sort(tmp.begin(), tmp.end(), greater<int>());
    for (int i = 0; i < tmp.size(); i++)
        f[u] = max(f[u], tmp[i]+i+1);
}

namespace SUB2
{
    void solve()
    {
        dfs(M[0], M[0]);
        cout << f[M[0]];
    }
}

namespace SUB4
{
    int trace[N];
    void bfs(int s, int t)
    {
        queue<int> q;
        q.push(s);
        trace[s] = -1;
        while (!q.empty())
        {
            int u = q.front(); q.pop();
            for (int v: g[u])
            {
                if (!trace[v])
                {
                    trace[v] = u;
                    q.push(v);
                    if (v == t) return;
                }
            }
        }
    }
    vector<ii> e;
    vector<int> tmp;
    int calc(int u, int v)
    {
        banned = mp(min(u, v), max(u,v));
        dfs(M[0], M[0]);
        dfs(M[1], M[1]);
        int t1 = f[M[0]], t2 = f[M[1]];
        if (t1 < t2)
            t1 += (c[M[0]] == t1);    
        else if (t2 < t1)
            t2 += (c[M[1]] == t2);
        else if (c[M[0]] == t1 && c[M[1]] == t2)
            t1++;
        return max(t1, t2);
    }
    void T()
    {
        int tl = calc(tmp[0], tmp[1]),
            tr = calc(tmp[tmp.size()-2], tmp[tmp.size()-1]);
        int l = 1, r = tmp.size()-2;  
        for (int i = 17; i >= 0; i--)
        {
            if (l + (1 << i) > r) continue;
            int j = l + (1 << i);
            int u = tmp[j], v = tmp[j-1];
            if (calc(u, v) == tl) l += (1 << i);
        }
        for (int i = 17; i >= 0; i--)
        {
            if (r - (1 << i) < l) continue;
            int j = r - (1 << i);
            int u = tmp[j], v = tmp[j+1];
            if (calc(u, v) == tr) r -= (1 << i);    
            // cout << l << ' ' << r << '\n';
        }
        // int ans = l;
        for (int i = 17; i >= 0; i--)
        {
            if (l + (1 << i) > r) continue;
            int j = l + (1 << i);
            int s = tmp[j-1], u = tmp[j], v = tmp[j+1];

            if (calc(s, u) > calc(u, v)) l += (1 << i);    
            // cout << l << ' ' << r << '\n';
        }
        for (int i = 17; i >= 0; i--)
        {
            if (l + (1 << i) > r) continue;
            int j = l + (1 << i);
            int s = tmp[j-1], u = tmp[j], v = tmp[j+1];

            if (calc(s, u) < calc(u, v)) r -= (1 << i);    
        }
        int ans =INT_MAX;
            // cout << l << ' ' << r << '\n';
        for (int i = l; i <= r; i++)
            ans = min(ans, calc(tmp[i], tmp[i+1]));
        // cerr <<  "ok\n";

        // while (l < r)
        // {
        //     cerr << l << ' ' << r << '\n';
        //     int m = (l + r)/2;
        //     int t1 = calc(tmp[m-1], tmp[m]),
        //         t2 = calc(tmp[m], tmp[m+1]);
        //     if (t1 < t2)
        //         r = m;
        //     else if (t1 > t2)
        //         l = m;
        //     else
        //     {
        //         ans = m;   
        //         break;
        //     }
        // }
        // if (ans) l = ans;
        cout << ans;
    }
    void solve()
    {
        int ans = INT_MAX;
        bfs(M[0], M[1]);
        int v = M[1];
        while (v != -1)
        {
            tmp.push_back(v);
            v = trace[v];
        }
        reverse(tmp.begin(), tmp.end());
        // for (int i = 1; i < tmp.size(); i++)
        //     e.push_back(mp(min(tmp[i-1], tmp[i]), max(tmp[i-1], tmp[i])));
        T();
    }
}

int main()
{
    //freopen(smile"inp", "r", stdin);
    //freopen(smile"out", "w", stdout);
    cin.tie(0) -> sync_with_stdio(0);   
    cin >> n >> M[0] >> M[1];
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);        
    }
    // cin >> m;   
    // for (int i = 0; i < m; i++)
    //     cin >> M[i];
    // if (m == 1)
    //     SUB2::solve();
    // else
        SUB4::solve();
    return 0;
}

Compilation message

torrent.cpp: In function 'void dfs(int, int)':
torrent.cpp:31:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |     for (int i = 0; i < tmp.size(); i++)
      |                     ~~^~~~~~~~~~~~
torrent.cpp: In function 'void SUB4::solve()':
torrent.cpp:147:13: warning: unused variable 'ans' [-Wunused-variable]
  147 |         int ans = INT_MAX;
      |             ^~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7516 KB Output is correct
2 Correct 8 ms 7516 KB Output is correct
3 Correct 10 ms 7600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2070 ms 28756 KB Time limit exceeded
2 Halted 0 ms 0 KB -