답안 #923567

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
923567 2024-02-07T12:19:52 Z danikoynov Mergers (JOI19_mergers) C++14
100 / 100
1168 ms 228776 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}


const int maxn = 5e5 + 10;
int n, k, state[maxn];
vector < int > adj[maxn], group[maxn];
void input()
{
    cin >> n >> k;
    for (int i = 1; i < n; i ++)
    {
        int v, u;
        cin >> v >> u;
        adj[v].push_back(u);
        adj[u].push_back(v);
    }
    for (int i = 1; i <= n; i ++)
    {
        cin >> state[i];
        group[state[i]].push_back(i);
    }
}

int tin[maxn], tout[maxn], timer;
int occ[2 * maxn], depth[maxn], par[maxn];
void dfs(int v, int p)
{
    tin[v] = ++ timer;
    occ[timer] = v;
    par[v] = p;
    for (int u : adj[v])
    {
        if (u == p)
            continue;

        depth[u] = depth[v] + 1;
        dfs(u, v);
        occ[++ timer] = v;
    }

    tout[v] = timer;
}

const int maxlog = 22;

int dp[maxlog][maxn * 2], lg[maxn * 2];
void build_sparse_table()
{
    for (int i = 1; i <= timer; i ++)
    {
        lg[i] = lg[i / 2] + 1;
        dp[0][i] = occ[i];
    }

    for (int j = 1; j < lg[timer]; j ++)
        for (int i = 1; i <= timer - (1 << j) + 1; i ++)
    {
        dp[j][i] = dp[j - 1][i + (1 << (j - 1))];
        if (depth[dp[j - 1][i]] < depth[dp[j][i]])
            dp[j][i] = dp[j - 1][i];
    }
}

int get_lca(int v, int u)
{
    v = tin[v];
    u = tin[u];
    if (v > u)
        swap(v, u);
    int len = lg[u - v + 1] - 1;
    int res = dp[len][u - (1 << len) + 1];
    if (depth[dp[len][v]] < depth[res])
        res = dp[len][v];

    return res;
}

int mark[maxn], add[maxn], rem[maxn];
void mark_path(int v, int u)
{
    int lca = get_lca(v, u);
    add[v] ++;
    add[u] ++;
    rem[lca] += 2;

}

int mark_dfs(int v, int p)
{
    int s = add[v] - rem[v];
    for (int u : adj[v])
    {
        if (u ==p)
            continue;
        s += mark_dfs(u, v);
    }
    if (s > 0)
        mark[v] = 1;
    return s;
}
void find_redundant()
{
    for (int d = 1; d <= k; d ++)
    {
        for (int i = 0; i < group[d].size(); i ++)
        {
            int bef = i - 1;
            if (bef < 0)
                bef = group[d].size() - 1;
            mark_path(group[d][bef], group[d][i]);
        }
    }

    mark_dfs(1, -1);
}

vector < int > g[maxn];
int used[maxn];
int cp_cnt;

void trav(int v)
{
    used[v] = cp_cnt;
    for (int u : g[v])
    {
        if (used[u] != 0)
            continue;
        trav(u);
    }
}

vector < int > bridge[maxn];
void build_new_graph()
{
    /// exclude 1
    for (int i = 2; i <= n; i ++)
    {
        if (mark[i])
        {
            g[i].push_back(par[i]);
            g[par[i]].push_back(i);
        }
    }

    //for (int i = 1; i <= n; i ++)
        //cout << "mark " << i << " " << mark[i] << endl;
    for (int i = 1; i <= n; i ++)
    {
        if (!used[i])
        {
            cp_cnt ++;
            trav(i);
        }
    }

    ///for (int i = 1; i <= n; i ++)
        //cout << i << " " << used[i] << endl;

    for (int i = 2; i <= n; i ++)
    {
        if (!mark[i])
        {
            assert(used[par[i]] != used[i]);
            bridge[used[i]].push_back(used[par[i]]);
            bridge[used[par[i]]].push_back(used[i]);
        }
    }
}

