Submission #1092166

# Submission time Handle Problem Language Result Execution time Memory
1092166 2024-09-23T11:41:09 Z vjudge1 Capital City (JOI20_capital_city) C++17
100 / 100
313 ms 41572 KB

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
#define all(x) x.begin(),x.end()
#define pb push_back
#define for1(i,x,n) for(int i=x;i<=n;i++)
#define for2(i,x,n) for(int i=x;i>=n;i--)
//#define int ll

typedef long long ll;
typedef pair<int,int> pii;

const ll maxn=2e5+13;
const ll mod=1e9+7;
const ll inf=1e18;

int n,k,c[maxn];
vector<int> adj[maxn],L[maxn];

int sz[maxn],par[maxn],cnt[maxn];
bool used[maxn],dd[maxn];

void dfs_sz(int u,int pre)
{
    sz[u]=1;
    cnt[c[u]]=0;
    for(int v: adj[u])
    {
        if (v==pre||used[v]) continue;
        dfs_sz(v,u);
        sz[u]+=sz[v];
    }
}
int Find_cen(int u,int pre,int ts)
{
    for(int v:adj[u])
    {
        if (v==pre || used[v]) continue;
        if (sz[v]*2>ts) return Find_cen(v,u,ts);
    }
    return u;
}
void dfs_on(int u,int pre)
{
    par[u]=pre;
    dd[c[u]]=0;
    cnt[c[u]]++;
    for(int v:adj[u])
    {
        if (v==pre||used[v]) continue;
        dfs_on(v,u);
    }
}

