Submission #943811

# Submission time Handle Problem Language Result Execution time Memory
943811 2024-03-12T01:20:40 Z guagua0407 Capital City (JOI20_capital_city) C++17
100 / 100
2835 ms 472516 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=17;
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 56 ms 181840 KB Output is correct
2 Correct 48 ms 182060 KB Output is correct
3 Correct 48 ms 182108 KB Output is correct
4 Correct 49 ms 182048 KB Output is correct
5 Correct 47 ms 181840 KB Output is correct
6 Correct 47 ms 182084 KB Output is correct
7 Correct 48 ms 181852 KB Output is correct
8 Correct 47 ms 181992 KB Output is correct
9 Correct 47 ms 181904 KB Output is correct
10 Correct 49 ms 181852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 181840 KB Output is correct
2 Correct 48 ms 182060 KB Output is correct
3 Correct 48 ms 182108 KB Output is correct
4 Correct 49 ms 182048 KB Output is correct
5 Correct 47 ms 181840 KB Output is correct
6 Correct 47 ms 182084 KB Output is correct
7 Correct 48 ms 181852 KB Output is correct
8 Correct 47 ms 181992 KB Output is correct
9 Correct 47 ms 181904 KB Output is correct
10 Correct 49 ms 181852 KB Output is correct
11 Correct 52 ms 184660 KB Output is correct
12 Correct 53 ms 184660 KB Output is correct
13 Correct 55 ms 184660 KB Output is correct
14 Correct 53 ms 184656 KB Output is correct
15 Correct 54 ms 184404 KB Output is correct
16 Correct 57 ms 184740 KB Output is correct
17 Correct 54 ms 184404 KB Output is correct
18 Correct 53 ms 184404 KB Output is correct
19 Correct 53 ms 184656 KB Output is correct
20 Correct 53 ms 184404 KB Output is correct
21 Correct 53 ms 184404 KB Output is correct
22 Correct 55 ms 184600 KB Output is correct
23 Correct 54 ms 184532 KB Output is correct
24 Correct 53 ms 184548 KB Output is correct
25 Correct 54 ms 184480 KB Output is correct
26 Correct 54 ms 184400 KB Output is correct
27 Correct 54 ms 184404 KB Output is correct
28 Correct 54 ms 184600 KB Output is correct
29 Correct 55 ms 184404 KB Output is correct
30 Correct 55 ms 184404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2346 ms 468280 KB Output is correct
2 Correct 901 ms 459800 KB Output is correct
3 Correct 2384 ms 464392 KB Output is correct
4 Correct 898 ms 459296 KB Output is correct
5 Correct 2835 ms 472516 KB Output is correct
6 Correct 954 ms 461436 KB Output is correct
7 Correct 2548 ms 464684 KB Output is correct
8 Correct 935 ms 459672 KB Output is correct
9 Correct 2044 ms 450652 KB Output is correct
10 Correct 2044 ms 451044 KB Output is correct
11 Correct 1990 ms 452716 KB Output is correct
12 Correct 2000 ms 453308 KB Output is correct
13 Correct 2054 ms 449728 KB Output is correct
14 Correct 2093 ms 452484 KB Output is correct
15 Correct 2086 ms 452620 KB Output is correct
16 Correct 1974 ms 450448 KB Output is correct
17 Correct 1982 ms 452400 KB Output is correct
18 Correct 1977 ms 451796 KB Output is correct
19 Correct 2111 ms 452552 KB Output is correct
20 Correct 2029 ms 453160 KB Output is correct
21 Correct 47 ms 181840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 181840 KB Output is correct
2 Correct 48 ms 182060 KB Output is correct
3 Correct 48 ms 182108 KB Output is correct
4 Correct 49 ms 182048 KB Output is correct
5 Correct 47 ms 181840 KB Output is correct
6 Correct 47 ms 182084 KB Output is correct
7 Correct 48 ms 181852 KB Output is correct
8 Correct 47 ms 181992 KB Output is correct
9 Correct 47 ms 181904 KB Output is correct
10 Correct 49 ms 181852 KB Output is correct
11 Correct 52 ms 184660 KB Output is correct
12 Correct 53 ms 184660 KB Output is correct
13 Correct 55 ms 184660 KB Output is correct
14 Correct 53 ms 184656 KB Output is correct
15 Correct 54 ms 184404 KB Output is correct
16 Correct 57 ms 184740 KB Output is correct
17 Correct 54 ms 184404 KB Output is correct
18 Correct 53 ms 184404 KB Output is correct
19 Correct 53 ms 184656 KB Output is correct
20 Correct 53 ms 184404 KB Output is correct
21 Correct 53 ms 184404 KB Output is correct
22 Correct 55 ms 184600 KB Output is correct
23 Correct 54 ms 184532 KB Output is correct
24 Correct 53 ms 184548 KB Output is correct
25 Correct 54 ms 184480 KB Output is correct
26 Correct 54 ms 184400 KB Output is correct
27 Correct 54 ms 184404 KB Output is correct
28 Correct 54 ms 184600 KB Output is correct
29 Correct 55 ms 184404 KB Output is correct
30 Correct 55 ms 184404 KB Output is correct
31 Correct 2346 ms 468280 KB Output is correct
32 Correct 901 ms 459800 KB Output is correct
33 Correct 2384 ms 464392 KB Output is correct
34 Correct 898 ms 459296 KB Output is correct
35 Correct 2835 ms 472516 KB Output is correct
36 Correct 954 ms 461436 KB Output is correct
37 Correct 2548 ms 464684 KB Output is correct
38 Correct 935 ms 459672 KB Output is correct
39 Correct 2044 ms 450652 KB Output is correct
40 Correct 2044 ms 451044 KB Output is correct
41 Correct 1990 ms 452716 KB Output is correct
42 Correct 2000 ms 453308 KB Output is correct
43 Correct 2054 ms 449728 KB Output is correct
44 Correct 2093 ms 452484 KB Output is correct
45 Correct 2086 ms 452620 KB Output is correct
46 Correct 1974 ms 450448 KB Output is correct
47 Correct 1982 ms 452400 KB Output is correct
48 Correct 1977 ms 451796 KB Output is correct
49 Correct 2111 ms 452552 KB Output is correct
50 Correct 2029 ms 453160 KB Output is correct
51 Correct 47 ms 181840 KB Output is correct
52 Correct 898 ms 467936 KB Output is correct
53 Correct 916 ms 466740 KB Output is correct
54 Correct 911 ms 467740 KB Output is correct
55 Correct 931 ms 467484 KB Output is correct
56 Correct 926 ms 467708 KB Output is correct
57 Correct 938 ms 467092 KB Output is correct
58 Correct 1706 ms 459532 KB Output is correct
59 Correct 1742 ms 460100 KB Output is correct
60 Correct 1921 ms 468976 KB Output is correct
61 Correct 1966 ms 466252 KB Output is correct
62 Correct 914 ms 462516 KB Output is correct
63 Correct 894 ms 461608 KB Output is correct
64 Correct 917 ms 460376 KB Output is correct
65 Correct 911 ms 462168 KB Output is correct
66 Correct 1683 ms 461716 KB Output is correct
67 Correct 1745 ms 458148 KB Output is correct
68 Correct 1636 ms 460076 KB Output is correct
69 Correct 1618 ms 460824 KB Output is correct
70 Correct 1628 ms 460188 KB Output is correct
71 Correct 1665 ms 462080 KB Output is correct
72 Correct 1593 ms 459868 KB Output is correct
73 Correct 1810 ms 457496 KB Output is correct
74 Correct 1611 ms 460276 KB Output is correct
75 Correct 1644 ms 459848 KB Output is correct
76 Correct 1114 ms 461088 KB Output is correct
77 Correct 1094 ms 459852 KB Output is correct
78 Correct 2003 ms 450088 KB Output is correct
79 Correct 1992 ms 449084 KB Output is correct
80 Correct 2001 ms 451780 KB Output is correct
81 Correct 1984 ms 448652 KB Output is correct
82 Correct 1978 ms 450520 KB Output is correct
83 Correct 2099 ms 448024 KB Output is correct
84 Correct 2013 ms 451068 KB Output is correct
85 Correct 2017 ms 451024 KB Output is correct
86 Correct 1982 ms 448192 KB Output is correct
87 Correct 1979 ms 448728 KB Output is correct
88 Correct 1929 ms 452268 KB Output is correct
89 Correct 1887 ms 449396 KB Output is correct
90 Correct 1824 ms 449888 KB Output is correct
91 Correct 1909 ms 450156 KB Output is correct
92 Correct 1899 ms 451192 KB Output is correct
93 Correct 1975 ms 450868 KB Output is correct
94 Correct 1943 ms 449192 KB Output is correct
95 Correct 1893 ms 450948 KB Output is correct
96 Correct 2054 ms 450696 KB Output is correct
97 Correct 2051 ms 451524 KB Output is correct