Submission #1088820

# Submission time Handle Problem Language Result Execution time Memory
1088820 2024-09-15T08:18:27 Z phong Capital City (JOI20_capital_city) C++17
100 / 100
421 ms 42868 KB
#include<bits/stdc++.h>
#define ll long long
const int nmax = 2e5 + 5, N = 1e6;
const ll oo = 1e9 + 5, base = 311;
const int lg = 62, tx = 26;
const ll mod = 1e9 + 7, mod2 = 1e9+3;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
#define NAME code
using namespace std;


int n, k;
vector<int> adj[nmax];
vector<int> mau[nmax];
int idx[nmax], c[nmax];
int sz[nmax], vis[nmax], cntw;

void dfs_1(int u, int p){
    sz[u] = 1;
    for(auto v : adj[u]){
        if(vis[v] || v == p) continue;
        dfs_1(v, u);
        sz[u] += sz[v];
    }
}

int Find(int u, int p){
    for(auto v : adj[u]){
        if(vis[v] || v == p) continue;
        if(sz[v] > cntw / 2) return Find(v, u);
    }
    return u;
}
int rev[nmax], ok[nmax], par[nmax];

int res = 0;
void Init(int u, int p, int val){
    ok[u] = val;
    for(auto v : adj[u]){
        if(vis[v] || v == p) continue;
        par[v] = u;
        Init(v, u, val);
    }
}
struct holder{
    int vis[nmax];
    vector<int> tmp;
    void add(int u){
        vis[u] = 1;
        tmp.push_back(u);
    }
    void clear(){
        for(auto p : tmp) vis[p] = 0;
        tmp.clear();
    }
}one;
queue<int> q;
void solve(int u){
    int root = u;
    while(q.size()) q.pop();
    for(auto p : mau[c[root]]){
        if(!ok[p]){
            res = oo;
            return;
        }
        q.push(p);
        one.add(p);
    }
    while(q.size()){
        int u = q.front();q.pop();
        if(u == root) continue;
        int v = par[u];
//        cout << u << ' ' << v << endl;
        if(one.vis[v]) continue;
        res++;
        for(auto p : mau[c[v]]){
            if(!ok[p]){
                res = oo;
                return;
            }
            q.push(p);
            one.add(p);
        }
     }
}
int centroid(int u){
    dfs_1(u, -1); cntw = sz[u];
    int root = Find(u,-1);vis[root] = 1;
    res = 0;
    one.clear();
    Init(root, -1, 1);
    solve(root);
    Init(root, -1, 0);
    int cur = res;
    for(auto v : adj[root]){
        if(!vis[v])cur = min(cur, centroid(v));
    }
    return cur;
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
//    freopen("code.inp", "r", stdin);
//    freopen("code.out", "w", stdout);
    cin >> n >> k;
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for(int i = 1; i <= n; ++i){
        cin >> c[i];
        mau[c[i]].push_back(i);
    }
    cout << centroid(1);
}
/*
7 3
3 2
1 3
7 3
5 2
6 2
4 7
2 1 3 1 2 3 2

*/

Compilation message

