Submission #943806

# Submission time Handle Problem Language Result Execution time Memory
943806 2024-03-12T01:16:00 Z guagua0407 Capital City (JOI20_capital_city) C++17
11 / 100
2100 ms 524288 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=2e5+5;
const int inf=1e9;
const int B=20;
vector<int> adj[mxn];
vector<int> adj2[mxn*B],adjr[mxn*B];
bool visited[mxn*B];
int c[mxn];
int up[mxn][B];
int id[mxn][B];
int depth[mxn];
vector<int> belong[mxn];
vector<int> ord;
int L[mxn];
int sz[mxn*B];
int scc[mxn*B];
int out[mxn*B];
int n,k;

void dfs(int v,int p=0){
    up[v][0]=p;
    depth[v]=depth[p]+1;
    for(auto u:adj[v]){
        if(u==p) continue;
        dfs(u,v);
    }
}

void dfs2(int v){
    visited[v]=true;
    for(auto u:adj2[v]){
        if(visited[u]) continue;
        dfs2(u);
    }
    ord.push_back(v);
}

void dfs3(int v,int cur){
    visited[v]=true;
    scc[v]=cur;
    if(v<=k) sz[cur]++;
    for(auto u:adjr[v]){
        if(visited[u]) continue;
        dfs3(u,cur);
    }
}

int lca(int a,int b){
    if(depth[a]<depth[b]) swap(a,b);
    int len=depth[a]-depth[b];
    for(int i=0;i<B;i++){
        if(len&(1<<i)) a=up[a][i];
    }
    if(a==b) return a;
    for(int i=B-1;i>=0;i--){
        int ta=up[a][i];
        int tb=up[b][i];
        if(ta!=tb){
            a=ta;
            b=tb;
        }
    }
    return up[a][0];
}

int main() {_
    cin>>n>>k;
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for(int i=1;i<=n;i++){
        cin>>c[i];
        belong[c[i]].push_back(i);
    }
    dfs(1);
    for(int j=1;j<B;j++){
        for(int i=1;i<=n;i++){
            up[i][j]=up[up[i][j-1]][j-1];
        }
    }
    for(int i=1;i<=k;i++){
        L[i]=belong[i][0];
        for(int j=1;j<(int)belong[i].size();j++){
            L[i]=lca(L[i],belong[i][j]);
        }
    }
    int cnt=k;
    for(int i=1;i<=n;i++){
        id[i][0]=c[i];
    }
    for(int j=1;j<B;j++){
        for(int i=1;i<=n;i++){
            id[i][j]=++cnt;
            adj2[id[i][j]].push_back(id[i][j-1]);
            adj2[id[i][j]].push_back(id[up[i][j-1]][j-1]);
        }
    }
    for(int i=1;i<=k;i++){
        //cout<<L[i]<<'\n';
        for(auto v:belong[i]){
            int bit=__lg(depth[v]-depth[L[i]]+1);
            adj2[i].push_back(id[v][bit]);
            int len=depth[v]-(depth[L[i]]+(1<<bit)-1);
            int x=v;
            for(int j=0;j<B;j++){
                if(len&(1<<j)) x=up[x][j];
            }
            adj2[i].push_back(id[x][bit]);
            //cout<<v<<' '<<x<<' '<<bit<<'\n';

        }
    }
    for(int i=1;i<=cnt;i++){
        for(auto u:adj2[i]){
            adjr[u].push_back(i);
        }
    }
    fill(visited+1,visited+cnt+1,false);
    for(int i=1;i<=cnt;i++){
        if(!visited[i]){
            dfs2(i);
        }
    }
    reverse(all(ord));
    fill(visited+1,visited+cnt+1,false);
    int sccnt=0;
    for(auto i:ord){
        if(!visited[i]){
            ++sccnt;
            dfs3(i,sccnt);
        }
    }
    for(int i=1;i<=cnt;i++){
        for(auto u:adj2[i]){
            if(scc[u]!=scc[i]) out[scc[i]]++;
        }
    }
    int ans=inf;
    for(int i=1;i<=sccnt;i++){
        if(out[i]==0 and sz[i]>0) ans=min(ans,sz[i]-1);
    }
    cout<<ans<<'\n';
    return 0;
}
//maybe its multiset not set
//yeeorz
//laborz

