Submission #1001927

# Submission time Handle Problem Language Result Execution time Memory
1001927 2024-06-19T08:35:53 Z Unforgettablepl Capital City (JOI20_capital_city) C++17
41 / 100
743 ms 52212 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

void strat1(int n){
    int k;
    cin >> k;
    vector<vector<int>> adj(n+1);
    for(int i=1;i<n;i++){
        int a,b;cin>>a>>b;
        adj[a].emplace_back(b);
        adj[b].emplace_back(a);
    }
    vector<int> col(n+1);
    for(int i=1;i<=n;i++)cin>>col[i];
    vector<int> nodes(k+1);
    function<void(int,int,int)> dfs = [&](int x,int par,int dep){
        nodes[col[x]] = x;
        for(int&i:adj[x])if(i!=par)dfs(i,x,dep+1);
    };
    dfs(1,0,1);
    vector<vector<int>> compoadj(k+1);
    for(int i=1;i<=k;i++){
        set<int> bad;
        function<bool(int,int)> pdfs = [&](int x,int p){
            bool found = col[x]==i;
            for(int&i:adj[x])if(i!=p)if(pdfs(i,x))found=true;
            if(found)bad.insert(col[x]);
            return found;
        };
        pdfs(nodes[i],0);
        for(int x:bad)compoadj[i].emplace_back(x);
    }
    vector<bool> visited(k+1);
    int siz = 0;
    function<void(int)> compodfs = [&](int x){
        if(visited[x])return;
        siz++;
        visited[x]=true;
        for(int&i:compoadj[x])compodfs(i);
    };
    int ans = n;
    for(int i=1;i<=k;i++){
        siz = 0;
        visited = vector<bool>(k+1);
        compodfs(i);
        ans = min(ans,siz-1);
    }
    cout << ans << '\n';
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    if(n<=2000){
        strat1(n);
        return 0;
    }
    int k;
    cin >> k;
    vector<vector<int>> adj(n+1);
    for(int i=1;i<n;i++){
        int a,b;cin>>a>>b;
        adj[a].emplace_back(b);
        adj[b].emplace_back(a);
    }
    vector<int> colcor(n+1);
    vector<int> col(n+1);
    for(int i=1;i<=n;i++)cin>>colcor[i];
    function<void(int,int,int)> dfs = [&](int x,int par,int idx){
        col[idx] = colcor[x];
        for(int&i:adj[x])if(i!=par)dfs(i,x,idx+1);
    };
    for(int i=1;i<=n;i++)if(adj[i].size()==1){
        dfs(i,0,1);
        break;
    }
    vector<int> minidx(k+1);
    vector<int> maxidx(k+1);
    for(int i=1;i<=n;i++)maxidx[col[i]]=i;
    for(int i=n;i;i--)minidx[col[i]]=i;
    vector<vector<int>> compoadj(k+1);
    vector<vector<int>> comporevadj(k+1);
    for(int i=1;i<=k;i++){
        for(int x=minidx[i];x<=maxidx[i];x++){
            if(col[x]!=i){
                compoadj[i].emplace_back(col[x]);
                comporevadj[col[x]].emplace_back(i);
                if(x==minidx[col[x]])x = maxidx[col[x]];
            }
        }
    }
    vector<bool> visited(k+1);
    vector<int> order;
    vector<int> siz(k+1);
    vector<int> mycompo(k+1);
    function<void(int)> topo = [&](int x){
        if(visited[x])return;
        visited[x]=true;
        for(int&i:compoadj[x])topo(i);
        order.emplace_back(x);
    };
    for(int i=1;i<=k;i++)if(!visited[i])topo(i);
    reverse(order.begin(),order.end());
    function<void(int,int)> cr_dfs = [&](int x,int compo){
        if(mycompo[x]){
            if(mycompo[x]!=compo)siz[mycompo[x]]=n+1;
            return;
        }
        siz[compo]++;
        mycompo[x] = compo;
        for(int&i:comporevadj[x])cr_dfs(i,compo);
    };
    int components = 0;
    int ans = n+1;
    for(int i=0;i<k;i++)if(mycompo[order[i]]==0)cr_dfs(order[i],++components);
    for(int i=1;i<=components;i++){
        ans = min(ans,siz[i]-1);
    }
    cout << ans << '\n';
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 14 ms 604 KB Output is correct
12 Correct 14 ms 608 KB Output is correct
13 Correct 18 ms 700 KB Output is correct
14 Correct 14 ms 604 KB Output is correct
15 Correct 25 ms 864 KB Output is correct
16 Correct 33 ms 1496 KB Output is correct
17 Correct 12 ms 604 KB Output is correct
18 Correct 15 ms 604 KB Output is correct
19 Correct 12 ms 604 KB Output is correct
20 Correct 12 ms 600 KB Output is correct
21 Correct 19 ms 604 KB Output is correct
22 Correct 743 ms 6156 KB Output is correct
23 Correct 658 ms 5924 KB Output is correct
24 Correct 19 ms 604 KB Output is correct
25 Correct 23 ms 724 KB Output is correct
26 Correct 15 ms 872 KB Output is correct
27 Correct 21 ms 728 KB Output is correct
28 Correct 15 ms 860 KB Output is correct
29 Correct 15 ms 856 KB Output is correct
30 Correct 14 ms 880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 52080 KB Output is correct
2 Correct 74 ms 52212 KB Output is correct
3 Correct 112 ms 52172 KB Output is correct
4 Correct 68 ms 52168 KB Output is correct
5 Correct 102 ms 51156 KB Output is correct
6 Correct 68 ms 51924 KB Output is correct
7 Correct 121 ms 50344 KB Output is correct
8 Correct 68 ms 49524 KB Output is correct
9 Correct 99 ms 47560 KB Output is correct
10 Correct 99 ms 47816 KB Output is correct
11 Correct 118 ms 50120 KB Output is correct
12 Correct 105 ms 47820 KB Output is correct
13 Correct 95 ms 47048 KB Output is correct
14 Correct 121 ms 46328 KB Output is correct
15 Correct 116 ms 46536 KB Output is correct
16 Correct 93 ms 46840 KB Output is correct
17 Correct 98 ms 46864 KB Output is correct
18 Correct 135 ms 47820 KB Output is correct
19 Correct 97 ms 46540 KB Output is correct
20 Correct 91 ms 46148 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 14 ms 604 KB Output is correct
12 Correct 14 ms 608 KB Output is correct
13 Correct 18 ms 700 KB Output is correct
14 Correct 14 ms 604 KB Output is correct
15 Correct 25 ms 864 KB Output is correct
16 Correct 33 ms 1496 KB Output is correct
17 Correct 12 ms 604 KB Output is correct
18 Correct 15 ms 604 KB Output is correct
19 Correct 12 ms 604 KB Output is correct
20 Correct 12 ms 600 KB Output is correct
21 Correct 19 ms 604 KB Output is correct
22 Correct 743 ms 6156 KB Output is correct
23 Correct 658 ms 5924 KB Output is correct
24 Correct 19 ms 604 KB Output is correct
25 Correct 23 ms 724 KB Output is correct
26 Correct 15 ms 872 KB Output is correct
27 Correct 21 ms 728 KB Output is correct
28 Correct 15 ms 860 KB Output is correct
29 Correct 15 ms 856 KB Output is correct
30 Correct 14 ms 880 KB Output is correct
31 Correct 111 ms 52080 KB Output is correct
32 Correct 74 ms 52212 KB Output is correct
33 Correct 112 ms 52172 KB Output is correct
34 Correct 68 ms 52168 KB Output is correct
35 Correct 102 ms 51156 KB Output is correct
36 Correct 68 ms 51924 KB Output is correct
37 Correct 121 ms 50344 KB Output is correct
38 Correct 68 ms 49524 KB Output is correct
39 Correct 99 ms 47560 KB Output is correct
40 Correct 99 ms 47816 KB Output is correct
41 Correct 118 ms 50120 KB Output is correct
42 Correct 105 ms 47820 KB Output is correct
43 Correct 95 ms 47048 KB Output is correct
44 Correct 121 ms 46328 KB Output is correct
45 Correct 116 ms 46536 KB Output is correct
46 Correct 93 ms 46840 KB Output is correct
47 Correct 98 ms 46864 KB Output is correct
48 Correct 135 ms 47820 KB Output is correct
49 Correct 97 ms 46540 KB Output is correct
50 Correct 91 ms 46148 KB Output is correct
51 Correct 0 ms 344 KB Output is correct
52 Incorrect 77 ms 21636 KB Output isn't correct
53 Halted 0 ms 0 KB -