답안 #1082770

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082770 2024-09-01T13:53:55 Z Evirir 수도 (JOI20_capital_city) C++17
41 / 100
3000 ms 39428 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")
#define setp(x) cout<<fixed<<setprecision(x)
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
//template<typename T>
//using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
template<typename T> void amax(T &a, T b){ a=max(a,b); }
template<typename T> void amin(T &a, T b){ a=min(a,b); }
struct Hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

const ll INF = ll(1e18);
const int MOD = 998244353;
const bool DEBUG = 0;
const int MAXN = 200005;
const int LG = 21;

int n,K;
vector<int> adj[MAXN];
int c[MAXN], sub[MAXN], cnt[MAXN];
bool cvst[MAXN];

void dfs_sz(int u, int p)
{
    sub[u] = 1;
    for (const auto v : adj[u])
    {
        if (v == p || cvst[v]) continue;
        dfs_sz(v, u);
        sub[u] += sub[v];
    }
}

int centroid(int u, int tot)
{
    int p = -1;
    while (true)
    {
        bool ok = 1;
        for (const auto v : adj[u])
        {
            if (v == p || cvst[v]) continue;
            if (sub[v] * 2 > tot)
            {
                p = u;
                u = v;
                ok = 0;
                break;
            }
        }
        if (ok) return u;
    }
}

int scnt[MAXN];
bool need[MAXN];
vector<int> s, needv;
vector<int> prt[MAXN];

void dfs(int u, int p)
{
    s.pb(c[u]);
    scnt[c[u]]++;
    if (p != -1) prt[c[u]].pb(c[p]);
    for (const auto v : adj[u])
    {
        if (v == p || cvst[u]) continue;
        dfs(v, u);
    }
}

int ans = 1e9;

void solve(int u)
{
    dfs_sz(u, -1);
    int tot = sub[u];
    u = centroid(u, tot);

    dfs(u, -1);
    queue<int> q;
    q.push(c[u]);
    needv.pb(c[u]);
    need[c[u]] = 1;
    while (!q.empty())
    {
        int curc = q.front(); q.pop();
        for (const auto x : prt[curc])
        {
            if (need[x]) continue;
            need[x] = 1;
            needv.pb(x);
            q.push(x);
        }
    }
    bool ok = 1;
    for (const auto x : needv)
    {
        if (cnt[x] != scnt[x])
        {
            ok = 0;
            break;
        }
    }
    if (ok) amin(ans, sz(needv) - 1);

    // clean up
    for (const auto x : s)
    {
        scnt[x] = 0;
        prt[x].clear();
        need[x] = 0;
    }
    s.clear();
    needv.clear();

    cvst[u] = 1;
    for (const auto v : adj[u])
    {
        if (cvst[v]) continue;
        solve(v);
    }
}

