답안 #540342

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
540342 2022-03-20T05:46:39 Z PixelCat 수도 (JOI20_capital_city) C++14
11 / 100
1973 ms 524288 KB
/* code by the cute ~~Dengdualang~~ PixelCat owo */
/*     as cute as nacho neko (aka. my wife)!     */

/*     Nhade stay for a night here               */
/*     183234 deng deng deng pixelcat oops       */
/*     (yang wang yesu)*2                        */
/*     ^ some weird stuff. nvm =w=               */

#pragma GCC optimize("O4,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
// #define int LL  //__int128
#define double long double
using namespace std;
using LL = long long;
using LLL = __int128_t;
using uLL = unsigned long long;
using pii = pair<int, int>;

#define For(i, a, b) for (int i = a; i <= b; i++)
#define Fors(i, a, b, s) for (int i = a; i <= b; i += s)
#define Forr(i, a, b) for (int i = a; i >= b; i--)
#define F first
#define S second
#define L(id) (id * 2 + 1)
#define R(id) (id * 2 + 2)
#define LO(x) (x & (-x))

#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define mkp make_pair

#define MOD (int)(998244353)
// #define MOD (int)((LL)1e9 + 7)
// #define INF (int)(4e18)  // 9e18
#define INF 1e9
#define EPS (1e-6)

#ifdef NYAOWO
#include "library/debug.hpp"
inline void USACO(const string &s) {
    cerr << "USACO: " << s << "\n";
}

#else
#define debug(...)
inline void timer() {}
inline void USACO(const string &s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

#endif

inline void NYA() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
inline int gcd(int a, int b) {
    return __gcd(a, b);
}
inline int lcm(int a, int b) {
    return a / gcd(a, b) * b;
}
int fpow(int b, int p, const int &mod) {
    int ans = 1;
    while (p) {
        if (p & 1) ans = ans * b % mod;
        p >>= 1;
        b = b * b % mod;
    }
    return ans;
}
int fpow(int b, int p) {
    return fpow(b, p, MOD);
}
template <typename T>
inline void chmin(T &_a, const T &_b) {
    if (_b < _a) _a = _b;
}
template <typename T>
inline void chmax(T &_a, const T &_b) {
    if (_b > _a) _a = _b;
}
// mt19937_64 rng(
//     chrono::steady_clock::now().time_since_epoch().count());

const int MAXN=200020;
const int lg=19;

vector<int> tr[MAXN];
int par[MAXN][20];
int dep[MAXN];

int cid[MAXN]; // city number
int crt[MAXN]; // root for each city

struct Node{
    vector<Node*> adj;
    vector<Node*> rev;
    int vis;
    bool real;
    Node():adj(),vis(0),real(0){}
};
Node up[MAXN][20];
Node ct[MAXN];

inline void link(Node &a,Node &b){
    a.adj.eb(&b);
    b.rev.eb(&a);
    // cerr<<&a<<" "<<&b<<"\n";
}

void buildLCA(int n){
    auto dfs = [&](auto fdfs, int x, int p, int d) -> void{
        dep[x]=d;
        par[x][0]=p;
        link(up[x][0],ct[cid[x]]);
        link(ct[cid[x]],up[x][0]);
        for(auto &i:tr[x]) if(i!=p){
            fdfs(fdfs,i,x,d+1);
        }
    };
    dfs(dfs,1,1,1);
    For(j,1,lg) For(i,1,n){
        par[i][j]=par[par[i][j-1]][j-1];
        link(up[i][j],up[i][j-1]);
        link(up[i][j],up[par[i][j-1]][j-1]);
    }
}
int getLCA(int a,int b){
    if(dep[a]<dep[b]) swap(a,b);
    int dif=dep[a]-dep[b];
    Forr(j,lg,0) if(dif&(1<<j)){
        a=par[a][j];
    }
    Forr(j,lg,0) if(par[a][j]!=par[b][j]){
        a=par[a][j];
        b=par[b][j];
    }
    if(a!=b) a=par[a][0];
    return a;
}
void link(int a,int r){
    int dif=dep[a]-dep[r]+1;
    int k=a;
    Forr(j,lg,0) if(dif&(1<<j)){
        link(up[a][0],up[k][j]);
        k=par[k][j];
    }
}

int buildSCC(int n){
    vector<Node*> st;
    auto dfs1=[&](auto dfs,Node* nd)->void{
        nd->vis=-1;
        for(auto &i:nd->rev) if(i->vis==0){
            dfs(dfs,i);
        }
        st.eb(nd);
    };
    For(i,1,n) if(ct[i].vis==0) dfs1(dfs1,ct+i);
    // cerr<<st<<"\n";
    int sccid;
    int now;
    int ans=INF;
    auto dfs2=[&](auto dfs,Node* nd)->void{
        // cerr<<nd<<" "<<sccid<<"\n";
        nd->vis=sccid;
        now+=nd->real;
        for(auto &i:nd->adj){
            if(i->vis==-1) dfs(dfs,i);
            else if(i->vis!=sccid) now=INF;
        }
    };
    sccid=0;
    while(sz(st)){
        auto x=st.back(); st.pop_back();
        if(x->vis!=-1) continue;
        sccid++;
        now=0;
        dfs2(dfs2,x);
        chmin(ans,now);
    }
    return ans;
}

int32_t main() {
    NYA();
    // nyaacho >/////<
    // miku sama bless me >/////<
    int n,k; cin>>n>>k;
    For(i,1,n-1){
        int a,b; cin>>a>>b;
        tr[a].eb(b);
        tr[b].eb(a);
    }
    For(i,1,n) cin>>cid[i];
    buildLCA(n);
    For(i,1,n){
        int &c=cid[i];
        if(crt[c]==0) crt[c]=i;
        else crt[c]=getLCA(crt[c],i);
    }
    For(i,1,n) link(i,crt[cid[i]]);
    For(i,1,k) ct[i].real=1;

    // cerr<<"\n";
    // For(i,1,n) For(j,0,lg){
    //     cerr<<(par[i][j])<<" \n"[j==lg];
    // }
    // cerr<<"\n";
    // For(i,1,n) For(j,0,lg){
    //     cerr<<&(up[i][j])<<" \n"[j==lg];
    // }
    // cerr<<"\n";
    // For(i,1,k) cerr<<ct+i<<" \n"[i==k];
    // cerr<<"\n";

    cout<<buildSCC(k)-1<<"\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 235212 KB Output is correct
2 Correct 115 ms 235224 KB Output is correct
3 Correct 114 ms 235152 KB Output is correct
4 Correct 113 ms 235228 KB Output is correct
5 Correct 118 ms 235224 KB Output is correct
6 Correct 116 ms 235224 KB Output is correct
7 Correct 115 ms 235132 KB Output is correct
8 Correct 115 ms 235224 KB Output is correct
9 Correct 118 ms 235208 KB Output is correct
10 Correct 110 ms 235084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 235212 KB Output is correct
2 Correct 115 ms 235224 KB Output is correct
3 Correct 114 ms 235152 KB Output is correct
4 Correct 113 ms 235228 KB Output is correct
5 Correct 118 ms 235224 KB Output is correct
6 Correct 116 ms 235224 KB Output is correct
7 Correct 115 ms 235132 KB Output is correct
8 Correct 115 ms 235224 KB Output is correct
9 Correct 118 ms 235208 KB Output is correct
10 Correct 110 ms 235084 KB Output is correct
11 Correct 126 ms 238976 KB Output is correct
12 Correct 127 ms 239012 KB Output is correct
13 Correct 127 ms 239032 KB Output is correct
14 Correct 127 ms 239056 KB Output is correct
15 Correct 143 ms 238936 KB Output is correct
16 Correct 125 ms 239052 KB Output is correct
17 Correct 126 ms 239040 KB Output is correct
18 Correct 142 ms 239084 KB Output is correct
19 Correct 125 ms 239032 KB Output is correct
20 Correct 131 ms 239064 KB Output is correct
21 Correct 121 ms 239024 KB Output is correct
22 Correct 123 ms 239076 KB Output is correct
23 Correct 127 ms 239060 KB Output is correct
24 Correct 127 ms 239012 KB Output is correct
25 Correct 127 ms 239048 KB Output is correct
26 Correct 130 ms 239044 KB Output is correct
27 Correct 144 ms 238956 KB Output is correct
28 Correct 129 ms 239084 KB Output is correct
29 Correct 146 ms 239004 KB Output is correct
30 Correct 124 ms 239040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1973 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 235212 KB Output is correct
2 Correct 115 ms 235224 KB Output is correct
3 Correct 114 ms 235152 KB Output is correct
4 Correct 113 ms 235228 KB Output is correct
5 Correct 118 ms 235224 KB Output is correct
6 Correct 116 ms 235224 KB Output is correct
7 Correct 115 ms 235132 KB Output is correct
8 Correct 115 ms 235224 KB Output is correct
9 Correct 118 ms 235208 KB Output is correct
10 Correct 110 ms 235084 KB Output is correct
11 Correct 126 ms 238976 KB Output is correct
12 Correct 127 ms 239012 KB Output is correct
13 Correct 127 ms 239032 KB Output is correct
14 Correct 127 ms 239056 KB Output is correct
15 Correct 143 ms 238936 KB Output is correct
16 Correct 125 ms 239052 KB Output is correct
17 Correct 126 ms 239040 KB Output is correct
18 Correct 142 ms 239084 KB Output is correct
19 Correct 125 ms 239032 KB Output is correct
20 Correct 131 ms 239064 KB Output is correct
21 Correct 121 ms 239024 KB Output is correct
22 Correct 123 ms 239076 KB Output is correct
23 Correct 127 ms 239060 KB Output is correct
24 Correct 127 ms 239012 KB Output is correct
25 Correct 127 ms 239048 KB Output is correct
26 Correct 130 ms 239044 KB Output is correct
27 Correct 144 ms 238956 KB Output is correct
28 Correct 129 ms 239084 KB Output is correct
29 Correct 146 ms 239004 KB Output is correct
30 Correct 124 ms 239040 KB Output is correct
31 Runtime error 1973 ms 524288 KB Execution killed with signal 9
32 Halted 0 ms 0 KB -