답안 #844861

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
844861 2023-09-06T06:00:38 Z Cookie Torrent (COI16_torrent) C++14
31 / 100
7 ms 860 KB
#include<bits/stdc++.h>
#define ll long long
#define vt vector
#define pb push_back
#define pii pair<int, int>
#define sz(v) (int)v.size()
using namespace std;
const ll base = 107, mod = 1e9 + 7;
const int mxn = 2005, inf = 1e9, sq = 400;
int a, b, n;
vt<pii>adj[mxn + 1];
int pa[mxn + 1], toedge[mxn + 1];
int tin[mxn + 1], tout[mxn + 1], colour[mxn + 1], tt = 0;
int dp[mxn + 1];
bool vis = 0;
bool inside(int u, int v){
    return(tin[u] <= tin[v] && tout[v] <= tout[u]);
}
void dfs(int s, int pre){
    tin[s] = ++tt; 
    pa[s] = pre;
    for(auto [i, id]: adj[s]){
        if(i != pre){
            toedge[i] = id;
            dfs(i, s);
        }
    }
    tout[s] = tt;
}
int dfs_dp(int s, int pre, int co){
    dp[s] = 0;
    vt<int>comp;
    for(auto [i, id]: adj[s]){
        if(i != pre && colour[id] == co){
            comp.pb(dfs_dp(i, s, co));
        }
    }
    sort(comp.rbegin(), comp.rend());
    for(int i = 0; i < sz(comp); i++){
        dp[s] = max(dp[s], comp[i] + i + 1);
    }
    return(dp[s]);
}
int main(){
    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;
        adj[u].pb({v, i}); adj[v].pb({u, i});
    }
    dfs(a, -1);
    vt<int>comp;
    int ver = b;
    while(ver != a){
        comp.pb(ver); ver = pa[ver];
    }
    int ans = inf;
    for(int i = 0; i < sz(comp); i++){
        int root = comp[i];
        //cout << root << " ";
        for(int j = 1; j <= n; j++){
            if(j != root && inside(root, j)){
                
                colour[toedge[j]] = 2;
            }else{
                colour[toedge[j]] = 1;
            }
        }
        colour[toedge[root]] = 3;
        
        int cand = max(dfs_dp(a, -1, 1), dfs_dp(b, -1, 2));
        //for(int j = 1; j <= n; j++)cout << dp[j] << " ";
        ans = min(ans, cand);
      
       
    }
    cout << ans;
}

Compilation message

torrent.cpp: In function 'void dfs(int, int)':
torrent.cpp:22:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   22 |     for(auto [i, id]: adj[s]){
      |              ^
torrent.cpp: In function 'int dfs_dp(int, int, int)':
torrent.cpp:33:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   33 |     for(auto [i, id]: adj[s]){
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 600 KB Output is correct
2 Correct 3 ms 604 KB Output is correct
3 Correct 7 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 860 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -