Submission #694081

# Submission time Handle Problem Language Result Execution time Memory
694081 2023-02-03T17:36:14 Z tamthegod Mergers (JOI19_mergers) C++17
100 / 100
1920 ms 223408 KB
// Make the best become better
// No room for laziness
#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, m;
vector<int> adj[maxN];
vector<int> vc[maxN];
int f[maxN][21];
int depth[maxN];
pair<int, int> e[maxN];
int sum[maxN];
int lab[maxN];
int deg[maxN];
void ReadInput()
{
    cin >> n >> m;
    for(int i=1; i<n; i++)
    {
        int u, v;
        cin >> u >> v;
        e[i] = {u, v};
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for(int i=1; i<=n; i++)
    {
        int x;
        cin >> x;
        vc[x].pb(i);
    }
}
int Findset(int u)
{
    return lab[u] < 0 ? u : lab[u] = Findset(lab[u]);
}
void Unite(int u, int v)
{
    int r = Findset(u), s = Findset(v);
    if(r == s) return;
    if(lab[r] > lab[s]) swap(r, s);
    lab[r] += lab[s];
    lab[s] = r;
}
void dfs(int u, int par)
{
    for(int v : adj[u])
    {
        if(v == par) continue;
        f[v][0] = u;
        depth[v] = depth[u] + 1;
        for(int i=1; i<=20; i++)
            f[v][i] = f[f[v][i - 1]][i - 1];
        dfs(v, u);
    }
}
int lca(int u, int v)
{
    if(depth[u] < depth[v]) swap(u, v);
    int k = depth[u] - depth[v];
    for(int i=20; i>=0; i--)
        if((k >> i) & 1)
            u = f[u][i];
    if(u == v) return u;
    for(int i=20; i>=0; i--)
        if(f[u][i] != f[v][i])
        {
            u = f[u][i];
            v = f[v][i];
        }
    return f[u][0];
}
void dfs1(int u, int par)
{
    for(int v : adj[u])
    {
        if(v == par) continue;
        dfs1(v, u);
        sum[u] += sum[v];
    }
    if(sum[u]) Unite(u, par);
}
void Solve()
{
    dfs(1, 0);
    for(int i=1; i<=m; i++)
    {
        if(vc[i].empty()) continue;
        int tmp = vc[i][0];
        for(int v : vc[i])
        {
            sum[v] += -1;
            tmp = lca(tmp, v);
        }
        sum[tmp] += vc[i].size();
    }
    memset(lab, -1, sizeof lab);
    dfs1(1, 0);
    map<pair<int, int>, int> mp;
    for(int i=1; i<n; i++)
    {
        int u = Findset(e[i].fi), v = Findset(e[i].se);
        if(u == v || mp[{u, v}]) continue;
        deg[u]++;
        deg[v]++;
        mp[{u, v}] = mp[{v, u}] = 1;
    }
    int cnt = 0;
    for(int i=1; i<=n; i++)
    {
      //  cout << Findset(i) << " ";
        if(lab[i] > 0) continue;
        if(deg[i] == 1) cnt++;
    }
    cout << (cnt + 1) / 2;
}
int32_t main()
{
  //  freopen("sol.inp", "r", stdin);
  //  freopen("sol.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ReadInput();
    Solve();
}
# Verdict Execution time Memory Grader output
1 Correct 25 ms 51156 KB Output is correct
2 Correct 24 ms 51260 KB Output is correct
3 Correct 23 ms 51204 KB Output is correct
4 Correct 25 ms 51296 KB Output is correct
5 Correct 26 ms 51188 KB Output is correct
6 Correct 26 ms 51188 KB Output is correct
7 Correct 25 ms 51212 KB Output is correct
8 Correct 23 ms 51148 KB Output is correct
9 Correct 25 ms 51236 KB Output is correct
10 Correct 24 ms 51168 KB Output is correct
11 Correct 24 ms 51256 KB Output is correct
12 Correct 24 ms 51240 KB Output is correct
13 Correct 24 ms 51160 KB Output is correct
14 Correct 24 ms 51208 KB Output is correct
15 Correct 22 ms 51152 KB Output is correct
16 Correct 27 ms 51248 KB Output is correct
17 Correct 28 ms 51156 KB Output is correct
18 Correct 24 ms 51148 KB Output is correct
19 Correct 24 ms 51172 KB Output is correct
20 Correct 25 ms 51184 KB Output is correct
21 Correct 26 ms 51232 KB Output is correct
22 Correct 25 ms 51156 KB Output is correct
23 Correct 26 ms 51148 KB Output is correct
24 Correct 23 ms 51232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 51156 KB Output is correct
2 Correct 24 ms 51260 KB Output is correct
3 Correct 23 ms 51204 KB Output is correct
4 Correct 25 ms 51296 KB Output is correct
5 Correct 26 ms 51188 KB Output is correct
6 Correct 26 ms 51188 KB Output is correct
7 Correct 25 ms 51212 KB Output is correct
8 Correct 23 ms 51148 KB Output is correct
9 Correct 25 ms 51236 KB Output is correct
10 Correct 24 ms 51168 KB Output is correct
11 Correct 24 ms 51256 KB Output is correct
12 Correct 24 ms 51240 KB Output is correct
13 Correct 24 ms 51160 KB Output is correct
14 Correct 24 ms 51208 KB Output is correct
15 Correct 22 ms 51152 KB Output is correct
16 Correct 27 ms 51248 KB Output is correct
17 Correct 28 ms 51156 KB Output is correct
18 Correct 24 ms 51148 KB Output is correct
19 Correct 24 ms 51172 KB Output is correct
20 Correct 25 ms 51184 KB Output is correct
21 Correct 26 ms 51232 KB Output is correct
22 Correct 25 ms 51156 KB Output is correct
23 Correct 26 ms 51148 KB Output is correct
24 Correct 23 ms 51232 KB Output is correct
25 Correct 28 ms 51260 KB Output is correct
26 Correct 28 ms 51880 KB Output is correct
27 Correct 26 ms 51604 KB Output is correct
28 Correct 29 ms 52060 KB Output is correct
29 Correct 28 ms 52040 KB Output is correct
30 Correct 26 ms 51556 KB Output is correct
31 Correct 23 ms 51148 KB Output is correct
32 Correct 26 ms 51924 KB Output is correct
33 Correct 26 ms 51348 KB Output is correct
34 Correct 27 ms 51616 KB Output is correct
35 Correct 27 ms 52048 KB Output is correct
36 Correct 26 ms 51660 KB Output is correct
37 Correct 30 ms 51648 KB Output is correct
38 Correct 27 ms 51216 KB Output is correct
39 Correct 27 ms 51656 KB Output is correct
40 Correct 26 ms 51532 KB Output is correct
41 Correct 26 ms 51632 KB Output is correct
42 Correct 27 ms 51776 KB Output is correct
43 Correct 26 ms 51796 KB Output is correct
44 Correct 24 ms 51164 KB Output is correct
45 Correct 27 ms 51784 KB Output is correct
46 Correct 25 ms 51712 KB Output is correct
47 Correct 26 ms 51236 KB Output is correct
48 Correct 26 ms 51688 KB Output is correct
49 Correct 27 ms 52052 KB Output is correct
50 Correct 28 ms 52160 KB Output is correct
51 Correct 26 ms 51588 KB Output is correct
52 Correct 25 ms 51668 KB Output is correct
53 Correct 25 ms 51668 KB Output is correct
54 Correct 28 ms 51648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 51156 KB Output is correct
2 Correct 24 ms 51260 KB Output is correct
3 Correct 23 ms 51204 KB Output is correct
4 Correct 25 ms 51296 KB Output is correct
5 Correct 26 ms 51188 KB Output is correct
6 Correct 26 ms 51188 KB Output is correct
7 Correct 25 ms 51212 KB Output is correct
8 Correct 23 ms 51148 KB Output is correct
9 Correct 25 ms 51236 KB Output is correct
10 Correct 24 ms 51168 KB Output is correct
11 Correct 24 ms 51256 KB Output is correct
12 Correct 24 ms 51240 KB Output is correct
13 Correct 24 ms 51160 KB Output is correct
14 Correct 24 ms 51208 KB Output is correct
15 Correct 22 ms 51152 KB Output is correct
16 Correct 27 ms 51248 KB Output is correct
17 Correct 28 ms 51156 KB Output is correct
18 Correct 24 ms 51148 KB Output is correct
19 Correct 24 ms 51172 KB Output is correct
20 Correct 25 ms 51184 KB Output is correct
21 Correct 26 ms 51232 KB Output is correct
22 Correct 25 ms 51156 KB Output is correct
23 Correct 26 ms 51148 KB Output is correct
24 Correct 23 ms 51232 KB Output is correct
25 Correct 24 ms 51156 KB Output is correct
26 Correct 80 ms 65032 KB Output is correct
27 Correct 106 ms 64688 KB Output is correct
28 Correct 26 ms 51580 KB Output is correct
29 Correct 25 ms 51208 KB Output is correct
30 Correct 25 ms 51152 KB Output is correct
31 Correct 106 ms 64824 KB Output is correct
32 Correct 25 ms 51576 KB Output is correct
33 Correct 134 ms 71848 KB Output is correct
34 Correct 106 ms 64776 KB Output is correct
35 Correct 26 ms 51676 KB Output is correct
36 Correct 128 ms 65520 KB Output is correct
37 Correct 26 ms 51596 KB Output is correct
38 Correct 29 ms 51612 KB Output is correct
39 Correct 91 ms 65096 KB Output is correct
40 Correct 25 ms 51796 KB Output is correct
41 Correct 102 ms 64764 KB Output is correct
42 Correct 133 ms 67144 KB Output is correct
43 Correct 24 ms 51156 KB Output is correct
44 Correct 142 ms 72132 KB Output is correct
45 Correct 129 ms 68492 KB Output is correct
46 Correct 26 ms 51668 KB Output is correct
47 Correct 26 ms 51552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 65364 KB Output is correct
2 Correct 181 ms 80732 KB Output is correct
3 Correct 29 ms 51796 KB Output is correct
4 Correct 26 ms 51656 KB Output is correct
5 Correct 25 ms 51156 KB Output is correct
6 Correct 25 ms 51244 KB Output is correct
7 Correct 25 ms 51668 KB Output is correct
8 Correct 134 ms 67580 KB Output is correct
9 Correct 25 ms 51672 KB Output is correct
10 Correct 109 ms 65248 KB Output is correct
11 Correct 25 ms 51148 KB Output is correct
12 Correct 125 ms 65148 KB Output is correct
13 Correct 138 ms 68556 KB Output is correct
14 Correct 237 ms 80316 KB Output is correct
15 Correct 86 ms 65052 KB Output is correct
16 Correct 27 ms 51668 KB Output is correct
17 Correct 27 ms 51148 KB Output is correct
18 Correct 172 ms 77744 KB Output is correct
19 Correct 238 ms 84108 KB Output is correct
20 Correct 26 ms 51640 KB Output is correct
21 Correct 25 ms 51156 KB Output is correct
22 Correct 115 ms 68736 KB Output is correct
23 Correct 27 ms 51668 KB Output is correct
24 Correct 130 ms 66620 KB Output is correct
25 Correct 239 ms 82884 KB Output is correct
26 Correct 30 ms 52116 KB Output is correct
27 Correct 31 ms 52156 KB Output is correct
28 Correct 26 ms 51668 KB Output is correct
29 Correct 25 ms 51636 KB Output is correct
30 Correct 28 ms 51668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 51156 KB Output is correct
2 Correct 24 ms 51260 KB Output is correct
3 Correct 23 ms 51204 KB Output is correct
4 Correct 25 ms 51296 KB Output is correct
5 Correct 26 ms 51188 KB Output is correct
6 Correct 26 ms 51188 KB Output is correct
7 Correct 25 ms 51212 KB Output is correct
8 Correct 23 ms 51148 KB Output is correct
9 Correct 25 ms 51236 KB Output is correct
10 Correct 24 ms 51168 KB Output is correct
11 Correct 24 ms 51256 KB Output is correct
12 Correct 24 ms 51240 KB Output is correct
13 Correct 24 ms 51160 KB Output is correct
14 Correct 24 ms 51208 KB Output is correct
15 Correct 22 ms 51152 KB Output is correct
16 Correct 27 ms 51248 KB Output is correct
17 Correct 28 ms 51156 KB Output is correct
18 Correct 24 ms 51148 KB Output is correct
19 Correct 24 ms 51172 KB Output is correct
20 Correct 25 ms 51184 KB Output is correct
21 Correct 26 ms 51232 KB Output is correct
22 Correct 25 ms 51156 KB Output is correct
23 Correct 26 ms 51148 KB Output is correct
24 Correct 23 ms 51232 KB Output is correct
25 Correct 28 ms 51260 KB Output is correct
26 Correct 28 ms 51880 KB Output is correct
27 Correct 26 ms 51604 KB Output is correct
28 Correct 29 ms 52060 KB Output is correct
29 Correct 28 ms 52040 KB Output is correct
30 Correct 26 ms 51556 KB Output is correct
31 Correct 23 ms 51148 KB Output is correct
32 Correct 26 ms 51924 KB Output is correct
33 Correct 26 ms 51348 KB Output is correct
34 Correct 27 ms 51616 KB Output is correct
35 Correct 27 ms 52048 KB Output is correct
36 Correct 26 ms 51660 KB Output is correct
37 Correct 30 ms 51648 KB Output is correct
38 Correct 27 ms 51216 KB Output is correct
39 Correct 27 ms 51656 KB Output is correct
40 Correct 26 ms 51532 KB Output is correct
41 Correct 26 ms 51632 KB Output is correct
42 Correct 27 ms 51776 KB Output is correct
43 Correct 26 ms 51796 KB Output is correct
44 Correct 24 ms 51164 KB Output is correct
45 Correct 27 ms 51784 KB Output is correct
46 Correct 25 ms 51712 KB Output is correct
47 Correct 26 ms 51236 KB Output is correct
48 Correct 26 ms 51688 KB Output is correct
49 Correct 27 ms 52052 KB Output is correct
50 Correct 28 ms 52160 KB Output is correct
51 Correct 26 ms 51588 KB Output is correct
52 Correct 25 ms 51668 KB Output is correct
53 Correct 25 ms 51668 KB Output is correct
54 Correct 28 ms 51648 KB Output is correct
55 Correct 24 ms 51156 KB Output is correct
56 Correct 80 ms 65032 KB Output is correct
57 Correct 106 ms 64688 KB Output is correct
58 Correct 26 ms 51580 KB Output is correct
59 Correct 25 ms 51208 KB Output is correct
60 Correct 25 ms 51152 KB Output is correct
61 Correct 106 ms 64824 KB Output is correct
62 Correct 25 ms 51576 KB Output is correct
63 Correct 134 ms 71848 KB Output is correct
64 Correct 106 ms 64776 KB Output is correct
65 Correct 26 ms 51676 KB Output is correct
66 Correct 128 ms 65520 KB Output is correct
67 Correct 26 ms 51596 KB Output is correct
68 Correct 29 ms 51612 KB Output is correct
69 Correct 91 ms 65096 KB Output is correct
70 Correct 25 ms 51796 KB Output is correct
71 Correct 102 ms 64764 KB Output is correct
72 Correct 133 ms 67144 KB Output is correct
73 Correct 24 ms 51156 KB Output is correct
74 Correct 142 ms 72132 KB Output is correct
75 Correct 129 ms 68492 KB Output is correct
76 Correct 26 ms 51668 KB Output is correct
77 Correct 26 ms 51552 KB Output is correct
78 Correct 88 ms 65364 KB Output is correct
79 Correct 181 ms 80732 KB Output is correct
80 Correct 29 ms 51796 KB Output is correct
81 Correct 26 ms 51656 KB Output is correct
82 Correct 25 ms 51156 KB Output is correct
83 Correct 25 ms 51244 KB Output is correct
84 Correct 25 ms 51668 KB Output is correct
85 Correct 134 ms 67580 KB Output is correct
86 Correct 25 ms 51672 KB Output is correct
87 Correct 109 ms 65248 KB Output is correct
88 Correct 25 ms 51148 KB Output is correct
89 Correct 125 ms 65148 KB Output is correct
90 Correct 138 ms 68556 KB Output is correct
91 Correct 237 ms 80316 KB Output is correct
92 Correct 86 ms 65052 KB Output is correct
93 Correct 27 ms 51668 KB Output is correct
94 Correct 27 ms 51148 KB Output is correct
95 Correct 172 ms 77744 KB Output is correct
96 Correct 238 ms 84108 KB Output is correct
97 Correct 26 ms 51640 KB Output is correct
98 Correct 25 ms 51156 KB Output is correct
99 Correct 115 ms 68736 KB Output is correct
100 Correct 27 ms 51668 KB Output is correct
101 Correct 130 ms 66620 KB Output is correct
102 Correct 239 ms 82884 KB Output is correct
103 Correct 30 ms 52116 KB Output is correct
104 Correct 31 ms 52156 KB Output is correct
105 Correct 26 ms 51668 KB Output is correct
106 Correct 25 ms 51636 KB Output is correct
107 Correct 28 ms 51668 KB Output is correct
108 Correct 586 ms 132040 KB Output is correct
109 Correct 921 ms 136704 KB Output is correct
110 Correct 895 ms 147928 KB Output is correct
111 Correct 1856 ms 223408 KB Output is correct
112 Correct 1331 ms 180980 KB Output is correct
113 Correct 1118 ms 184552 KB Output is correct
114 Correct 615 ms 118416 KB Output is correct
115 Correct 619 ms 118456 KB Output is correct
116 Correct 789 ms 123264 KB Output is correct
117 Correct 1707 ms 196728 KB Output is correct
118 Correct 664 ms 119704 KB Output is correct
119 Correct 1731 ms 196584 KB Output is correct
120 Correct 1920 ms 220408 KB Output is correct
121 Correct 1770 ms 206232 KB Output is correct
122 Correct 923 ms 148448 KB Output is correct
123 Correct 1261 ms 208048 KB Output is correct
124 Correct 1429 ms 181964 KB Output is correct