답안 #944605

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944605 2024-03-13T01:41:09 Z VinhLuu Mergers (JOI19_mergers) C++17
100 / 100
1275 ms 182712 KB
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()

using namespace std;

typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 5e5 + 5;
const ll oo = 1e16;
const int mod = 1e9 + 7;
const int base = 23;

int n, pa[N], sz[N], g[N], k, ans, X[N], Y[N], col[N], a[N], f[N][22], dp[N], in[N], en[N], demin, b[N], st[N << 1];
vector<int> p[N], vr[N], open[N];
bool fl[N];

int fin(int u){
    return pa[u] == u ? u :  pa[u] = fin(pa[u]);
}

void dsu(int u,int v){
    u = fin(u);
    v = fin(v);
    if(u == v) return;
    if(sz[u] < sz[v]) swap(u, v);
    sz[u] += sz[v];
    pa[v] = u;
}

void update(int i){
    i += n - 1;
    st[i] ++;
    while(i > 1){
        i /= 2;
        st[i]++;
    }
}

int get(int l,int r){
    r++;
    int ret = 0;
    for(l += n - 1, r += n - 1; l < r; l /= 2, r /= 2){
        if(l & 1) ret += st[l ++];
        if(r & 1) ret += st[-- r];
    }
    return ret;
}

void pre(int u,int v){
    dp[u] = ((int)p[u].size() == 1);
    if(v == 0) for(int i = 0; i <= 20; i ++) f[u][i] = u;
    else{
        f[u][0] = v;
        for(int i = 1; i <= 20; i ++) f[u][i] = f[f[u][i - 1]][i - 1];
    }
    in[u] = ++demin;
    b[demin] = u;
    for(auto j : p[u]){
        if(j == v) continue;
        pre(j, u);
        dp[u] += dp[j];
    }
    en[u] = demin;
}

bool kt(int u,int v){
    return in[u] <= in[v] && en[u] >= in[v];
}

int lca(int u,int v){
    int kq = u;
    if(kt(u, v)) return u;
    else{
        for(int i = 20; i >= 0; i --){
            if(kt(f[u][i], v)) kq = f[u][i];
            else u = f[u][i];
        }
        return kq;
    }
}