Compilation message

capital_city.cpp: In function 'void setIO(std::string)':
capital_city.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 90 ms 197720 KB Output is correct
2 Correct 86 ms 197716 KB Output is correct
3 Correct 76 ms 197716 KB Output is correct
4 Correct 79 ms 197736 KB Output is correct
5 Correct 84 ms 197760 KB Output is correct
6 Correct 81 ms 197800 KB Output is correct
7 Correct 79 ms 197588 KB Output is correct
8 Correct 77 ms 197660 KB Output is correct
9 Correct 85 ms 197716 KB Output is correct
10 Correct 74 ms 197712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 197720 KB Output is correct
2 Correct 86 ms 197716 KB Output is correct
3 Correct 76 ms 197716 KB Output is correct
4 Correct 79 ms 197736 KB Output is correct
5 Correct 84 ms 197760 KB Output is correct
6 Correct 81 ms 197800 KB Output is correct
7 Correct 79 ms 197588 KB Output is correct
8 Correct 77 ms 197660 KB Output is correct
9 Correct 85 ms 197716 KB Output is correct
10 Correct 74 ms 197712 KB Output is correct
11 Correct 87 ms 201264 KB Output is correct
12 Correct 93 ms 201136 KB Output is correct
13 Correct 82 ms 201116 KB Output is correct
14 Correct 84 ms 201208 KB Output is correct
15 Correct 88 ms 201328 KB Output is correct
16 Correct 86 ms 201212 KB Output is correct
17 Correct 85 ms 201144 KB Output is correct
18 Correct 94 ms 201228 KB Output is correct
19 Correct 93 ms 201180 KB Output is correct
20 Correct 83 ms 201324 KB Output is correct
21 Correct 87 ms 201240 KB Output is correct
22 Correct 89 ms 201344 KB Output is correct
23 Correct 92 ms 202288 KB Output is correct
24 Correct 87 ms 201272 KB Output is correct
25 Correct 83 ms 201088 KB Output is correct
26 Correct 90 ms 201148 KB Output is correct
27 Correct 92 ms 201292 KB Output is correct
28 Correct 87 ms 201140 KB Output is correct
29 Correct 85 ms 201168 KB Output is correct
30 Correct 83 ms 201096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2100 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 90 ms 197720 KB Output is correct
2 Correct 86 ms 197716 KB Output is correct
3 Correct 76 ms 197716 KB Output is correct
4 Correct 79 ms 197736 KB Output is correct
5 Correct 84 ms 197760 KB Output is correct
6 Correct 81 ms 197800 KB Output is correct
7 Correct 79 ms 197588 KB Output is correct
8 Correct 77 ms 197660 KB Output is correct
9 Correct 85 ms 197716 KB Output is correct
10 Correct 74 ms 197712 KB Output is correct
11 Correct 87 ms 201264 KB Output is correct
12 Correct 93 ms 201136 KB Output is correct
13 Correct 82 ms 201116 KB Output is correct
14 Correct 84 ms 201208 KB Output is correct
15 Correct 88 ms 201328 KB Output is correct
16 Correct 86 ms 201212 KB Output is correct
17 Correct 85 ms 201144 KB Output is correct
18 Correct 94 ms 201228 KB Output is correct
19 Correct 93 ms 201180 KB Output is correct
20 Correct 83 ms 201324 KB Output is correct
21 Correct 87 ms 201240 KB Output is correct
22 Correct 89 ms 201344 KB Output is correct
23 Correct 92 ms 202288 KB Output is correct
24 Correct 87 ms 201272 KB Output is correct
25 Correct 83 ms 201088 KB Output is correct
26 Correct 90 ms 201148 KB Output is correct
27 Correct 92 ms 201292 KB Output is correct
28 Correct 87 ms 201140 KB Output is correct
29 Correct 85 ms 201168 KB Output is correct
30 Correct 83 ms 201096 KB Output is correct
31 Runtime error 2100 ms 524288 KB Execution killed with signal 9
32 Halted 0 ms 0 KB -