답안 #694208

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
694208 2023-02-04T03:38:05 Z Do_you_copy Mergers (JOI19_mergers) C++17
70 / 100
140 ms 32800 KB
//Then
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 1;
//const int Mod = 1e9 + 7;
//const int inf =
int n, k;
int id[maxN];
int lab[maxN];
vector <int> S[maxN];
int sum[maxN];
pii e[maxN];
vector <int> adj[maxN], adj1[maxN];
int depth[maxN], lift[maxN][20];
int find_set(int u){
    return lab[u] < 0 ? u : lab[u] = find_set(lab[u]);
}

bool unite(int u, int v){
    u = find_set(u);
    v = find_set(v);
    if (u == v) return 0;
    lab[u] += lab[v];
    lab[v] = u;
    return 1;
}

inline int lca(int u, int v){
    if (depth[u] < depth[v]) swap(u, v);
    int k = depth[u] - depth[v];
    while (k){
        int t = __lg(k);
        u = lift[u][t];
        k ^= 1 << t;
    }
    if (u == v) return u;
    for (int i = 19; i >= 0; --i){
        if (lift[u][i] == lift[v][i]) continue;
        u = lift[u][i];
        v = lift[v][i];
    }
    return lift[u][0];
}

void dfs(int u, int p){
    depth[u] = depth[p] + 1;
    lift[u][0] = p;
    for (int i = 1; i < 20; ++i) lift[u][i] = lift[lift[u][i - 1]][i - 1];
    for (int i: adj[u]){
        if (i == p) continue;
        dfs(i, u);
    }
}

void dfs1(int u, int p){
    for (int i: adj[u]){
        if (i == p) continue;
        dfs1(i, u);
        sum[u] += sum[i];
    }
    if (sum[u] < 0){
        unite(id[u], id[p]);
    }
}

int leaf;
void dfs2(int u, int p){
    if (adj1[u].size() == 1) ++leaf;
    for (int i: adj1[u]){
        if (i == p) continue;
        dfs2(i, u);
    }
}

void Init(){
    cin >> n >> k;
    for (int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
        e[i] = {u, v};
    }
    for (int i = 1; i <= n; ++i){
        cin >> id[i];
        S[id[i]].pb(i);
    }
    fill(lab + 1, lab + n + 1, -1);
    dfs(1, 0);
    for (int i = 1; i <= k; ++i){
        int root = S[i][0];
        for (int j = 1; j < signed(S[i].size()); ++j){
            root = lca(root, S[i][j]);
        }
        for (int j: S[i]){
            --sum[j];
        }
        sum[root] += S[i].size();
    }
    dfs1(1, 0);
    for (int i = 1; i < n; ++i){
        int u = find_set(id[e[i].fi]);
        int v = find_set(id[e[i].se]);
        if (u == v) continue;
        adj1[u].pb(v);
        adj1[v].pb(u);
    }
    for (int i = 1; i <= k; ++i){
        if (adj1[i].size()){
            dfs2(i, 0);
            break;
        }
    }
    cout << (leaf + 1) / 2;
}