int find_leaves(int v, int p)
{
    ///cout << v << " : " << p << endl;
    int child = 0, leaves = 0;
    used[v] = 1;
    for (int u : bridge[v])
    {
        if (used[u] == 1)
            continue;
        child ++;
        leaves += find_leaves(u, v);
    }
    if ((p == -1 && child < 2) || child == 0)
        leaves ++;
    return leaves;
}

void print()
{
    memset(used, 0, sizeof(used));
    int res = find_leaves(1, - 1);
    if (res == 1)
        cout << 0 << endl;
    else
        cout << (res + 1) / 2 << endl;
}
void solve()
{
    input();
    dfs(1, -1);
    build_sparse_table();
    find_redundant();
    build_new_graph();
    print();

}

int main()
{
    solve();
    return 0;
}

/**
10 5
1 3
9 6
8 6
3 10
3 2
1 6
8 7
2 5
9 4
1
1
3
2
2
5
3
1
4
2

*/

Compilation message

mergers.cpp: In function 'void find_redundant()':
mergers.cpp:124:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |         for (int i = 0; i < group[d].size(); i ++)
      |                         ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 72280 KB Output is correct
2 Correct 17 ms 80472 KB Output is correct
3 Correct 16 ms 80472 KB Output is correct
4 Correct 15 ms 74508 KB Output is correct
5 Correct 15 ms 72284 KB Output is correct
6 Correct 15 ms 80476 KB Output is correct
7 Correct 14 ms 64088 KB Output is correct
8 Correct 15 ms 80476 KB Output is correct
9 Correct 15 ms 74504 KB Output is correct
10 Correct 14 ms 70232 KB Output is correct
11 Correct 16 ms 80476 KB Output is correct
12 Correct 15 ms 80476 KB Output is correct
13 Correct 15 ms 74332 KB Output is correct
14 Correct 16 ms 80476 KB Output is correct
15 Correct 15 ms 80476 KB Output is correct
16 Correct 15 ms 70236 KB Output is correct
17 Correct 15 ms 72280 KB Output is correct
18 Correct 16 ms 80472 KB Output is correct
19 Correct 14 ms 70236 KB Output is correct
20 Correct 16 ms 70236 KB Output is correct
21 Correct 14 ms 80660 KB Output is correct
22 Correct 16 ms 80728 KB Output is correct
23 Correct 14 ms 74332 KB Output is correct
24 Correct 16 ms 74332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 72280 KB Output is correct
2 Correct 17 ms 80472 KB Output is correct
3 Correct 16 ms 80472 KB Output is correct
4 Correct 15 ms 74508 KB Output is correct
5 Correct 15 ms 72284 KB Output is correct
6 Correct 15 ms 80476 KB Output is correct
7 Correct 14 ms 64088 KB Output is correct
8 Correct 15 ms 80476 KB Output is correct
9 Correct 15 ms 74504 KB Output is correct
10 Correct 14 ms 70232 KB Output is correct
11 Correct 16 ms 80476 KB Output is correct
12 Correct 15 ms 80476 KB Output is correct
13 Correct 15 ms 74332 KB Output is correct
14 Correct 16 ms 80476 KB Output is correct
15 Correct 15 ms 80476 KB Output is correct
16 Correct 15 ms 70236 KB Output is correct
17 Correct 15 ms 72280 KB Output is correct
18 Correct 16 ms 80472 KB Output is correct
19 Correct 14 ms 70236 KB Output is correct
20 Correct 16 ms 70236 KB Output is correct
21 Correct 14 ms 80660 KB Output is correct
22 Correct 16 ms 80728 KB Output is correct
23 Correct 14 ms 74332 KB Output is correct
24 Correct 16 ms 74332 KB Output is correct
25 Correct 15 ms 78428 KB Output is correct
26 Correct 20 ms 93272 KB Output is correct
27 Correct 19 ms 93020 KB Output is correct
28 Correct 20 ms 93276 KB Output is correct
29 Correct 18 ms 93080 KB Output is correct
30 Correct 19 ms 93016 KB Output is correct
31 Correct 16 ms 78428 KB Output is correct
32 Correct 19 ms 93416 KB Output is correct
33 Correct 15 ms 78428 KB Output is correct
34 Correct 19 ms 93216 KB Output is correct
35 Correct 19 ms 91224 KB Output is correct
36 Correct 19 ms 93088 KB Output is correct
37 Correct 19 ms 93020 KB Output is correct
38 Correct 17 ms 80472 KB Output is correct
39 Correct 19 ms 93020 KB Output is correct
40 Correct 19 ms 93016 KB Output is correct
41 Correct 20 ms 93024 KB Output is correct
42 Correct 18 ms 93276 KB Output is correct
43 Correct 19 ms 93272 KB Output is correct
44 Correct 17 ms 80476 KB Output is correct
45 Correct 20 ms 93276 KB Output is correct
46 Correct 20 ms 93240 KB Output is correct
47 Correct 15 ms 80476 KB Output is correct
48 Correct 20 ms 93020 KB Output is correct
49 Correct 19 ms 91228 KB Output is correct
50 Correct 19 ms 91228 KB Output is correct
51 Correct 19 ms 93016 KB Output is correct
52 Correct 19 ms 93236 KB Output is correct
53 Correct 19 ms 93132 KB Output is correct
54 Correct 19 ms 93020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 72280 KB Output is correct
2 Correct 17 ms 80472 KB Output is correct
3 Correct 16 ms 80472 KB Output is correct
4 Correct 15 ms 74508 KB Output is correct
5 Correct 15 ms 72284 KB Output is correct
6 Correct 15 ms 80476 KB Output is correct
7 Correct 14 ms 64088 KB Output is correct
8 Correct 15 ms 80476 KB Output is correct
9 Correct 15 ms 74504 KB Output is correct
10 Correct 14 ms 70232 KB Output is correct
11 Correct 16 ms 80476 KB Output is correct
12 Correct 15 ms 80476 KB Output is correct
13 Correct 15 ms 74332 KB Output is correct
14 Correct 16 ms 80476 KB Output is correct
15 Correct 15 ms 80476 KB Output is correct
16 Correct 15 ms 70236 KB Output is correct
17 Correct 15 ms 72280 KB Output is correct
18 Correct 16 ms 80472 KB Output is correct
19 Correct 14 ms 70236 KB Output is correct
20 Correct 16 ms 70236 KB Output is correct
21 Correct 14 ms 80660 KB Output is correct
22 Correct 16 ms 80728 KB Output is correct
23 Correct 14 ms 74332 KB Output is correct
24 Correct 16 ms 74332 KB Output is correct
25 Correct 15 ms 78428 KB Output is correct
26 Correct 104 ms 128124 KB Output is correct
27 Correct 130 ms 127508 KB Output is correct
28 Correct 19 ms 93020 KB Output is correct
29 Correct 15 ms 78428 KB Output is correct
30 Correct 14 ms 78428 KB Output is correct
31 Correct 135 ms 127460 KB Output is correct
32 Correct 18 ms 93016 KB Output is correct
33 Correct 130 ms 132980 KB Output is correct
34 Correct 132 ms 128976 KB Output is correct
35 Correct 19 ms 93276 KB Output is correct
36 Correct 129 ms 129460 KB Output is correct
37 Correct 19 ms 93016 KB Output is correct
38 Correct 19 ms 93016 KB Output is correct
39 Correct 98 ms 129356 KB Output is correct
40 Correct 19 ms 93272 KB Output is correct
41 Correct 121 ms 128720 KB Output is correct
42 Correct 133 ms 130896 KB Output is correct
43 Correct 16 ms 80476 KB Output is correct
44 Correct 130 ms 134744 KB Output is correct
45 Correct 137 ms 131664 KB Output is correct
46 Correct 19 ms 93160 KB Output is correct
47 Correct 19 ms 93016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 128096 KB Output is correct
2 Correct 124 ms 129192 KB Output is correct
3 Correct 20 ms 93276 KB Output is correct
4 Correct 20 ms 93020 KB Output is correct
5 Correct 15 ms 78428 KB Output is correct
6 Correct 15 ms 78428 KB Output is correct
7 Correct 21 ms 93020 KB Output is correct
8 Correct 144 ms 128728 KB Output is correct
9 Correct 19 ms 93016 KB Output is correct
10 Correct 143 ms 127724 KB Output is correct
11 Correct 15 ms 80472 KB Output is correct
12 Correct 128 ms 127312 KB Output is correct
13 Correct 150 ms 128852 KB Output is correct
14 Correct 155 ms 128352 KB Output is correct
15 Correct 100 ms 127972 KB Output is correct
16 Correct 19 ms 93272 KB Output is correct
17 Correct 14 ms 80476 KB Output is correct
18 Correct 122 ms 130400 KB Output is correct
19 Correct 171 ms 132200 KB Output is correct
20 Correct 20 ms 93016 KB Output is correct
21 Correct 14 ms 80476 KB Output is correct
22 Correct 120 ms 129220 KB Output is correct
23 Correct 19 ms 93016 KB Output is correct
24 Correct 146 ms 127908 KB Output is correct
25 Correct 143 ms 130628 KB Output is correct
26 Correct 19 ms 91128 KB Output is correct
27 Correct 18 ms 91228 KB Output is correct
28 Correct 20 ms 93228 KB Output is correct
29 Correct 18 ms 93020 KB Output is correct
30 Correct 20 ms 93020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 72280 KB Output is correct
2 Correct 17 ms 80472 KB Output is correct
3 Correct 16 ms 80472 KB Output is correct
4 Correct 15 ms 74508 KB Output is correct
5 Correct 15 ms 72284 KB Output is correct
6 Correct 15 ms 80476 KB Output is correct
7 Correct 14 ms 64088 KB Output is correct
8 Correct 15 ms 80476 KB Output is correct
9 Correct 15 ms 74504 KB Output is correct
10 Correct 14 ms 70232 KB Output is correct
11 Correct 16 ms 80476 KB Output is correct
12 Correct 15 ms 80476 KB Output is correct
13 Correct 15 ms 74332 KB Output is correct
14 Correct 16 ms 80476 KB Output is correct
15 Correct 15 ms 80476 KB Output is correct
16 Correct 15 ms 70236 KB Output is correct
17 Correct 15 ms 72280 KB Output is correct
18 Correct 16 ms 80472 KB Output is correct
19 Correct 14 ms 70236 KB Output is correct
20 Correct 16 ms 70236 KB Output is correct
21 Correct 14 ms 80660 KB Output is correct
22 Correct 16 ms 80728 KB Output is correct
23 Correct 14 ms 74332 KB Output is correct
24 Correct 16 ms 74332 KB Output is correct
25 Correct 15 ms 78428 KB Output is correct
26 Correct 20 ms 93272 KB Output is correct
27 Correct 19 ms 93020 KB Output is correct
28 Correct 20 ms 93276 KB Output is correct
29 Correct 18 ms 93080 KB Output is correct
30 Correct 19 ms 93016 KB Output is correct
31 Correct 16 ms 78428 KB Output is correct
32 Correct 19 ms 93416 KB Output is correct
33 Correct 15 ms 78428 KB Output is correct
34 Correct 19 ms 93216 KB Output is correct
35 Correct 19 ms 91224 KB Output is correct
36 Correct 19 ms 93088 KB Output is correct
37 Correct 19 ms 93020 KB Output is correct
38 Correct 17 ms 80472 KB Output is correct
39 Correct 19 ms 93020 KB Output is correct
40 Correct 19 ms 93016 KB Output is correct
41 Correct 20 ms 93024 KB Output is correct
42 Correct 18 ms 93276 KB Output is correct
43 Correct 19 ms 93272 KB Output is correct
44 Correct 17 ms 80476 KB Output is correct
45 Correct 20 ms 93276 KB Output is correct
46 Correct 20 ms 93240 KB Output is correct
47 Correct 15 ms 80476 KB Output is correct
48 Correct 20 ms 93020 KB Output is correct
49 Correct 19 ms 91228 KB Output is correct
50 Correct 19 ms 91228 KB Output is correct
51 Correct 19 ms 93016 KB Output is correct
52 Correct 19 ms 93236 KB Output is correct
53 Correct 19 ms 93132 KB Output is correct
54 Correct 19 ms 93020 KB Output is correct
55 Correct 15 ms 78428 KB Output is correct
56 Correct 104 ms 128124 KB Output is correct
57 Correct 130 ms 127508 KB Output is correct
58 Correct 19 ms 93020 KB Output is correct
59 Correct 15 ms 78428 KB Output is correct
60 Correct 14 ms 78428 KB Output is correct
61 Correct 135 ms 127460 KB Output is correct
62 Correct 18 ms 93016 KB Output is correct
63 Correct 130 ms 132980 KB Output is correct
64 Correct 132 ms 128976 KB Output is correct
65 Correct 19 ms 93276 KB Output is correct
66 Correct 129 ms 129460 KB Output is correct
67 Correct 19 ms 93016 KB Output is correct
68 Correct 19 ms 93016 KB Output is correct
69 Correct 98 ms 129356 KB Output is correct
70 Correct 19 ms 93272 KB Output is correct
71 Correct 121 ms 128720 KB Output is correct
72 Correct 133 ms 130896 KB Output is correct
73 Correct 16 ms 80476 KB Output is correct
74 Correct 130 ms 134744 KB Output is correct
75 Correct 137 ms 131664 KB Output is correct
76 Correct 19 ms 93160 KB Output is correct
77 Correct 19 ms 93016 KB Output is correct
78 Correct 100 ms 128096 KB Output is correct
79 Correct 124 ms 129192 KB Output is correct
80 Correct 20 ms 93276 KB Output is correct
81 Correct 20 ms 93020 KB Output is correct
82 Correct 15 ms 78428 KB Output is correct
83 Correct 15 ms 78428 KB Output is correct
84 Correct 21 ms 93020 KB Output is correct
85 Correct 144 ms 128728 KB Output is correct
86 Correct 19 ms 93016 KB Output is correct
87 Correct 143 ms 127724 KB Output is correct
88 Correct 15 ms 80472 KB Output is correct
89 Correct 128 ms 127312 KB Output is correct
90 Correct 150 ms 128852 KB Output is correct
91 Correct 155 ms 128352 KB Output is correct
92 Correct 100 ms 127972 KB Output is correct
93 Correct 19 ms 93272 KB Output is correct
94 Correct 14 ms 80476 KB Output is correct
95 Correct 122 ms 130400 KB Output is correct
96 Correct 171 ms 132200 KB Output is correct
97 Correct 20 ms 93016 KB Output is correct
98 Correct 14 ms 80476 KB Output is correct
99 Correct 120 ms 129220 KB Output is correct
100 Correct 19 ms 93016 KB Output is correct
101 Correct 146 ms 127908 KB Output is correct
102 Correct 143 ms 130628 KB Output is correct
103 Correct 19 ms 91128 KB Output is correct
104 Correct 18 ms 91228 KB Output is correct
105 Correct 20 ms 93228 KB Output is correct
106 Correct 18 ms 93020 KB Output is correct
107 Correct 20 ms 93020 KB Output is correct
108 Correct 636 ms 202256 KB Output is correct
109 Correct 1016 ms 206580 KB Output is correct
110 Correct 1063 ms 215464 KB Output is correct
111 Correct 1168 ms 228776 KB Output is correct
112 Correct 1032 ms 219792 KB Output is correct
113 Correct 718 ms 209956 KB Output is correct
114 Correct 839 ms 192940 KB Output is correct
115 Correct 831 ms 192992 KB Output is correct
116 Correct 926 ms 197036 KB Output is correct
117 Correct 956 ms 208528 KB Output is correct
118 Correct 865 ms 193872 KB Output is correct
119 Correct 945 ms 208364 KB Output is correct
120 Correct 1071 ms 221780 KB Output is correct
121 Correct 1056 ms 208956 KB Output is correct
122 Correct 948 ms 202584 KB Output is correct
123 Correct 647 ms 211132 KB Output is correct
124 Correct 1045 ms 206504 KB Output is correct