Submission #540341

# Submission time Handle Problem Language Result Execution time Memory
540341 2022-03-20T05:45:25 Z PixelCat Capital City (JOI20_capital_city) C++14
11 / 100
1897 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 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;
}
# Verdict Execution time Memory Grader output
1 Correct 129 ms 268052 KB Output is correct
2 Correct 121 ms 268104 KB Output is correct
3 Correct 126 ms 268100 KB Output is correct
4 Correct 125 ms 268104 KB Output is correct
5 Correct 135 ms 268108 KB Output is correct
6 Correct 136 ms 268112 KB Output is correct
7 Correct 125 ms 268104 KB Output is correct
8 Correct 121 ms 267980 KB Output is correct
9 Correct 125 ms 268000 KB Output is correct
10 Correct 132 ms 268064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 268052 KB Output is correct
2 Correct 121 ms 268104 KB Output is correct
3 Correct 126 ms 268100 KB Output is correct
4 Correct 125 ms 268104 KB Output is correct
5 Correct 135 ms 268108 KB Output is correct
6 Correct 136 ms 268112 KB Output is correct
7 Correct 125 ms 268104 KB Output is correct
8 Correct 121 ms 267980 KB Output is correct
9 Correct 125 ms 268000 KB Output is correct
10 Correct 132 ms 268064 KB Output is correct
11 Correct 135 ms 272076 KB Output is correct
12 Correct 138 ms 272120 KB Output is correct
13 Correct 142 ms 272044 KB Output is correct
14 Correct 143 ms 272152 KB Output is correct
15 Correct 136 ms 272016 KB Output is correct
16 Correct 133 ms 272100 KB Output is correct
17 Correct 153 ms 272136 KB Output is correct
18 Correct 135 ms 272064 KB Output is correct
19 Correct 138 ms 272176 KB Output is correct
20 Correct 148 ms 272112 KB Output is correct
21 Correct 153 ms 272144 KB Output is correct
22 Correct 138 ms 272156 KB Output is correct
23 Correct 149 ms 272120 KB Output is correct
24 Correct 137 ms 272060 KB Output is correct
25 Correct 135 ms 272108 KB Output is correct
26 Correct 135 ms 272108 KB Output is correct
27 Correct 156 ms 272052 KB Output is correct
28 Correct 142 ms 272036 KB Output is correct
29 Correct 138 ms 272072 KB Output is correct
30 Correct 143 ms 272016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1897 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 129 ms 268052 KB Output is correct
2 Correct 121 ms 268104 KB Output is correct
3 Correct 126 ms 268100 KB Output is correct
4 Correct 125 ms 268104 KB Output is correct
5 Correct 135 ms 268108 KB Output is correct
6 Correct 136 ms 268112 KB Output is correct
7 Correct 125 ms 268104 KB Output is correct
8 Correct 121 ms 267980 KB Output is correct
9 Correct 125 ms 268000 KB Output is correct
10 Correct 132 ms 268064 KB Output is correct
11 Correct 135 ms 272076 KB Output is correct
12 Correct 138 ms 272120 KB Output is correct
13 Correct 142 ms 272044 KB Output is correct
14 Correct 143 ms 272152 KB Output is correct
15 Correct 136 ms 272016 KB Output is correct
16 Correct 133 ms 272100 KB Output is correct
17 Correct 153 ms 272136 KB Output is correct
18 Correct 135 ms 272064 KB Output is correct
19 Correct 138 ms 272176 KB Output is correct
20 Correct 148 ms 272112 KB Output is correct
21 Correct 153 ms 272144 KB Output is correct
22 Correct 138 ms 272156 KB Output is correct
23 Correct 149 ms 272120 KB Output is correct
24 Correct 137 ms 272060 KB Output is correct
25 Correct 135 ms 272108 KB Output is correct
26 Correct 135 ms 272108 KB Output is correct
27 Correct 156 ms 272052 KB Output is correct
28 Correct 142 ms 272036 KB Output is correct
29 Correct 138 ms 272072 KB Output is correct
30 Correct 143 ms 272016 KB Output is correct
31 Runtime error 1897 ms 524288 KB Execution killed with signal 9
32 Halted 0 ms 0 KB -