capital_city.cpp:104:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  104 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 4 ms 9816 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 4 ms 9736 KB Output is correct
5 Correct 4 ms 9820 KB Output is correct
6 Correct 4 ms 9816 KB Output is correct
7 Correct 5 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 4 ms 9892 KB Output is correct
10 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 9816 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 4 ms 9736 KB Output is correct
5 Correct 4 ms 9820 KB Output is correct
6 Correct 4 ms 9816 KB Output is correct
7 Correct 5 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 4 ms 9892 KB Output is correct
10 Correct 4 ms 9816 KB Output is correct
11 Correct 5 ms 9820 KB Output is correct
12 Correct 5 ms 9928 KB Output is correct
13 Correct 5 ms 9820 KB Output is correct
14 Correct 5 ms 9960 KB Output is correct
15 Correct 5 ms 10076 KB Output is correct
16 Correct 5 ms 9816 KB Output is correct
17 Correct 5 ms 10072 KB Output is correct
18 Correct 5 ms 10060 KB Output is correct
19 Correct 6 ms 10072 KB Output is correct
20 Correct 5 ms 10076 KB Output is correct
21 Correct 5 ms 10072 KB Output is correct
22 Correct 5 ms 10076 KB Output is correct
23 Correct 5 ms 10076 KB Output is correct
24 Correct 5 ms 10076 KB Output is correct
25 Correct 6 ms 10076 KB Output is correct
26 Correct 5 ms 10072 KB Output is correct
27 Correct 6 ms 10072 KB Output is correct
28 Correct 7 ms 10076 KB Output is correct
29 Correct 6 ms 10076 KB Output is correct
30 Correct 7 ms 10136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 332 ms 42580 KB Output is correct
2 Correct 144 ms 42828 KB Output is correct
3 Correct 320 ms 42440 KB Output is correct
4 Correct 137 ms 42836 KB Output is correct
5 Correct 320 ms 39504 KB Output is correct
6 Correct 136 ms 42832 KB Output is correct
7 Correct 326 ms 39912 KB Output is correct
8 Correct 134 ms 42828 KB Output is correct
9 Correct 379 ms 38400 KB Output is correct
10 Correct 410 ms 35996 KB Output is correct
11 Correct 386 ms 38856 KB Output is correct
12 Correct 395 ms 41420 KB Output is correct
13 Correct 409 ms 35532 KB Output is correct
14 Correct 396 ms 41820 KB Output is correct
15 Correct 421 ms 41416 KB Output is correct
16 Correct 405 ms 36552 KB Output is correct
17 Correct 399 ms 37068 KB Output is correct
18 Correct 379 ms 37324 KB Output is correct
19 Correct 403 ms 40536 KB Output is correct
20 Correct 387 ms 42696 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 9816 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 4 ms 9736 KB Output is correct
5 Correct 4 ms 9820 KB Output is correct
6 Correct 4 ms 9816 KB Output is correct
7 Correct 5 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 4 ms 9892 KB Output is correct
10 Correct 4 ms 9816 KB Output is correct
11 Correct 5 ms 9820 KB Output is correct
12 Correct 5 ms 9928 KB Output is correct
13 Correct 5 ms 9820 KB Output is correct
14 Correct 5 ms 9960 KB Output is correct
15 Correct 5 ms 10076 KB Output is correct
16 Correct 5 ms 9816 KB Output is correct
17 Correct 5 ms 10072 KB Output is correct
18 Correct 5 ms 10060 KB Output is correct
19 Correct 6 ms 10072 KB Output is correct
20 Correct 5 ms 10076 KB Output is correct
21 Correct 5 ms 10072 KB Output is correct
22 Correct 5 ms 10076 KB Output is correct
23 Correct 5 ms 10076 KB Output is correct
24 Correct 5 ms 10076 KB Output is correct
25 Correct 6 ms 10076 KB Output is correct
26 Correct 5 ms 10072 KB Output is correct
27 Correct 6 ms 10072 KB Output is correct
28 Correct 7 ms 10076 KB Output is correct
29 Correct 6 ms 10076 KB Output is correct
30 Correct 7 ms 10136 KB Output is correct
31 Correct 332 ms 42580 KB Output is correct
32 Correct 144 ms 42828 KB Output is correct
33 Correct 320 ms 42440 KB Output is correct
34 Correct 137 ms 42836 KB Output is correct
35 Correct 320 ms 39504 KB Output is correct
36 Correct 136 ms 42832 KB Output is correct
37 Correct 326 ms 39912 KB Output is correct
38 Correct 134 ms 42828 KB Output is correct
39 Correct 379 ms 38400 KB Output is correct
40 Correct 410 ms 35996 KB Output is correct
41 Correct 386 ms 38856 KB Output is correct
42 Correct 395 ms 41420 KB Output is correct
43 Correct 409 ms 35532 KB Output is correct
44 Correct 396 ms 41820 KB Output is correct
45 Correct 421 ms 41416 KB Output is correct
46 Correct 405 ms 36552 KB Output is correct
47 Correct 399 ms 37068 KB Output is correct
48 Correct 379 ms 37324 KB Output is correct
49 Correct 403 ms 40536 KB Output is correct
50 Correct 387 ms 42696 KB Output is correct
51 Correct 4 ms 9816 KB Output is correct
52 Correct 207 ms 27208 KB Output is correct
53 Correct 235 ms 27336 KB Output is correct
54 Correct 235 ms 27196 KB Output is correct
55 Correct 226 ms 27208 KB Output is correct
56 Correct 223 ms 27232 KB Output is correct
57 Correct 257 ms 27208 KB Output is correct
58 Correct 251 ms 29908 KB Output is correct
59 Correct 244 ms 30032 KB Output is correct
60 Correct 268 ms 29368 KB Output is correct
61 Correct 287 ms 29012 KB Output is correct
62 Correct 145 ms 42832 KB Output is correct
63 Correct 165 ms 42868 KB Output is correct
64 Correct 145 ms 42740 KB Output is correct
65 Correct 138 ms 42668 KB Output is correct
66 Correct 214 ms 35452 KB Output is correct
67 Correct 202 ms 35528 KB Output is correct
68 Correct 210 ms 35484 KB Output is correct
69 Correct 203 ms 35488 KB Output is correct
70 Correct 208 ms 35520 KB Output is correct
71 Correct 212 ms 35456 KB Output is correct
72 Correct 198 ms 35524 KB Output is correct
73 Correct 201 ms 34520 KB Output is correct
74 Correct 201 ms 35532 KB Output is correct
75 Correct 196 ms 35460 KB Output is correct
76 Correct 312 ms 33480 KB Output is correct
77 Correct 301 ms 31536 KB Output is correct
78 Correct 387 ms 37088 KB Output is correct
79 Correct 405 ms 35012 KB Output is correct
80 Correct 411 ms 42188 KB Output is correct
81 Correct 403 ms 38600 KB Output is correct
82 Correct 398 ms 38736 KB Output is correct
83 Correct 377 ms 35512 KB Output is correct
84 Correct 381 ms 41160 KB Output is correct
85 Correct 377 ms 39624 KB Output is correct
86 Correct 368 ms 35020 KB Output is correct
87 Correct 365 ms 36872 KB Output is correct
88 Correct 308 ms 37928 KB Output is correct
89 Correct 334 ms 33808 KB Output is correct
90 Correct 335 ms 33736 KB Output is correct
91 Correct 345 ms 35976 KB Output is correct
92 Correct 323 ms 34608 KB Output is correct
93 Correct 346 ms 34508 KB Output is correct
94 Correct 318 ms 33740 KB Output is correct
95 Correct 335 ms 35164 KB Output is correct
96 Correct 335 ms 34024 KB Output is correct
97 Correct 324 ms 36040 KB Output is correct