Submission #1087376

# Submission time Handle Problem Language Result Execution time Memory
1087376 2024-09-12T14:54:22 Z smile Torrent (COI16_torrent) C++14
0 / 100
353 ms 28996 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 << j);
        }
        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 << j);    
        }
        int ans = 0;
        while (l < r)
        {
            // cerr << l << ' ' << r << '\n';
            int m = (l - r);
            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 = 0;   
                break;
            }
        }
        if (ans) l = ans;
        cout << calc(tmp[l-1], tmp[l]);
    }
    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:123:13: warning: unused variable 'ans' [-Wunused-variable]
  123 |         int ans = INT_MAX;
      |             ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 7516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 353 ms 28996 KB Output isn't correct
2 Halted 0 ms 0 KB -