답안 #1087245

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1087245 2024-09-12T11:17:37 Z smile Torrent (COI16_torrent) C++14
0 / 100
647 ms 524288 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;
                }
            }
        }
    }
    int calc()
    {
        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 solve()
    {
        int ans = INT_MAX;
        bfs(M[0], M[1]);
        vector<int> tmp;
        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++)
        {
            banned = mp(min(tmp[i-1], tmp[i]), max(tmp[i-1], tmp[i]));
            ans = min(ans, calc());
        }
        cout << ans;
    }
}

int main()
{
//    freopen(smile"inp", "r", stdin);
//    freopen(smile"out", "w", stdout);
    cin.tie(0) -> sync_with_stdio(0);   
    cin >> n;
    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:91:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         for (int i = 1; i < tmp.size(); i++)
      |                         ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 581 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 647 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -