답안 #960897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960897 2024-04-11T07:55:50 Z GrindMachine Mergers (JOI19_mergers) C++17
100 / 100
1588 ms 166728 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;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
hints from edi

*/

const int MOD = 1e9 + 7;
const int N = 5e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

vector<int> adj1[N], adj2[N];
    
struct lca_algo {
    // LCA template (for graphs with 1-based indexing)
 
    int LOG = 1;
    vector<int> depth;
    vector<vector<int>> up;
    vector<int> tin, tout;
    int timer = 1;
 
    lca_algo() {
 
    }
 
    lca_algo(int n) {
        lca_init(n);
    }
 
    void lca_init(int n) {
        while ((1 << LOG) < n) LOG++;
        up = vector<vector<int>>(n + 1, vector<int>(LOG, 1));
        depth = vector<int>(n + 1);
        tin = vector<int>(n + 1);
        tout = vector<int>(n + 1);
 
        lca_dfs(1, -1);
    }
 
    void lca_dfs(int node, int par) {
        tin[node] = timer++;
 
        trav(child, adj1[node]) {
            if (child == par) conts;
 
            up[child][0] = node;
            rep1(j, LOG - 1) {
                up[child][j] = up[up[child][j - 1]][j - 1];
            }
 
            depth[child] = depth[node] + 1;
 
            lca_dfs(child, node);
        }
 
        tout[node] = timer-1;
    }
 
    int lift(int u, int k) {
        rep(j, LOG) {
            if (k & (1 << j)) {
                u = up[u][j];
            }
        }
 
        return u;
    }

    int query(int u, int v) {
        if (depth[u] < depth[v]) swap(u, v);
        int k = depth[u] - depth[v];
        u = lift(u, k);
 
        if (u == v) return u;
 
        rev(j, LOG - 1, 0) {
            if (up[u][j] != up[v][j]) {
                u = up[u][j];
                v = up[v][j];
            }
        }
 
        u = up[u][0];
        return u;
    }
 
    int get_dis(int u, int v) {
        int lca = query(u, v);
        return depth[u] + depth[v] - 2 * depth[lca];
    }
 
    bool is_ances(int u, int v){
        return tin[u] <= tin[v] and tout[u] >= tout[v];
    }
};

vector<int> here[N];
vector<int> a(N);
vector<int> bad_cnt(N);

void dfs1(int u, int p){
    trav(v,adj1[u]){
        if(v == p) conts;
        dfs1(v,u);
        bad_cnt[u] += bad_cnt[v];
    }
}

vector<int> col(N);
vector<bool> vis(N);

void dfs2(int u, int p, int c){
    vis[u] = 1;
    col[u] = c;
    trav(v,adj2[u]){
        if(v == p) conts;
        dfs2(v,u,c);
    }
}

void solve(int test_case)
{
    int n,k; cin >> n >> k;
    rep1(i,n-1){
        int u,v; cin >> u >> v;
        adj1[u].pb(v), adj1[v].pb(u);
    }
    rep1(i,n) cin >> a[i];

    rep1(i,n) here[a[i]].pb(i);
    lca_algo LCA(n);

    rep1(c,k){
        auto &nodes = here[c];
        int lca = nodes[0];
        trav(u,nodes){
            lca = LCA.query(lca,u);
        }

        trav(u,nodes){
            bad_cnt[u]++;
            bad_cnt[lca]--;
        }
    }

    dfs1(1,-1);

    rep1(u,n){
        trav(v,adj1[u]){
            if(LCA.depth[v] > LCA.depth[u]){
                if(bad_cnt[v]){
                    adj2[u].pb(v), adj2[v].pb(u);
                }
            }
        }
    }

    int ptr = 0;
    rep1(u,n){
        if(vis[u]) conts;
        dfs2(u,-1,++ptr);
    }

    vector<int> deg(n+5);

    rep1(u,n){
        trav(v,adj1[u]){
            if(u < v and col[u] != col[v]){
                deg[col[u]]++, deg[col[v]]++;
            }
        }
    }

    int ans = 0;

    if(ptr > 1){
        int leaves = 0;
        rep1(c,ptr) leaves += (deg[c] == 1);
        ans = ceil2(leaves,2);
    }

    cout << ans << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 41560 KB Output is correct
2 Correct 11 ms 41560 KB Output is correct
3 Correct 11 ms 41820 KB Output is correct
4 Correct 13 ms 41536 KB Output is correct
5 Correct 11 ms 41384 KB Output is correct
6 Correct 11 ms 41564 KB Output is correct
7 Correct 11 ms 41564 KB Output is correct
8 Correct 10 ms 41564 KB Output is correct
9 Correct 11 ms 41636 KB Output is correct
10 Correct 13 ms 41564 KB Output is correct
11 Correct 11 ms 41636 KB Output is correct
12 Correct 13 ms 41564 KB Output is correct
13 Correct 12 ms 41564 KB Output is correct
14 Correct 10 ms 41816 KB Output is correct
15 Correct 12 ms 41820 KB Output is correct
16 Correct 12 ms 41480 KB Output is correct
17 Correct 11 ms 41816 KB Output is correct
18 Correct 12 ms 41564 KB Output is correct
19 Correct 12 ms 41564 KB Output is correct
20 Correct 11 ms 41640 KB Output is correct
21 Correct 11 ms 41560 KB Output is correct
22 Correct 11 ms 41564 KB Output is correct
23 Correct 10 ms 41624 KB Output is correct
24 Correct 13 ms 41564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 41560 KB Output is correct
2 Correct 11 ms 41560 KB Output is correct
3 Correct 11 ms 41820 KB Output is correct
4 Correct 13 ms 41536 KB Output is correct
5 Correct 11 ms 41384 KB Output is correct
6 Correct 11 ms 41564 KB Output is correct
7 Correct 11 ms 41564 KB Output is correct
8 Correct 10 ms 41564 KB Output is correct
9 Correct 11 ms 41636 KB Output is correct
10 Correct 13 ms 41564 KB Output is correct
11 Correct 11 ms 41636 KB Output is correct
12 Correct 13 ms 41564 KB Output is correct
13 Correct 12 ms 41564 KB Output is correct
14 Correct 10 ms 41816 KB Output is correct
15 Correct 12 ms 41820 KB Output is correct
16 Correct 12 ms 41480 KB Output is correct
17 Correct 11 ms 41816 KB Output is correct
18 Correct 12 ms 41564 KB Output is correct
19 Correct 12 ms 41564 KB Output is correct
20 Correct 11 ms 41640 KB Output is correct
21 Correct 11 ms 41560 KB Output is correct
22 Correct 11 ms 41564 KB Output is correct
23 Correct 10 ms 41624 KB Output is correct
24 Correct 13 ms 41564 KB Output is correct
25 Correct 12 ms 41560 KB Output is correct
26 Correct 15 ms 42076 KB Output is correct
27 Correct 15 ms 42076 KB Output is correct
28 Correct 13 ms 42072 KB Output is correct
29 Correct 12 ms 42072 KB Output is correct
30 Correct 14 ms 42076 KB Output is correct
31 Correct 12 ms 41564 KB Output is correct
32 Correct 13 ms 42160 KB Output is correct
33 Correct 11 ms 41564 KB Output is correct
34 Correct 12 ms 42072 KB Output is correct
35 Correct 18 ms 42072 KB Output is correct
36 Correct 15 ms 42332 KB Output is correct
37 Correct 14 ms 42004 KB Output is correct
38 Correct 12 ms 41560 KB Output is correct
39 Correct 14 ms 42164 KB Output is correct
40 Correct 13 ms 42328 KB Output is correct
41 Correct 12 ms 42076 KB Output is correct
42 Correct 13 ms 41996 KB Output is correct
43 Correct 13 ms 42076 KB Output is correct
44 Correct 11 ms 41520 KB Output is correct
45 Correct 14 ms 42124 KB Output is correct
46 Correct 12 ms 42076 KB Output is correct
47 Correct 11 ms 41564 KB Output is correct
48 Correct 16 ms 42076 KB Output is correct
49 Correct 12 ms 42072 KB Output is correct
50 Correct 12 ms 42076 KB Output is correct
51 Correct 13 ms 42168 KB Output is correct
52 Correct 12 ms 42156 KB Output is correct
53 Correct 13 ms 42076 KB Output is correct
54 Correct 14 ms 42072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 41560 KB Output is correct
2 Correct 11 ms 41560 KB Output is correct
3 Correct 11 ms 41820 KB Output is correct
4 Correct 13 ms 41536 KB Output is correct
5 Correct 11 ms 41384 KB Output is correct
6 Correct 11 ms 41564 KB Output is correct
7 Correct 11 ms 41564 KB Output is correct
8 Correct 10 ms 41564 KB Output is correct
9 Correct 11 ms 41636 KB Output is correct
10 Correct 13 ms 41564 KB Output is correct
11 Correct 11 ms 41636 KB Output is correct
12 Correct 13 ms 41564 KB Output is correct
13 Correct 12 ms 41564 KB Output is correct
14 Correct 10 ms 41816 KB Output is correct
15 Correct 12 ms 41820 KB Output is correct
16 Correct 12 ms 41480 KB Output is correct
17 Correct 11 ms 41816 KB Output is correct
18 Correct 12 ms 41564 KB Output is correct
19 Correct 12 ms 41564 KB Output is correct
20 Correct 11 ms 41640 KB Output is correct
21 Correct 11 ms 41560 KB Output is correct
22 Correct 11 ms 41564 KB Output is correct
23 Correct 10 ms 41624 KB Output is correct
24 Correct 13 ms 41564 KB Output is correct
25 Correct 11 ms 41632 KB Output is correct
26 Correct 103 ms 62320 KB Output is correct
27 Correct 109 ms 61668 KB Output is correct
28 Correct 13 ms 42076 KB Output is correct
29 Correct 11 ms 41564 KB Output is correct
30 Correct 11 ms 41584 KB Output is correct
31 Correct 122 ms 61524 KB Output is correct
32 Correct 13 ms 42072 KB Output is correct
33 Correct 145 ms 65876 KB Output is correct
34 Correct 140 ms 61700 KB Output is correct
35 Correct 13 ms 42072 KB Output is correct
36 Correct 122 ms 61880 KB Output is correct
37 Correct 14 ms 42076 KB Output is correct
38 Correct 12 ms 42096 KB Output is correct
39 Correct 86 ms 62160 KB Output is correct
40 Correct 12 ms 42204 KB Output is correct
41 Correct 124 ms 61468 KB Output is correct
42 Correct 144 ms 63312 KB Output is correct
43 Correct 12 ms 41820 KB Output is correct
44 Correct 162 ms 65844 KB Output is correct
45 Correct 159 ms 63740 KB Output is correct
46 Correct 12 ms 42076 KB Output is correct
47 Correct 13 ms 42188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 62248 KB Output is correct
2 Correct 84 ms 61636 KB Output is correct
3 Correct 13 ms 42076 KB Output is correct
4 Correct 13 ms 42076 KB Output is correct
5 Correct 11 ms 41564 KB Output is correct
6 Correct 11 ms 41844 KB Output is correct
7 Correct 13 ms 42076 KB Output is correct
8 Correct 140 ms 62732 KB Output is correct
9 Correct 14 ms 42040 KB Output is correct
10 Correct 113 ms 61776 KB Output is correct
11 Correct 12 ms 41636 KB Output is correct
12 Correct 144 ms 61516 KB Output is correct
13 Correct 122 ms 62580 KB Output is correct
14 Correct 111 ms 61452 KB Output is correct
15 Correct 92 ms 62028 KB Output is correct
16 Correct 12 ms 42072 KB Output is correct
17 Correct 12 ms 41564 KB Output is correct
18 Correct 109 ms 62072 KB Output is correct
19 Correct 124 ms 63688 KB Output is correct
20 Correct 13 ms 42076 KB Output is correct
21 Correct 13 ms 41536 KB Output is correct
22 Correct 91 ms 62924 KB Output is correct
23 Correct 13 ms 42072 KB Output is correct
24 Correct 142 ms 62028 KB Output is correct
25 Correct 126 ms 62740 KB Output is correct
26 Correct 14 ms 42160 KB Output is correct
27 Correct 14 ms 42076 KB Output is correct
28 Correct 13 ms 42108 KB Output is correct
29 Correct 16 ms 42076 KB Output is correct
30 Correct 14 ms 42072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 41560 KB Output is correct
2 Correct 11 ms 41560 KB Output is correct
3 Correct 11 ms 41820 KB Output is correct
4 Correct 13 ms 41536 KB Output is correct
5 Correct 11 ms 41384 KB Output is correct
6 Correct 11 ms 41564 KB Output is correct
7 Correct 11 ms 41564 KB Output is correct
8 Correct 10 ms 41564 KB Output is correct
9 Correct 11 ms 41636 KB Output is correct
10 Correct 13 ms 41564 KB Output is correct
11 Correct 11 ms 41636 KB Output is correct
12 Correct 13 ms 41564 KB Output is correct
13 Correct 12 ms 41564 KB Output is correct
14 Correct 10 ms 41816 KB Output is correct
15 Correct 12 ms 41820 KB Output is correct
16 Correct 12 ms 41480 KB Output is correct
17 Correct 11 ms 41816 KB Output is correct
18 Correct 12 ms 41564 KB Output is correct
19 Correct 12 ms 41564 KB Output is correct
20 Correct 11 ms 41640 KB Output is correct
21 Correct 11 ms 41560 KB Output is correct
22 Correct 11 ms 41564 KB Output is correct
23 Correct 10 ms 41624 KB Output is correct
24 Correct 13 ms 41564 KB Output is correct
25 Correct 12 ms 41560 KB Output is correct
26 Correct 15 ms 42076 KB Output is correct
27 Correct 15 ms 42076 KB Output is correct
28 Correct 13 ms 42072 KB Output is correct
29 Correct 12 ms 42072 KB Output is correct
30 Correct 14 ms 42076 KB Output is correct
31 Correct 12 ms 41564 KB Output is correct
32 Correct 13 ms 42160 KB Output is correct
33 Correct 11 ms 41564 KB Output is correct
34 Correct 12 ms 42072 KB Output is correct
35 Correct 18 ms 42072 KB Output is correct
36 Correct 15 ms 42332 KB Output is correct
37 Correct 14 ms 42004 KB Output is correct
38 Correct 12 ms 41560 KB Output is correct
39 Correct 14 ms 42164 KB Output is correct
40 Correct 13 ms 42328 KB Output is correct
41 Correct 12 ms 42076 KB Output is correct
42 Correct 13 ms 41996 KB Output is correct
43 Correct 13 ms 42076 KB Output is correct
44 Correct 11 ms 41520 KB Output is correct
45 Correct 14 ms 42124 KB Output is correct
46 Correct 12 ms 42076 KB Output is correct
47 Correct 11 ms 41564 KB Output is correct
48 Correct 16 ms 42076 KB Output is correct
49 Correct 12 ms 42072 KB Output is correct
50 Correct 12 ms 42076 KB Output is correct
51 Correct 13 ms 42168 KB Output is correct
52 Correct 12 ms 42156 KB Output is correct
53 Correct 13 ms 42076 KB Output is correct
54 Correct 14 ms 42072 KB Output is correct
55 Correct 11 ms 41632 KB Output is correct
56 Correct 103 ms 62320 KB Output is correct
57 Correct 109 ms 61668 KB Output is correct
58 Correct 13 ms 42076 KB Output is correct
59 Correct 11 ms 41564 KB Output is correct
60 Correct 11 ms 41584 KB Output is correct
61 Correct 122 ms 61524 KB Output is correct
62 Correct 13 ms 42072 KB Output is correct
63 Correct 145 ms 65876 KB Output is correct
64 Correct 140 ms 61700 KB Output is correct
65 Correct 13 ms 42072 KB Output is correct
66 Correct 122 ms 61880 KB Output is correct
67 Correct 14 ms 42076 KB Output is correct
68 Correct 12 ms 42096 KB Output is correct
69 Correct 86 ms 62160 KB Output is correct
70 Correct 12 ms 42204 KB Output is correct
71 Correct 124 ms 61468 KB Output is correct
72 Correct 144 ms 63312 KB Output is correct
73 Correct 12 ms 41820 KB Output is correct
74 Correct 162 ms 65844 KB Output is correct
75 Correct 159 ms 63740 KB Output is correct
76 Correct 12 ms 42076 KB Output is correct
77 Correct 13 ms 42188 KB Output is correct
78 Correct 94 ms 62248 KB Output is correct
79 Correct 84 ms 61636 KB Output is correct
80 Correct 13 ms 42076 KB Output is correct
81 Correct 13 ms 42076 KB Output is correct
82 Correct 11 ms 41564 KB Output is correct
83 Correct 11 ms 41844 KB Output is correct
84 Correct 13 ms 42076 KB Output is correct
85 Correct 140 ms 62732 KB Output is correct
86 Correct 14 ms 42040 KB Output is correct
87 Correct 113 ms 61776 KB Output is correct
88 Correct 12 ms 41636 KB Output is correct
89 Correct 144 ms 61516 KB Output is correct
90 Correct 122 ms 62580 KB Output is correct
91 Correct 111 ms 61452 KB Output is correct
92 Correct 92 ms 62028 KB Output is correct
93 Correct 12 ms 42072 KB Output is correct
94 Correct 12 ms 41564 KB Output is correct
95 Correct 109 ms 62072 KB Output is correct
96 Correct 124 ms 63688 KB Output is correct
97 Correct 13 ms 42076 KB Output is correct
98 Correct 13 ms 41536 KB Output is correct
99 Correct 91 ms 62924 KB Output is correct
100 Correct 13 ms 42072 KB Output is correct
101 Correct 142 ms 62028 KB Output is correct
102 Correct 126 ms 62740 KB Output is correct
103 Correct 14 ms 42160 KB Output is correct
104 Correct 14 ms 42076 KB Output is correct
105 Correct 13 ms 42108 KB Output is correct
106 Correct 16 ms 42076 KB Output is correct
107 Correct 14 ms 42072 KB Output is correct
108 Correct 704 ms 157996 KB Output is correct
109 Correct 1588 ms 159992 KB Output is correct
110 Correct 1551 ms 166728 KB Output is correct
111 Correct 1277 ms 164836 KB Output is correct
112 Correct 1213 ms 164784 KB Output is correct
113 Correct 618 ms 153452 KB Output is correct
114 Correct 901 ms 149888 KB Output is correct
115 Correct 903 ms 149964 KB Output is correct
116 Correct 1131 ms 154300 KB Output is correct
117 Correct 754 ms 149788 KB Output is correct
118 Correct 1040 ms 149584 KB Output is correct
119 Correct 793 ms 149788 KB Output is correct
120 Correct 1127 ms 157936 KB Output is correct
121 Correct 761 ms 149996 KB Output is correct
122 Correct 1087 ms 156780 KB Output is correct
123 Correct 607 ms 151312 KB Output is correct
124 Correct 1033 ms 152688 KB Output is correct