#define debug
#define taskname "test"
signed main(){
    faster
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    int tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
        timeout.close();
        #ifndef debug
        cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
        #endif // debug
    }
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7348 KB Output is correct
5 Correct 4 ms 7388 KB Output is correct
6 Correct 4 ms 7384 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7384 KB Output is correct
10 Correct 4 ms 7296 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7404 KB Output is correct
14 Correct 4 ms 7396 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7384 KB Output is correct
17 Correct 4 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 3 ms 7380 KB Output is correct
21 Correct 4 ms 7304 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7384 KB Output is correct
24 Correct 3 ms 7388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7348 KB Output is correct
5 Correct 4 ms 7388 KB Output is correct
6 Correct 4 ms 7384 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7384 KB Output is correct
10 Correct 4 ms 7296 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7404 KB Output is correct
14 Correct 4 ms 7396 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7384 KB Output is correct
17 Correct 4 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 3 ms 7380 KB Output is correct
21 Correct 4 ms 7304 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7384 KB Output is correct
24 Correct 3 ms 7388 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 6 ms 7892 KB Output is correct
27 Correct 6 ms 7824 KB Output is correct
28 Correct 6 ms 8020 KB Output is correct
29 Correct 7 ms 8020 KB Output is correct
30 Correct 5 ms 7764 KB Output is correct
31 Correct 4 ms 7388 KB Output is correct
32 Correct 6 ms 8096 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 5 ms 7764 KB Output is correct
35 Correct 7 ms 8148 KB Output is correct
36 Correct 5 ms 7784 KB Output is correct
37 Correct 6 ms 7892 KB Output is correct
38 Correct 4 ms 7508 KB Output is correct
39 Correct 6 ms 7784 KB Output is correct
40 Correct 6 ms 7760 KB Output is correct
41 Correct 5 ms 7784 KB Output is correct
42 Correct 6 ms 7892 KB Output is correct
43 Correct 6 ms 8040 KB Output is correct
44 Correct 4 ms 7392 KB Output is correct
45 Correct 6 ms 7912 KB Output is correct
46 Correct 5 ms 7892 KB Output is correct
47 Correct 4 ms 7380 KB Output is correct
48 Correct 6 ms 7764 KB Output is correct
49 Correct 6 ms 8036 KB Output is correct
50 Correct 6 ms 8020 KB Output is correct
51 Correct 6 ms 7784 KB Output is correct
52 Correct 6 ms 7804 KB Output is correct
53 Correct 6 ms 7912 KB Output is correct
54 Correct 5 ms 7764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7348 KB Output is correct
5 Correct 4 ms 7388 KB Output is correct
6 Correct 4 ms 7384 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7384 KB Output is correct
10 Correct 4 ms 7296 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7404 KB Output is correct
14 Correct 4 ms 7396 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7384 KB Output is correct
17 Correct 4 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 3 ms 7380 KB Output is correct
21 Correct 4 ms 7304 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7384 KB Output is correct
24 Correct 3 ms 7388 KB Output is correct
25 Correct 6 ms 7380 KB Output is correct
26 Correct 61 ms 23092 KB Output is correct
27 Correct 93 ms 22736 KB Output is correct
28 Correct 5 ms 7764 KB Output is correct
29 Correct 5 ms 7380 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 91 ms 22656 KB Output is correct
32 Correct 5 ms 7764 KB Output is correct
33 Correct 124 ms 29984 KB Output is correct
34 Correct 83 ms 22736 KB Output is correct
35 Correct 7 ms 7856 KB Output is correct
36 Correct 110 ms 23412 KB Output is correct
37 Correct 7 ms 7828 KB Output is correct
38 Correct 6 ms 7852 KB Output is correct
39 Correct 60 ms 23004 KB Output is correct
40 Correct 7 ms 8132 KB Output is correct
41 Correct 83 ms 22656 KB Output is correct
42 Correct 112 ms 25016 KB Output is correct
43 Correct 4 ms 7424 KB Output is correct
44 Correct 113 ms 30100 KB Output is correct
45 Correct 114 ms 26356 KB Output is correct
46 Correct 7 ms 7780 KB Output is correct
47 Correct 7 ms 7784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 23156 KB Output is correct
2 Correct 78 ms 29392 KB Output is correct
3 Correct 6 ms 7876 KB Output is correct
4 Correct 5 ms 7764 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7388 KB Output is correct
7 Correct 6 ms 7764 KB Output is correct
8 Correct 101 ms 24380 KB Output is correct
9 Correct 6 ms 7796 KB Output is correct
10 Correct 92 ms 22876 KB Output is correct
11 Correct 5 ms 7380 KB Output is correct
12 Correct 98 ms 22792 KB Output is correct
13 Correct 130 ms 24516 KB Output is correct
14 Correct 108 ms 28800 KB Output is correct
15 Correct 63 ms 22928 KB Output is correct
16 Correct 6 ms 7892 KB Output is correct
17 Correct 4 ms 7380 KB Output is correct
18 Correct 81 ms 28288 KB Output is correct
19 Correct 140 ms 32800 KB Output is correct
20 Correct 5 ms 7892 KB Output is correct
21 Correct 4 ms 7384 KB Output is correct
22 Correct 77 ms 24812 KB Output is correct
23 Correct 7 ms 7784 KB Output is correct
24 Correct 101 ms 23360 KB Output is correct
25 Correct 125 ms 31196 KB Output is correct
26 Correct 5 ms 8020 KB Output is correct
27 Correct 6 ms 8044 KB Output is correct
28 Correct 7 ms 7780 KB Output is correct
29 Correct 5 ms 7788 KB Output is correct
30 Correct 6 ms 7892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7348 KB Output is correct
5 Correct 4 ms 7388 KB Output is correct
6 Correct 4 ms 7384 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7384 KB Output is correct
10 Correct 4 ms 7296 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7404 KB Output is correct
14 Correct 4 ms 7396 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7384 KB Output is correct
17 Correct 4 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 3 ms 7380 KB Output is correct
21 Correct 4 ms 7304 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7384 KB Output is correct
24 Correct 3 ms 7388 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 6 ms 7892 KB Output is correct
27 Correct 6 ms 7824 KB Output is correct
28 Correct 6 ms 8020 KB Output is correct
29 Correct 7 ms 8020 KB Output is correct
30 Correct 5 ms 7764 KB Output is correct
31 Correct 4 ms 7388 KB Output is correct
32 Correct 6 ms 8096 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 5 ms 7764 KB Output is correct
35 Correct 7 ms 8148 KB Output is correct
36 Correct 5 ms 7784 KB Output is correct
37 Correct 6 ms 7892 KB Output is correct
38 Correct 4 ms 7508 KB Output is correct
39 Correct 6 ms 7784 KB Output is correct
40 Correct 6 ms 7760 KB Output is correct
41 Correct 5 ms 7784 KB Output is correct
42 Correct 6 ms 7892 KB Output is correct
43 Correct 6 ms 8040 KB Output is correct
44 Correct 4 ms 7392 KB Output is correct
45 Correct 6 ms 7912 KB Output is correct
46 Correct 5 ms 7892 KB Output is correct
47 Correct 4 ms 7380 KB Output is correct
48 Correct 6 ms 7764 KB Output is correct
49 Correct 6 ms 8036 KB Output is correct
50 Correct 6 ms 8020 KB Output is correct
51 Correct 6 ms 7784 KB Output is correct
52 Correct 6 ms 7804 KB Output is correct
53 Correct 6 ms 7912 KB Output is correct
54 Correct 5 ms 7764 KB Output is correct
55 Correct 6 ms 7380 KB Output is correct
56 Correct 61 ms 23092 KB Output is correct
57 Correct 93 ms 22736 KB Output is correct
58 Correct 5 ms 7764 KB Output is correct
59 Correct 5 ms 7380 KB Output is correct
60 Correct 4 ms 7380 KB Output is correct
61 Correct 91 ms 22656 KB Output is correct
62 Correct 5 ms 7764 KB Output is correct
63 Correct 124 ms 29984 KB Output is correct
64 Correct 83 ms 22736 KB Output is correct
65 Correct 7 ms 7856 KB Output is correct
66 Correct 110 ms 23412 KB Output is correct
67 Correct 7 ms 7828 KB Output is correct
68 Correct 6 ms 7852 KB Output is correct
69 Correct 60 ms 23004 KB Output is correct
70 Correct 7 ms 8132 KB Output is correct
71 Correct 83 ms 22656 KB Output is correct
72 Correct 112 ms 25016 KB Output is correct
73 Correct 4 ms 7424 KB Output is correct
74 Correct 113 ms 30100 KB Output is correct
75 Correct 114 ms 26356 KB Output is correct
76 Correct 7 ms 7780 KB Output is correct
77 Correct 7 ms 7784 KB Output is correct
78 Correct 71 ms 23156 KB Output is correct
79 Correct 78 ms 29392 KB Output is correct
80 Correct 6 ms 7876 KB Output is correct
81 Correct 5 ms 7764 KB Output is correct
82 Correct 4 ms 7380 KB Output is correct
83 Correct 5 ms 7388 KB Output is correct
84 Correct 6 ms 7764 KB Output is correct
85 Correct 101 ms 24380 KB Output is correct
86 Correct 6 ms 7796 KB Output is correct
87 Correct 92 ms 22876 KB Output is correct
88 Correct 5 ms 7380 KB Output is correct
89 Correct 98 ms 22792 KB Output is correct
90 Correct 130 ms 24516 KB Output is correct
91 Correct 108 ms 28800 KB Output is correct
92 Correct 63 ms 22928 KB Output is correct
93 Correct 6 ms 7892 KB Output is correct
94 Correct 4 ms 7380 KB Output is correct
95 Correct 81 ms 28288 KB Output is correct
96 Correct 140 ms 32800 KB Output is correct
97 Correct 5 ms 7892 KB Output is correct
98 Correct 4 ms 7384 KB Output is correct
99 Correct 77 ms 24812 KB Output is correct
100 Correct 7 ms 7784 KB Output is correct
101 Correct 101 ms 23360 KB Output is correct
102 Correct 125 ms 31196 KB Output is correct
103 Correct 5 ms 8020 KB Output is correct
104 Correct 6 ms 8044 KB Output is correct
105 Correct 7 ms 7780 KB Output is correct
106 Correct 5 ms 7788 KB Output is correct
107 Correct 6 ms 7892 KB Output is correct
108 Runtime error 10 ms 14700 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -