제출 #538023

#제출 시각아이디문제언어결과실행 시간메모리
538023wiwiho수도 (JOI20_capital_city)C++14
100 / 100
764 ms125796 KiB
#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

ll ifloor(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a < 0) return (a - b + 1) / b;
    else return a / b;
}

ll iceil(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a > 0) return (a + b - 1) / b;
    else return a / b;
}

int n, m;
vector<vector<int>> g, g2, r2;
vector<int> C;
vector<int> top, id, sz, dpt, pr;
vector<int> gv;
vector<vector<int>> cv;
vector<bool> vst;
int hldid = 1;
vector<int> hld;
vector<int> scc, cnt;
int sccid = 1;

void init(){
    cv.resize(m + 1);
    C.resize(n + 1);
    g.resize(n + 1);

    top.resize(n + 1);
    id.resize(n + 1);
    sz.resize(n + 1);
    dpt.resize(n + 1);
    pr.resize(n + 1);

    gv.resize(4 * n);
    g2.resize(1 + m + 4 * n);
    r2.resize(1 + m + 4 * n);
    vst.resize(1 + m + 4 * n);
    hld.resize(n + 1);

    scc.resize(1 + m + 4 * n);
    cnt.resize(1 + m + 4 * n);
}

void dfssz(int now, int p){
    sz[now] = 1;
    dpt[now] = dpt[p] + 1;
    pr[now] = p;
    for(int i : g[now]){
        if(i == p) continue;
        dfssz(i, now);
        sz[now] += sz[i];
    }
}

void dfstop(int now, int p, int t){
    if(t == -1) t = now;
    top[now] = t;
    int mx = -1;
    id[now] = hldid++;
    hld[id[now]] = now;
    for(int i : g[now]){
        if(i == p) continue;
        if(mx == -1 || sz[i] > sz[mx]) mx = i;
    }
    if(mx != -1) dfstop(mx, now, t);
    for(int i : g[now]){
        if(i == p || i == mx) continue;
        dfstop(i, now, -1);
    }
}

#define lc (2 * id + 1)
#define rc (2 * id + 2)
void buildsgt(int L = 1, int R = n, int id = 0){
    if(L == R){
        gv[id] = C[hld[L]];
        return;
    }
    gv[id] = m + 1 + id;
    int M = (L + R) / 2;
    buildsgt(L, M, lc);
    buildsgt(M + 1, R, rc);
    g2[gv[id]].eb(gv[lc]);
    g2[gv[id]].eb(gv[rc]);
}

void modify(int l, int r, int v, int L = 1, int R = n, int id = 0){
    if(l <= L && R <= r){
        g2[v].eb(gv[id]);
        return;
    }
    int M = (L + R) / 2;
    if(r <= M) modify(l, r, v, L, M, lc);
    else if(l > M) modify(l, r, v, M + 1, R, rc);
    else{
        modify(l, r, v, L, M, lc);
        modify(l, r, v, M + 1, R, rc);
    }
}

void solve(int a, int b, int c){
    //cerr << "solve2 " << a << " " << b << " " << c << "\n";
    while(top[a] != top[b]){
        if(dpt[top[a]] < dpt[top[b]]) swap(a, b);
        modify(id[top[a]], id[a], c);
        //cerr << top[a] << " " << a << "\n";
        a = pr[top[a]];
    }
    if(dpt[a] < dpt[b]) swap(a, b);
    modify(id[b], id[a], c);
    //cerr << b << " " << a << "\n";
}

void solve(int c){
    //cerr << "solve " << c << "\n";
    int lst = -1;
    for(int i : cv[c]){
        if(lst == -1){
            lst = i;
            continue;
        }
        solve(lst, i, c);
        lst = i;
    }
}

vector<int> tmp;
void dfs1(int now){
    vst[now] = true;
    for(int i : r2[now]){
        if(vst[i]) continue;
        dfs1(i);
    }
    tmp.eb(now);
}

void dfs2(int now, int id){
    scc[now] = id;
    for(int i : g2[now]){
        if(scc[i]) continue;
        dfs2(i, id);
    }
}

int main(){
    StarBurstStream

    cin >> n >> m;
    init();
    
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        g[u].eb(v);
        g[v].eb(u);
    }
    for(int i = 1; i <= n; i++) cin >> C[i], cv[C[i]].eb(i);

    dfssz(1, 1);
    dfstop(1, 1, -1);
    buildsgt();

    //cerr << "top "; printv(top, cerr);
    //cerr << "id "; printv(id, cerr);
    
    for(int i = 1; i <= m; i++) solve(i);

    for(int i = 1; i <= m + 4 * n; i++){
        for(int j : g2[i]) r2[j].eb(i);
    }
    
    for(int i = 1; i <= m; i++) if(!vst[i]) dfs1(i);
    reverse(iter(tmp));
    for(int i : tmp) if(!scc[i]) dfs2(i, sccid++);
    for(int i = 1; i <= m; i++){
        cnt[scc[i]]++;
    }
    //printv(scc, cerr);

    vector<bool> ok(1 + m + 4 * n, true);
    for(int i = 1; i <= m + 4 * n; i++){
        for(int j : g2[i]){
            if(scc[i] != scc[j]) ok[scc[i]] = false;
        }
    }
    int ans = m;
    for(int i = 1; i <= m; i++){
        if(!ok[scc[i]]) continue;
        ans = min(ans, cnt[scc[i]] - 1);
    }
    cout << ans << "\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...