답안 #253241

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253241 2020-07-27T14:16:20 Z egekabas 수도 (JOI20_capital_city) C++14
컴파일 오류
0 ms 0 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
ll n, k;
vector<ll> g[200009];
ll col[200009];
ll depth[200009];
ll dad[200009][30];
void dfs1(ll v, ll prt){
    for(ll i = 1; i < 30; ++i)
        dad[v][i] = dad[dad[v][i-1]][i-1];
    for(auto u : g[v])
        if(u != prt){
            depth[u] = depth[v]+1;
            dad[u][0] = v;
            dfs1(u, v);
        }
}
ll lca(ll x, ll y){
    if(depth[x] < depth[y])
        swap(x, y);
    for(ll i = 29; i >= 0; --i)
        if(depth[dad[x][i]] >= depth[y])
            x = dad[x][i];
    if(x == y) return x;
    for(ll i = 29; i >= 0; --i)
        if(dad[x][i] != dad[y][i]){
            x = dad[x][i];
            y = dad[y][i];
        }
    return dad[x][0];
}
ll root[200009];
vector<ll> con[200009];
vector<ll> inc[200009];

void dfs2(ll v, ll prt){
    for(auto u : g[v])
        if(u != prt){
            dfs2(u, v);
            if(col[u] != col[v] && root[col[u]] != depth[u]){
                root[col[u]] = min(root[col[u]], root[col[v]]);
                con[col[u]].pb(col[v]);
            }
        }
}
ll vis[200009];
vector<ll> order;
void ssc1(ll v){
    vis[v] = 1;
    for(auto u : con[v])
        if(vis[u] == 0)
            ssc1(u);
    order.pb(v);
}
ll gather(ll v){
    ll ret = 1;
    vis[v] = 1;
    for(auto u : inc[v])
        if(vis[u] == 0){
            ret += gather(u);
            ret = min(ret, ll(1e9));
        }
    for(auto u : con[v])
        if(vis[u] == 0)
            ret = 1e9;
    return ret;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    cin >> n >> k;
    for(ll i = 0; i < n-1; ++i){
        ll x, y;
        cin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
    }
    for(ll i = 1; i <= n; ++i)
        cin >> col[i];
    
    depth[1] = 1;
    dfs1(1, 0);
    for(ll i = 1; i <= n; ++i){
        if(root[col[i]] == 0)
            root[col[i]] = i;
        else
            root[col[i]] = lca(root[col[i]], i);
    }
    for(ll i = 1; i <= k; ++i)
        root[i] = depth[root[i]];
    dfs2(1, 0);
    dfs2(1, 0);

    for(ll i = 1; i <= k; ++i){
        sort(con[i].begin(), con[i].end());
        con[i].resize(unique(con[i].begin(), con[i].end())-con[i].begin());
        for(auto u : con[i])
            inc[u].pb(i);
    }
    for(ll i = 1; i <= k; ++i)
        if(vis[i] == 0)
            ssc1(i);
    for(ll i = 1; i <= k; ++i) vis[i] = 0;
    ll ans = k-1;
    reverse(order.begin(), order.end());
    for(auto u : order){
        if(vis[u] == 0)
            ans = min(ans, gather(u)-1);
    }
    assert(ans >= 0)
    cout << ans << '\n';
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:126:5: error: expected ';' before 'cout'
     cout << ans << '\n';
     ^~~~