vector<int>Q;
int centroid(int u)
{
    dfs_sz(u,-1);
    u=Find_cen(u,-1,sz[u]);
    used[u]=1;
    dfs_on(u,-1);
    Q.clear();
    Q.pb(c[u]);
    dd[c[u]]=1;
    int res=0;
    while (!Q.empty())
    {
        int cu=Q.back();
        Q.pop_back();
        if (cnt[cu]!=L[cu].size())
        {
            res=n;
            break;
        }
        for(int v:L[cu])
        {
            if (par[v]==-1) continue;
            if (!dd[c[par[v]]])
            {
                Q.pb(c[par[v]]);
                dd[c[par[v]]]=1;
                res++;
            }
        }
    }
    Q.clear();
//    Q.clear();
//    dfs_off(u,-1);
    for(int v: adj[u]) if (!used[v]) res=min(res,centroid(v));
    return res;
}
void sol()
{
    cin >> n>>k;
    for1(i,1,n-1)
    {
        int u,v; cin >> u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for1(i,1,n)
    {
        cin >>c[i];
        L[c[i]].pb(i);
    }
    cout << centroid(1);
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("wall.inp","r",stdin);
//    freopen("wall.out","w",stdout);
    int t=1;//cin >> t;
    while (t--)
    {
//        cerr<<"avdsava "<< t<<'\n';
        sol();
    }
}
/*
1
3
3 2 3 1 1 2
4 1
6 1
3 5
5 1
2 4
*/

Compilation message

capital_city.cpp: In function 'int centroid(int)':
capital_city.cpp:75:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |         if (cnt[cu]!=L[cu].size())
      |             ~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 4 ms 9820 KB Output is correct
3 Correct 5 ms 9816 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 5 ms 9820 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9716 KB Output is correct
9 Correct 5 ms 9816 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 4 ms 9820 KB Output is correct
3 Correct 5 ms 9816 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 5 ms 9820 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9716 KB Output is correct
9 Correct 5 ms 9816 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 5 ms 9820 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 6 ms 9820 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 5 ms 9820 KB Output is correct
16 Correct 5 ms 9820 KB Output is correct
17 Correct 5 ms 10072 KB Output is correct
18 Correct 5 ms 10096 KB Output is correct
19 Correct 5 ms 10328 KB Output is correct
20 Correct 5 ms 10076 KB Output is correct
21 Correct 6 ms 10076 KB Output is correct
22 Correct 5 ms 10104 KB Output is correct
23 Correct 5 ms 10076 KB Output is correct
24 Correct 6 ms 9828 KB Output is correct
25 Correct 5 ms 10116 KB Output is correct
26 Correct 5 ms 10160 KB Output is correct
27 Correct 5 ms 9904 KB Output is correct
28 Correct 5 ms 10072 KB Output is correct
29 Correct 5 ms 9948 KB Output is correct
30 Correct 6 ms 10076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 247 ms 41072 KB Output is correct
2 Correct 124 ms 41552 KB Output is correct
3 Correct 258 ms 40528 KB Output is correct
4 Correct 117 ms 41552 KB Output is correct
5 Correct 242 ms 37712 KB Output is correct
6 Correct 119 ms 41552 KB Output is correct
7 Correct 243 ms 37972 KB Output is correct
8 Correct 124 ms 41060 KB Output is correct
9 Correct 280 ms 36164 KB Output is correct
10 Correct 283 ms 33616 KB Output is correct
11 Correct 300 ms 36592 KB Output is correct
12 Correct 286 ms 39116 KB Output is correct
13 Correct 289 ms 33200 KB Output is correct
14 Correct 282 ms 39424 KB Output is correct
15 Correct 279 ms 39008 KB Output is correct
16 Correct 274 ms 34132 KB Output is correct
17 Correct 287 ms 34644 KB Output is correct
18 Correct 313 ms 34900 KB Output is correct
19 Correct 280 ms 38024 KB Output is correct
20 Correct 282 ms 40340 KB Output is correct
21 Correct 4 ms 9816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 4 ms 9820 KB Output is correct
3 Correct 5 ms 9816 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 5 ms 9820 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9716 KB Output is correct
9 Correct 5 ms 9816 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 5 ms 9820 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 6 ms 9820 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 5 ms 9820 KB Output is correct
16 Correct 5 ms 9820 KB Output is correct
17 Correct 5 ms 10072 KB Output is correct
18 Correct 5 ms 10096 KB Output is correct
19 Correct 5 ms 10328 KB Output is correct
20 Correct 5 ms 10076 KB Output is correct
21 Correct 6 ms 10076 KB Output is correct
22 Correct 5 ms 10104 KB Output is correct
23 Correct 5 ms 10076 KB Output is correct
24 Correct 6 ms 9828 KB Output is correct
25 Correct 5 ms 10116 KB Output is correct
26 Correct 5 ms 10160 KB Output is correct
27 Correct 5 ms 9904 KB Output is correct
28 Correct 5 ms 10072 KB Output is correct
29 Correct 5 ms 9948 KB Output is correct
30 Correct 6 ms 10076 KB Output is correct
31 Correct 247 ms 41072 KB Output is correct
32 Correct 124 ms 41552 KB Output is correct
33 Correct 258 ms 40528 KB Output is correct
34 Correct 117 ms 41552 KB Output is correct
35 Correct 242 ms 37712 KB Output is correct
36 Correct 119 ms 41552 KB Output is correct
37 Correct 243 ms 37972 KB Output is correct
38 Correct 124 ms 41060 KB Output is correct
39 Correct 280 ms 36164 KB Output is correct
40 Correct 283 ms 33616 KB Output is correct
41 Correct 300 ms 36592 KB Output is correct
42 Correct 286 ms 39116 KB Output is correct
43 Correct 289 ms 33200 KB Output is correct
44 Correct 282 ms 39424 KB Output is correct
45 Correct 279 ms 39008 KB Output is correct
46 Correct 274 ms 34132 KB Output is correct
47 Correct 287 ms 34644 KB Output is correct
48 Correct 313 ms 34900 KB Output is correct
49 Correct 280 ms 38024 KB Output is correct
50 Correct 282 ms 40340 KB Output is correct
51 Correct 4 ms 9816 KB Output is correct
52 Correct 169 ms 23988 KB Output is correct
53 Correct 172 ms 24256 KB Output is correct
54 Correct 174 ms 23888 KB Output is correct
55 Correct 171 ms 23892 KB Output is correct
56 Correct 168 ms 24084 KB Output is correct
57 Correct 166 ms 23888 KB Output is correct
58 Correct 190 ms 27732 KB Output is correct
59 Correct 181 ms 28108 KB Output is correct
60 Correct 206 ms 27500 KB Output is correct
61 Correct 217 ms 27216 KB Output is correct
62 Correct 112 ms 41572 KB Output is correct
63 Correct 114 ms 41556 KB Output is correct
64 Correct 122 ms 41328 KB Output is correct
65 Correct 119 ms 41300 KB Output is correct
66 Correct 147 ms 32876 KB Output is correct
67 Correct 153 ms 32560 KB Output is correct
68 Correct 150 ms 32712 KB Output is correct
69 Correct 154 ms 32712 KB Output is correct
70 Correct 157 ms 32640 KB Output is correct
71 Correct 150 ms 32712 KB Output is correct
72 Correct 148 ms 32716 KB Output is correct
73 Correct 147 ms 31728 KB Output is correct
74 Correct 156 ms 32716 KB Output is correct
75 Correct 161 ms 32712 KB Output is correct
76 Correct 226 ms 31572 KB Output is correct
77 Correct 238 ms 29520 KB Output is correct
78 Correct 298 ms 34560 KB Output is correct
79 Correct 293 ms 32592 KB Output is correct
80 Correct 280 ms 39760 KB Output is correct
81 Correct 283 ms 36332 KB Output is correct
82 Correct 292 ms 36404 KB Output is correct
83 Correct 287 ms 33108 KB Output is correct
84 Correct 298 ms 39240 KB Output is correct
85 Correct 296 ms 37456 KB Output is correct
86 Correct 288 ms 32596 KB Output is correct
87 Correct 278 ms 34388 KB Output is correct
88 Correct 243 ms 35468 KB Output is correct
89 Correct 232 ms 31572 KB Output is correct
90 Correct 236 ms 31316 KB Output is correct
91 Correct 230 ms 33620 KB Output is correct
92 Correct 237 ms 32336 KB Output is correct
93 Correct 229 ms 32084 KB Output is correct
94 Correct 230 ms 31288 KB Output is correct
95 Correct 231 ms 32852 KB Output is correct
96 Correct 230 ms 31724 KB Output is correct
97 Correct 226 ms 33620 KB Output is correct