void dfs(int u,int v){
    for(auto j : p[u]){
        if(j == v) continue;
        dfs(j, u);
    }
    for(auto j : open[u]){
        for(auto o : vr[j]) update(in[o]);
    }
    if(v && get(in[u], en[u]) != (en[u] - in[u] + 1)){
        dsu(u, v);
    }
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #define task "v"
    if(fopen(task ".inp","r")){
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }

    cin >> n >> k;

    for(int i = 1; i <= n - 1; i ++){
        cin >> X[i] >> Y[i];
        p[X[i]].pb(Y[i]);
        p[Y[i]].pb(X[i]);
    }
    int root = 1, tmp = 1;
    for(int i = 1; i <= n; i ++){
        pa[i] = i, sz[i] = 1;
        if((int)p[i].size() > tmp) root = i, tmp = (int)p[i].size();
        int x; cin >> x;
        vr[x].pb(i);
        col[i] = x;
    }
    if(n == 1){
        cout << 0;
        return 0;
    }
    if(n == 2){
        if(col[1] == col[2]){
            cout << 0 << "\n";
        }else cout << 1 << "\n";
        return 0;
    }
    in[n + 1] = n + 1;
    pre(root, 0);
     for(int i = 1; i <= k; i ++){
        if(vr[i].empty()) continue;

        sort(all(vr[i]), [&] (int x,int y){return in[x] < in[y];});
        int mi = vr[i][0];
        for(int j = 0; j < vr[i].size() - 1; j ++){
            int u = lca(vr[i][j], vr[i][j + 1]);
            if(in[u] < in[mi]) mi = u;
        }
        open[mi].pb(i);
    }
    dfs(root, 0);
    for(int i = 1; i <= n; i ++){
        for(auto j : p[i]) if(fin(j) != fin(i)) g[fin(i)]++, g[fin(j)]++;
    }
    for(int i = 1; i <= n; i ++) if(g[i] == 2) ans++;
    cerr << (ans + 1) / 2 << "\n";
   cout << (ans + 1) / 2;
}


Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:144:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |         for(int j = 0; j < vr[i].size() - 1; j ++){
      |                        ~~^~~~~~~~~~~~~~~~~~
mergers.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         freopen(task ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 57692 KB Output is correct
2 Correct 11 ms 57828 KB Output is correct
3 Correct 11 ms 57688 KB Output is correct
4 Correct 12 ms 57916 KB Output is correct
5 Correct 12 ms 57844 KB Output is correct
6 Correct 11 ms 57692 KB Output is correct
7 Correct 9 ms 45404 KB Output is correct
8 Correct 11 ms 57692 KB Output is correct
9 Correct 11 ms 57948 KB Output is correct
10 Correct 11 ms 57692 KB Output is correct
11 Correct 12 ms 57692 KB Output is correct
12 Correct 11 ms 57692 KB Output is correct
13 Correct 11 ms 57692 KB Output is correct
14 Correct 12 ms 57768 KB Output is correct
15 Correct 11 ms 57692 KB Output is correct
16 Correct 11 ms 58136 KB Output is correct
17 Correct 11 ms 57692 KB Output is correct
18 Correct 11 ms 57688 KB Output is correct
19 Correct 11 ms 57688 KB Output is correct
20 Correct 11 ms 57880 KB Output is correct
21 Correct 11 ms 57692 KB Output is correct
22 Correct 12 ms 57820 KB Output is correct
23 Correct 11 ms 57692 KB Output is correct
24 Correct 13 ms 57692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 57692 KB Output is correct
2 Correct 11 ms 57828 KB Output is correct
3 Correct 11 ms 57688 KB Output is correct
4 Correct 12 ms 57916 KB Output is correct
5 Correct 12 ms 57844 KB Output is correct
6 Correct 11 ms 57692 KB Output is correct
7 Correct 9 ms 45404 KB Output is correct
8 Correct 11 ms 57692 KB Output is correct
9 Correct 11 ms 57948 KB Output is correct
10 Correct 11 ms 57692 KB Output is correct
11 Correct 12 ms 57692 KB Output is correct
12 Correct 11 ms 57692 KB Output is correct
13 Correct 11 ms 57692 KB Output is correct
14 Correct 12 ms 57768 KB Output is correct
15 Correct 11 ms 57692 KB Output is correct
16 Correct 11 ms 58136 KB Output is correct
17 Correct 11 ms 57692 KB Output is correct
18 Correct 11 ms 57688 KB Output is correct
19 Correct 11 ms 57688 KB Output is correct
20 Correct 11 ms 57880 KB Output is correct
21 Correct 11 ms 57692 KB Output is correct
22 Correct 12 ms 57820 KB Output is correct
23 Correct 11 ms 57692 KB Output is correct
24 Correct 13 ms 57692 KB Output is correct
25 Correct 11 ms 57692 KB Output is correct
26 Correct 14 ms 57948 KB Output is correct
27 Correct 12 ms 58000 KB Output is correct
28 Correct 13 ms 58200 KB Output is correct
29 Correct 13 ms 58204 KB Output is correct
30 Correct 13 ms 57948 KB Output is correct
31 Correct 13 ms 57876 KB Output is correct
32 Correct 13 ms 58204 KB Output is correct
33 Correct 11 ms 57688 KB Output is correct
34 Correct 12 ms 57944 KB Output is correct
35 Correct 14 ms 58204 KB Output is correct
36 Correct 13 ms 57948 KB Output is correct
37 Correct 14 ms 57948 KB Output is correct
38 Correct 11 ms 57848 KB Output is correct
39 Correct 13 ms 57944 KB Output is correct
40 Correct 12 ms 57948 KB Output is correct
41 Correct 13 ms 58084 KB Output is correct
42 Correct 13 ms 57944 KB Output is correct
43 Correct 12 ms 58280 KB Output is correct
44 Correct 13 ms 57848 KB Output is correct
45 Correct 13 ms 58204 KB Output is correct
46 Correct 13 ms 58100 KB Output is correct
47 Correct 11 ms 57692 KB Output is correct
48 Correct 12 ms 58076 KB Output is correct
49 Correct 12 ms 58052 KB Output is correct
50 Correct 13 ms 58332 KB Output is correct
51 Correct 13 ms 57944 KB Output is correct
52 Correct 12 ms 57948 KB Output is correct
53 Correct 13 ms 58084 KB Output is correct
54 Correct 13 ms 57944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 57692 KB Output is correct
2 Correct 11 ms 57828 KB Output is correct
3 Correct 11 ms 57688 KB Output is correct
4 Correct 12 ms 57916 KB Output is correct
5 Correct 12 ms 57844 KB Output is correct
6 Correct 11 ms 57692 KB Output is correct
7 Correct 9 ms 45404 KB Output is correct
8 Correct 11 ms 57692 KB Output is correct
9 Correct 11 ms 57948 KB Output is correct
10 Correct 11 ms 57692 KB Output is correct
11 Correct 12 ms 57692 KB Output is correct
12 Correct 11 ms 57692 KB Output is correct
13 Correct 11 ms 57692 KB Output is correct
14 Correct 12 ms 57768 KB Output is correct
15 Correct 11 ms 57692 KB Output is correct
16 Correct 11 ms 58136 KB Output is correct
17 Correct 11 ms 57692 KB Output is correct
18 Correct 11 ms 57688 KB Output is correct
19 Correct 11 ms 57688 KB Output is correct
20 Correct 11 ms 57880 KB Output is correct
21 Correct 11 ms 57692 KB Output is correct
22 Correct 12 ms 57820 KB Output is correct
23 Correct 11 ms 57692 KB Output is correct
24 Correct 13 ms 57692 KB Output is correct
25 Correct 12 ms 57692 KB Output is correct
26 Correct 101 ms 72076 KB Output is correct
27 Correct 93 ms 71712 KB Output is correct
28 Correct 12 ms 58276 KB Output is correct
29 Correct 11 ms 57784 KB Output is correct
30 Correct 11 ms 57932 KB Output is correct
31 Correct 104 ms 71664 KB Output is correct
32 Correct 13 ms 57944 KB Output is correct
33 Correct 101 ms 78648 KB Output is correct
34 Correct 105 ms 71760 KB Output is correct
35 Correct 14 ms 57944 KB Output is correct
36 Correct 103 ms 72216 KB Output is correct
37 Correct 13 ms 57944 KB Output is correct
38 Correct 12 ms 57948 KB Output is correct
39 Correct 79 ms 71752 KB Output is correct
40 Correct 13 ms 58200 KB Output is correct
41 Correct 86 ms 71428 KB Output is correct
42 Correct 108 ms 74172 KB Output is correct
43 Correct 11 ms 57688 KB Output is correct
44 Correct 102 ms 78856 KB Output is correct
45 Correct 105 ms 75344 KB Output is correct
46 Correct 12 ms 57944 KB Output is correct
47 Correct 13 ms 58000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 72068 KB Output is correct
2 Correct 101 ms 78028 KB Output is correct
3 Correct 14 ms 58200 KB Output is correct
4 Correct 13 ms 57948 KB Output is correct
5 Correct 12 ms 57820 KB Output is correct
6 Correct 13 ms 57688 KB Output is correct
7 Correct 12 ms 57948 KB Output is correct
8 Correct 112 ms 73652 KB Output is correct
9 Correct 13 ms 58100 KB Output is correct
10 Correct 115 ms 71836 KB Output is correct
11 Correct 11 ms 57692 KB Output is correct
12 Correct 99 ms 72024 KB Output is correct
13 Correct 105 ms 74180 KB Output is correct
14 Correct 114 ms 77736 KB Output is correct
15 Correct 77 ms 71756 KB Output is correct
16 Correct 12 ms 57948 KB Output is correct
17 Correct 12 ms 57692 KB Output is correct
18 Correct 106 ms 77160 KB Output is correct
19 Correct 145 ms 81516 KB Output is correct
20 Correct 13 ms 57948 KB Output is correct
21 Correct 12 ms 57940 KB Output is correct
22 Correct 94 ms 73776 KB Output is correct
23 Correct 13 ms 57948 KB Output is correct
24 Correct 107 ms 72276 KB Output is correct
25 Correct 163 ms 80112 KB Output is correct
26 Correct 12 ms 58068 KB Output is correct
27 Correct 13 ms 58200 KB Output is correct
28 Correct 14 ms 57948 KB Output is correct
29 Correct 12 ms 57948 KB Output is correct
30 Correct 13 ms 57948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 57692 KB Output is correct
2 Correct 11 ms 57828 KB Output is correct
3 Correct 11 ms 57688 KB Output is correct
4 Correct 12 ms 57916 KB Output is correct
5 Correct 12 ms 57844 KB Output is correct
6 Correct 11 ms 57692 KB Output is correct
7 Correct 9 ms 45404 KB Output is correct
8 Correct 11 ms 57692 KB Output is correct
9 Correct 11 ms 57948 KB Output is correct
10 Correct 11 ms 57692 KB Output is correct
11 Correct 12 ms 57692 KB Output is correct
12 Correct 11 ms 57692 KB Output is correct
13 Correct 11 ms 57692 KB Output is correct
14 Correct 12 ms 57768 KB Output is correct
15 Correct 11 ms 57692 KB Output is correct
16 Correct 11 ms 58136 KB Output is correct
17 Correct 11 ms 57692 KB Output is correct
18 Correct 11 ms 57688 KB Output is correct
19 Correct 11 ms 57688 KB Output is correct
20 Correct 11 ms 57880 KB Output is correct
21 Correct 11 ms 57692 KB Output is correct
22 Correct 12 ms 57820 KB Output is correct
23 Correct 11 ms 57692 KB Output is correct
24 Correct 13 ms 57692 KB Output is correct
25 Correct 11 ms 57692 KB Output is correct
26 Correct 14 ms 57948 KB Output is correct
27 Correct 12 ms 58000 KB Output is correct
28 Correct 13 ms 58200 KB Output is correct
29 Correct 13 ms 58204 KB Output is correct
30 Correct 13 ms 57948 KB Output is correct
31 Correct 13 ms 57876 KB Output is correct
32 Correct 13 ms 58204 KB Output is correct
33 Correct 11 ms 57688 KB Output is correct
34 Correct 12 ms 57944 KB Output is correct
35 Correct 14 ms 58204 KB Output is correct
36 Correct 13 ms 57948 KB Output is correct
37 Correct 14 ms 57948 KB Output is correct
38 Correct 11 ms 57848 KB Output is correct
39 Correct 13 ms 57944 KB Output is correct
40 Correct 12 ms 57948 KB Output is correct
41 Correct 13 ms 58084 KB Output is correct
42 Correct 13 ms 57944 KB Output is correct
43 Correct 12 ms 58280 KB Output is correct
44 Correct 13 ms 57848 KB Output is correct
45 Correct 13 ms 58204 KB Output is correct
46 Correct 13 ms 58100 KB Output is correct
47 Correct 11 ms 57692 KB Output is correct
48 Correct 12 ms 58076 KB Output is correct
49 Correct 12 ms 58052 KB Output is correct
50 Correct 13 ms 58332 KB Output is correct
51 Correct 13 ms 57944 KB Output is correct
52 Correct 12 ms 57948 KB Output is correct
53 Correct 13 ms 58084 KB Output is correct
54 Correct 13 ms 57944 KB Output is correct
55 Correct 12 ms 57692 KB Output is correct
56 Correct 101 ms 72076 KB Output is correct
57 Correct 93 ms 71712 KB Output is correct
58 Correct 12 ms 58276 KB Output is correct
59 Correct 11 ms 57784 KB Output is correct
60 Correct 11 ms 57932 KB Output is correct
61 Correct 104 ms 71664 KB Output is correct
62 Correct 13 ms 57944 KB Output is correct
63 Correct 101 ms 78648 KB Output is correct
64 Correct 105 ms 71760 KB Output is correct
65 Correct 14 ms 57944 KB Output is correct
66 Correct 103 ms 72216 KB Output is correct
67 Correct 13 ms 57944 KB Output is correct
68 Correct 12 ms 57948 KB Output is correct
69 Correct 79 ms 71752 KB Output is correct
70 Correct 13 ms 58200 KB Output is correct
71 Correct 86 ms 71428 KB Output is correct
72 Correct 108 ms 74172 KB Output is correct
73 Correct 11 ms 57688 KB Output is correct
74 Correct 102 ms 78856 KB Output is correct
75 Correct 105 ms 75344 KB Output is correct
76 Correct 12 ms 57944 KB Output is correct
77 Correct 13 ms 58000 KB Output is correct
78 Correct 81 ms 72068 KB Output is correct
79 Correct 101 ms 78028 KB Output is correct
80 Correct 14 ms 58200 KB Output is correct
81 Correct 13 ms 57948 KB Output is correct
82 Correct 12 ms 57820 KB Output is correct
83 Correct 13 ms 57688 KB Output is correct
84 Correct 12 ms 57948 KB Output is correct
85 Correct 112 ms 73652 KB Output is correct
86 Correct 13 ms 58100 KB Output is correct
87 Correct 115 ms 71836 KB Output is correct
88 Correct 11 ms 57692 KB Output is correct
89 Correct 99 ms 72024 KB Output is correct
90 Correct 105 ms 74180 KB Output is correct
91 Correct 114 ms 77736 KB Output is correct
92 Correct 77 ms 71756 KB Output is correct
93 Correct 12 ms 57948 KB Output is correct
94 Correct 12 ms 57692 KB Output is correct
95 Correct 106 ms 77160 KB Output is correct
96 Correct 145 ms 81516 KB Output is correct
97 Correct 13 ms 57948 KB Output is correct
98 Correct 12 ms 57940 KB Output is correct
99 Correct 94 ms 73776 KB Output is correct
100 Correct 13 ms 57948 KB Output is correct
101 Correct 107 ms 72276 KB Output is correct
102 Correct 163 ms 80112 KB Output is correct
103 Correct 12 ms 58068 KB Output is correct
104 Correct 13 ms 58200 KB Output is correct
105 Correct 14 ms 57948 KB Output is correct
106 Correct 12 ms 57948 KB Output is correct
107 Correct 13 ms 57948 KB Output is correct
108 Correct 646 ms 139644 KB Output is correct
109 Correct 845 ms 141856 KB Output is correct
110 Correct 853 ms 158144 KB Output is correct
111 Correct 1260 ms 182712 KB Output is correct
112 Correct 1275 ms 173068 KB Output is correct
113 Correct 806 ms 156920 KB Output is correct
114 Correct 745 ms 128924 KB Output is correct
115 Correct 671 ms 128956 KB Output is correct
116 Correct 761 ms 133608 KB Output is correct
117 Correct 988 ms 160016 KB Output is correct
118 Correct 646 ms 130032 KB Output is correct
119 Correct 1024 ms 159740 KB Output is correct
120 Correct 1247 ms 176296 KB Output is correct
121 Correct 983 ms 159572 KB Output is correct
122 Correct 968 ms 143156 KB Output is correct
123 Correct 824 ms 161344 KB Output is correct
124 Correct 1122 ms 158600 KB Output is correct