Submission #923563

# Submission time Handle Problem Language Result Execution time Memory
923563 2024-02-07T12:19:09 Z danikoynov Mergers (JOI19_mergers) C++14
56 / 100
3000 ms 133616 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;
    while(v != lca)
    {
        mark[v] = 1;
        v = par[v];
    }
    while(u != lca)
    {
        mark[u] = 1;
        u = par[u];
    }
}

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:133:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |         for (int i = 0; i < group[d].size(); i ++)
      |                         ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 72280 KB Output is correct
2 Correct 21 ms 80632 KB Output is correct
3 Correct 16 ms 80632 KB Output is correct
4 Correct 15 ms 74328 KB Output is correct
5 Correct 14 ms 72284 KB Output is correct
6 Correct 16 ms 80664 KB Output is correct
7 Correct 14 ms 64096 KB Output is correct
8 Correct 15 ms 80476 KB Output is correct
9 Correct 14 ms 74332 KB Output is correct
10 Correct 15 ms 70232 KB Output is correct
11 Correct 15 ms 80476 KB Output is correct
12 Correct 16 ms 80476 KB Output is correct
13 Correct 14 ms 74332 KB Output is correct
14 Correct 16 ms 80672 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 72432 KB Output is correct
18 Correct 16 ms 80476 KB Output is correct
19 Correct 15 ms 70236 KB Output is correct
20 Correct 17 ms 70236 KB Output is correct
21 Correct 15 ms 80484 KB Output is correct
22 Correct 16 ms 80476 KB Output is correct
23 Correct 15 ms 74332 KB Output is correct
24 Correct 14 ms 74332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 72280 KB Output is correct
2 Correct 21 ms 80632 KB Output is correct
3 Correct 16 ms 80632 KB Output is correct
4 Correct 15 ms 74328 KB Output is correct
5 Correct 14 ms 72284 KB Output is correct
6 Correct 16 ms 80664 KB Output is correct
7 Correct 14 ms 64096 KB Output is correct
8 Correct 15 ms 80476 KB Output is correct
9 Correct 14 ms 74332 KB Output is correct
10 Correct 15 ms 70232 KB Output is correct
11 Correct 15 ms 80476 KB Output is correct
12 Correct 16 ms 80476 KB Output is correct
13 Correct 14 ms 74332 KB Output is correct
14 Correct 16 ms 80672 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 72432 KB Output is correct
18 Correct 16 ms 80476 KB Output is correct
19 Correct 15 ms 70236 KB Output is correct
20 Correct 17 ms 70236 KB Output is correct
21 Correct 15 ms 80484 KB Output is correct
22 Correct 16 ms 80476 KB Output is correct
23 Correct 15 ms 74332 KB Output is correct
24 Correct 14 ms 74332 KB Output is correct
25 Correct 16 ms 78428 KB Output is correct
26 Correct 21 ms 93044 KB Output is correct
27 Correct 19 ms 93020 KB Output is correct
28 Correct 22 ms 93344 KB Output is correct
29 Correct 20 ms 93244 KB Output is correct
30 Correct 19 ms 93164 KB Output is correct
31 Correct 16 ms 78428 KB Output is correct
32 Correct 20 ms 93276 KB Output is correct
33 Correct 15 ms 78612 KB Output is correct
34 Correct 19 ms 93020 KB Output is correct
35 Correct 20 ms 91116 KB Output is correct
36 Correct 19 ms 93192 KB Output is correct
37 Correct 20 ms 93020 KB Output is correct
38 Correct 17 ms 80472 KB Output is correct
39 Correct 19 ms 93016 KB Output is correct
40 Correct 19 ms 93016 KB Output is correct
41 Correct 18 ms 93020 KB Output is correct
42 Correct 21 ms 93152 KB Output is correct
43 Correct 25 ms 93276 KB Output is correct
44 Correct 17 ms 80560 KB Output is correct
45 Correct 20 ms 93136 KB Output is correct
46 Correct 20 ms 93040 KB Output is correct
47 Correct 15 ms 80612 KB Output is correct
48 Correct 23 ms 93160 KB Output is correct
49 Correct 22 ms 91228 KB Output is correct
50 Correct 21 ms 91228 KB Output is correct
51 Correct 21 ms 93020 KB Output is correct
52 Correct 19 ms 93020 KB Output is correct
53 Correct 19 ms 93020 KB Output is correct
54 Correct 21 ms 93176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 72280 KB Output is correct
2 Correct 21 ms 80632 KB Output is correct
3 Correct 16 ms 80632 KB Output is correct
4 Correct 15 ms 74328 KB Output is correct
5 Correct 14 ms 72284 KB Output is correct
6 Correct 16 ms 80664 KB Output is correct
7 Correct 14 ms 64096 KB Output is correct
8 Correct 15 ms 80476 KB Output is correct
9 Correct 14 ms 74332 KB Output is correct
10 Correct 15 ms 70232 KB Output is correct
11 Correct 15 ms 80476 KB Output is correct
12 Correct 16 ms 80476 KB Output is correct
13 Correct 14 ms 74332 KB Output is correct
14 Correct 16 ms 80672 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 72432 KB Output is correct
18 Correct 16 ms 80476 KB Output is correct
19 Correct 15 ms 70236 KB Output is correct
20 Correct 17 ms 70236 KB Output is correct
21 Correct 15 ms 80484 KB Output is correct
22 Correct 16 ms 80476 KB Output is correct
23 Correct 15 ms 74332 KB Output is correct
24 Correct 14 ms 74332 KB Output is correct
25 Correct 16 ms 78428 KB Output is correct
26 Correct 114 ms 129416 KB Output is correct
27 Correct 142 ms 128852 KB Output is correct
28 Correct 21 ms 93124 KB Output is correct
29 Correct 17 ms 78540 KB Output is correct
30 Correct 17 ms 78428 KB Output is correct
31 Correct 142 ms 129008 KB Output is correct
32 Correct 19 ms 93276 KB Output is correct
33 Execution timed out 3089 ms 131456 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 104 ms 129476 KB Output is correct
2 Correct 127 ms 130460 KB Output is correct
3 Correct 20 ms 93272 KB Output is correct
4 Correct 19 ms 93020 KB Output is correct
5 Correct 15 ms 78424 KB Output is correct
6 Correct 17 ms 78428 KB Output is correct
7 Correct 19 ms 93272 KB Output is correct
8 Correct 137 ms 130388 KB Output is correct
9 Correct 21 ms 93144 KB Output is correct
10 Correct 124 ms 129216 KB Output is correct
11 Correct 16 ms 80476 KB Output is correct
12 Correct 148 ms 129052 KB Output is correct
13 Correct 136 ms 130476 KB Output is correct
14 Correct 137 ms 129876 KB Output is correct
15 Correct 101 ms 129428 KB Output is correct
16 Correct 19 ms 93272 KB Output is correct
17 Correct 16 ms 80476 KB Output is correct
18 Correct 116 ms 132296 KB Output is correct
19 Correct 142 ms 133616 KB Output is correct
20 Correct 20 ms 93140 KB Output is correct
21 Correct 14 ms 80476 KB Output is correct
22 Correct 112 ms 130776 KB Output is correct
23 Correct 19 ms 93020 KB Output is correct
24 Correct 135 ms 129700 KB Output is correct
25 Correct 140 ms 132272 KB Output is correct
26 Correct 20 ms 91180 KB Output is correct
27 Correct 19 ms 91224 KB Output is correct
28 Correct 19 ms 93020 KB Output is correct
29 Correct 19 ms 93276 KB Output is correct
30 Correct 19 ms 93276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 72280 KB Output is correct
2 Correct 21 ms 80632 KB Output is correct
3 Correct 16 ms 80632 KB Output is correct
4 Correct 15 ms 74328 KB Output is correct
5 Correct 14 ms 72284 KB Output is correct
6 Correct 16 ms 80664 KB Output is correct
7 Correct 14 ms 64096 KB Output is correct
8 Correct 15 ms 80476 KB Output is correct
9 Correct 14 ms 74332 KB Output is correct
10 Correct 15 ms 70232 KB Output is correct
11 Correct 15 ms 80476 KB Output is correct
12 Correct 16 ms 80476 KB Output is correct
13 Correct 14 ms 74332 KB Output is correct
14 Correct 16 ms 80672 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 72432 KB Output is correct
18 Correct 16 ms 80476 KB Output is correct
19 Correct 15 ms 70236 KB Output is correct
20 Correct 17 ms 70236 KB Output is correct
21 Correct 15 ms 80484 KB Output is correct
22 Correct 16 ms 80476 KB Output is correct
23 Correct 15 ms 74332 KB Output is correct
24 Correct 14 ms 74332 KB Output is correct
25 Correct 16 ms 78428 KB Output is correct
26 Correct 21 ms 93044 KB Output is correct
27 Correct 19 ms 93020 KB Output is correct
28 Correct 22 ms 93344 KB Output is correct
29 Correct 20 ms 93244 KB Output is correct
30 Correct 19 ms 93164 KB Output is correct
31 Correct 16 ms 78428 KB Output is correct
32 Correct 20 ms 93276 KB Output is correct
33 Correct 15 ms 78612 KB Output is correct
34 Correct 19 ms 93020 KB Output is correct
35 Correct 20 ms 91116 KB Output is correct
36 Correct 19 ms 93192 KB Output is correct
37 Correct 20 ms 93020 KB Output is correct
38 Correct 17 ms 80472 KB Output is correct
39 Correct 19 ms 93016 KB Output is correct
40 Correct 19 ms 93016 KB Output is correct
41 Correct 18 ms 93020 KB Output is correct
42 Correct 21 ms 93152 KB Output is correct
43 Correct 25 ms 93276 KB Output is correct
44 Correct 17 ms 80560 KB Output is correct
45 Correct 20 ms 93136 KB Output is correct
46 Correct 20 ms 93040 KB Output is correct
47 Correct 15 ms 80612 KB Output is correct
48 Correct 23 ms 93160 KB Output is correct
49 Correct 22 ms 91228 KB Output is correct
50 Correct 21 ms 91228 KB Output is correct
51 Correct 21 ms 93020 KB Output is correct
52 Correct 19 ms 93020 KB Output is correct
53 Correct 19 ms 93020 KB Output is correct
54 Correct 21 ms 93176 KB Output is correct
55 Correct 16 ms 78428 KB Output is correct
56 Correct 114 ms 129416 KB Output is correct
57 Correct 142 ms 128852 KB Output is correct
58 Correct 21 ms 93124 KB Output is correct
59 Correct 17 ms 78540 KB Output is correct
60 Correct 17 ms 78428 KB Output is correct
61 Correct 142 ms 129008 KB Output is correct
62 Correct 19 ms 93276 KB Output is correct
63 Execution timed out 3089 ms 131456 KB Time limit exceeded
64 Halted 0 ms 0 KB -