void init()
{
    // adj.resize(n);
    // c.resize(n);
    // cvst.resize(n);
    // sub.resize(n);
    // cnt.resize(K);
    // scnt.resize(K);
    // prt.resize(K);
    // need.resize(K);
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);

    cin>>n>>K;
    init();

    forn(i,0,n-1)
    {
        int u,v; cin>>u>>v; u--; v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    forn(i,0,n)
    {
        cin>>c[i];
        c[i]--;
        cnt[c[i]]++;
    }

    solve(0);
    cout << ans << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13148 KB Output is correct
4 Correct 3 ms 13400 KB Output is correct
5 Correct 2 ms 13148 KB Output is correct
6 Correct 2 ms 13148 KB Output is correct
7 Correct 2 ms 13148 KB Output is correct
8 Correct 2 ms 13148 KB Output is correct
9 Correct 2 ms 13148 KB Output is correct
10 Correct 2 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13148 KB Output is correct
4 Correct 3 ms 13400 KB Output is correct
5 Correct 2 ms 13148 KB Output is correct
6 Correct 2 ms 13148 KB Output is correct
7 Correct 2 ms 13148 KB Output is correct
8 Correct 2 ms 13148 KB Output is correct
9 Correct 2 ms 13148 KB Output is correct
10 Correct 2 ms 13148 KB Output is correct
11 Correct 3 ms 13400 KB Output is correct
12 Correct 3 ms 13472 KB Output is correct
13 Correct 3 ms 13400 KB Output is correct
14 Correct 3 ms 13404 KB Output is correct
15 Correct 3 ms 13404 KB Output is correct
16 Correct 3 ms 13404 KB Output is correct
17 Correct 4 ms 13404 KB Output is correct
18 Correct 4 ms 13552 KB Output is correct
19 Correct 4 ms 13404 KB Output is correct
20 Correct 3 ms 13404 KB Output is correct
21 Correct 3 ms 13404 KB Output is correct
22 Correct 3 ms 13404 KB Output is correct
23 Correct 3 ms 13560 KB Output is correct
24 Correct 3 ms 13524 KB Output is correct
25 Correct 4 ms 13404 KB Output is correct
26 Correct 3 ms 13404 KB Output is correct
27 Correct 3 ms 13404 KB Output is correct
28 Correct 3 ms 13404 KB Output is correct
29 Correct 4 ms 13404 KB Output is correct
30 Correct 4 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 38832 KB Output is correct
2 Correct 129 ms 39288 KB Output is correct
3 Correct 228 ms 38528 KB Output is correct
4 Correct 130 ms 39368 KB Output is correct
5 Correct 223 ms 35512 KB Output is correct
6 Correct 143 ms 39372 KB Output is correct
7 Correct 227 ms 35884 KB Output is correct
8 Correct 136 ms 39112 KB Output is correct
9 Correct 269 ms 33992 KB Output is correct
10 Correct 282 ms 31548 KB Output is correct
11 Correct 290 ms 34616 KB Output is correct
12 Correct 283 ms 37060 KB Output is correct
13 Correct 281 ms 31156 KB Output is correct
14 Correct 283 ms 37492 KB Output is correct
15 Correct 281 ms 37132 KB Output is correct
16 Correct 323 ms 32012 KB Output is correct
17 Correct 267 ms 32712 KB Output is correct
18 Correct 279 ms 32788 KB Output is correct
19 Correct 268 ms 36036 KB Output is correct
20 Correct 299 ms 38088 KB Output is correct
21 Correct 2 ms 13144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13148 KB Output is correct
4 Correct 3 ms 13400 KB Output is correct
5 Correct 2 ms 13148 KB Output is correct
6 Correct 2 ms 13148 KB Output is correct
7 Correct 2 ms 13148 KB Output is correct
8 Correct 2 ms 13148 KB Output is correct
9 Correct 2 ms 13148 KB Output is correct
10 Correct 2 ms 13148 KB Output is correct
11 Correct 3 ms 13400 KB Output is correct
12 Correct 3 ms 13472 KB Output is correct
13 Correct 3 ms 13400 KB Output is correct
14 Correct 3 ms 13404 KB Output is correct
15 Correct 3 ms 13404 KB Output is correct
16 Correct 3 ms 13404 KB Output is correct
17 Correct 4 ms 13404 KB Output is correct
18 Correct 4 ms 13552 KB Output is correct
19 Correct 4 ms 13404 KB Output is correct
20 Correct 3 ms 13404 KB Output is correct
21 Correct 3 ms 13404 KB Output is correct
22 Correct 3 ms 13404 KB Output is correct
23 Correct 3 ms 13560 KB Output is correct
24 Correct 3 ms 13524 KB Output is correct
25 Correct 4 ms 13404 KB Output is correct
26 Correct 3 ms 13404 KB Output is correct
27 Correct 3 ms 13404 KB Output is correct
28 Correct 3 ms 13404 KB Output is correct
29 Correct 4 ms 13404 KB Output is correct
30 Correct 4 ms 13404 KB Output is correct
31 Correct 236 ms 38832 KB Output is correct
32 Correct 129 ms 39288 KB Output is correct
33 Correct 228 ms 38528 KB Output is correct
34 Correct 130 ms 39368 KB Output is correct
35 Correct 223 ms 35512 KB Output is correct
36 Correct 143 ms 39372 KB Output is correct
37 Correct 227 ms 35884 KB Output is correct
38 Correct 136 ms 39112 KB Output is correct
39 Correct 269 ms 33992 KB Output is correct
40 Correct 282 ms 31548 KB Output is correct
41 Correct 290 ms 34616 KB Output is correct
42 Correct 283 ms 37060 KB Output is correct
43 Correct 281 ms 31156 KB Output is correct
44 Correct 283 ms 37492 KB Output is correct
45 Correct 281 ms 37132 KB Output is correct
46 Correct 323 ms 32012 KB Output is correct
47 Correct 267 ms 32712 KB Output is correct
48 Correct 279 ms 32788 KB Output is correct
49 Correct 268 ms 36036 KB Output is correct
50 Correct 299 ms 38088 KB Output is correct
51 Correct 2 ms 13144 KB Output is correct
52 Correct 188 ms 22328 KB Output is correct
53 Correct 182 ms 22236 KB Output is correct
54 Correct 176 ms 22212 KB Output is correct
55 Correct 184 ms 22216 KB Output is correct
56 Correct 199 ms 22216 KB Output is correct
57 Correct 182 ms 22220 KB Output is correct
58 Correct 182 ms 25804 KB Output is correct
59 Correct 175 ms 25904 KB Output is correct
60 Correct 207 ms 25284 KB Output is correct
61 Correct 248 ms 24780 KB Output is correct
62 Correct 143 ms 39212 KB Output is correct
63 Correct 131 ms 39428 KB Output is correct
64 Correct 128 ms 38988 KB Output is correct
65 Correct 131 ms 39368 KB Output is correct
66 Execution timed out 3061 ms 30788 KB Time limit exceeded
67 Halted 0 ms 0 KB -