Submission #676892

# Submission time Handle Problem Language Result Execution time Memory
676892 2023-01-01T13:20:55 Z Tien_Noob Mergers (JOI19_mergers) C++17
100 / 100
1052 ms 135540 KB
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 5e5, cbit = 18;
vector<int> num[N + 1], adj[N + 1];
int n, k, tin[N + 1], tout[N + 1], depth[N + 1], dp[N + 1], timer, res, cnt[N + 1], f[N + 1][cbit + 1];
void read()
{
    cin >> n >> k;
    for (int i = 1; i < n; ++ i)
    {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int u = 1; u <= n; ++ u)
    {
        int a;
        cin >> a;
        num[a].push_back(u);
    }
}
void preDFS(int u, int p)
{
    tin[u] = ++timer;
    for (int v : adj[u])
    {
        if (v == p)
        {
            continue;
        }
        depth[v] = depth[u] + 1;
        f[v][0] = u;
        for (int i = 1; i <= cbit; ++ i)
        {
            f[v][i] = f[f[v][i - 1]][i - 1];
            if (!f[v][i])
            {
                break;
            }
        }
        preDFS(v, u);
    }
    tout[u] = timer;
}
int LCA(int u, int v)
{
    if (depth[u] > depth[v])
    {
        swap(u, v);
    }
    int k = depth[v] - depth[u];
    for (int i = cbit; i >= 0; -- i)
    {
        if ((k >> i) & 1)
        {
            v = f[v][i];
        }
    }
    if (u == v)
    {
        return u;
    }
    for (int i = cbit; i >= 0; -- i)
    {
        if (f[u][i] != f[v][i])
        {
            u = f[u][i];
            v = f[v][i];
        }
    }
    return f[u][0];
}
void DFS_down(int u, int p)
{
    for (int v : adj[u])
    {
        if (v == p)
        {
            continue;
        }
        DFS_down(v, u);
        dp[u] += dp[v];
    }
}
void DFS(int u, int p, int last_zero)
{
    if (dp[u] == 0)
    {
        if (last_zero != -1)
        {
            ++cnt[last_zero];
            ++cnt[u];
        }
        last_zero = u;
    }
    for (int v : adj[u])
    {
        if (v == p)
        {
            continue;
        }
        DFS(v, u, last_zero);
    }
}
void solve()
{
    preDFS(1, 0);
    for (int i = 1; i <= k; ++ i)
    {
        sort(num[i].begin(), num[i].end(), [&](const int &x, const int &y)
             {
                 return tin[x] < tin[y];
             });
        vector<int> add, tmp;
        stack<int> st;
        for (int j = 1; j < num[i].size(); ++ j)
        {
            add.push_back(LCA(num[i][j], num[i][j - 1]));
        }
        move(add.begin(), add.end(), back_inserter(num[i]));
        sort(num[i].begin(), num[i].end(), [&](const int &x, const int &y)
             {
                 return tin[x] < tin[y];
             });
        num[i].erase(unique(num[i].begin(), num[i].end()), num[i].end());
        for (int u : num[i])
        {
            while(!st.empty() && (tin[u] < tin[st.top()] || tin[u] > tout[st.top()]))
            {
                st.pop();
            }
            if (!st.empty())
            {
                ++dp[st.top()];
                --dp[u];
            }
            st.push(u);
        }
    }
    DFS_down(1, 0);
    DFS(1, 0, -1);
    for (int u = 1; u <= n; ++ u)
    {
        res += (cnt[u] == 1);
    }
    cout << (res + 1)/2;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        //freopen(TASK".OUT", "w", stdout);
    }
    int t = 1;
    bool typetest = false;
    if (typetest)
    {
        cin >> t;
    }
    for (int __ = 1; __ <= t; ++ __)
    {
        //cout << "Case #" << __ << ":" << '\n';
        read();
        solve();
    }
}

Compilation message

mergers.cpp: In function 'void solve()':
mergers.cpp:121:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |         for (int j = 1; j < num[i].size(); ++ j)
      |                         ~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:159:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23812 KB Output is correct
3 Correct 17 ms 23816 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 12 ms 23740 KB Output is correct
7 Correct 12 ms 23828 KB Output is correct
8 Correct 11 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 13 ms 23800 KB Output is correct
11 Correct 12 ms 23824 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 14 ms 23812 KB Output is correct
14 Correct 13 ms 23752 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23744 KB Output is correct
18 Correct 12 ms 23848 KB Output is correct
19 Correct 11 ms 23764 KB Output is correct
20 Correct 11 ms 23748 KB Output is correct
21 Correct 12 ms 23828 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23764 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23812 KB Output is correct
3 Correct 17 ms 23816 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 12 ms 23740 KB Output is correct
7 Correct 12 ms 23828 KB Output is correct
8 Correct 11 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 13 ms 23800 KB Output is correct
11 Correct 12 ms 23824 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 14 ms 23812 KB Output is correct
14 Correct 13 ms 23752 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23744 KB Output is correct
18 Correct 12 ms 23848 KB Output is correct
19 Correct 11 ms 23764 KB Output is correct
20 Correct 11 ms 23748 KB Output is correct
21 Correct 12 ms 23828 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23764 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23816 KB Output is correct
26 Correct 14 ms 24276 KB Output is correct
27 Correct 14 ms 24208 KB Output is correct
28 Correct 15 ms 24476 KB Output is correct
29 Correct 20 ms 24352 KB Output is correct
30 Correct 14 ms 24276 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 12 ms 24524 KB Output is correct
33 Correct 11 ms 23764 KB Output is correct
34 Correct 14 ms 24268 KB Output is correct
35 Correct 15 ms 24272 KB Output is correct
36 Correct 16 ms 24220 KB Output is correct
37 Correct 14 ms 24404 KB Output is correct
38 Correct 12 ms 23824 KB Output is correct
39 Correct 14 ms 24332 KB Output is correct
40 Correct 14 ms 24232 KB Output is correct
41 Correct 20 ms 24428 KB Output is correct
42 Correct 14 ms 24340 KB Output is correct
43 Correct 14 ms 24552 KB Output is correct
44 Correct 11 ms 23764 KB Output is correct
45 Correct 13 ms 24340 KB Output is correct
46 Correct 14 ms 24212 KB Output is correct
47 Correct 12 ms 23764 KB Output is correct
48 Correct 14 ms 24280 KB Output is correct
49 Correct 13 ms 24360 KB Output is correct
50 Correct 14 ms 24396 KB Output is correct
51 Correct 13 ms 24236 KB Output is correct
52 Correct 13 ms 24276 KB Output is correct
53 Correct 13 ms 24320 KB Output is correct
54 Correct 14 ms 24204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23812 KB Output is correct
3 Correct 17 ms 23816 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 12 ms 23740 KB Output is correct
7 Correct 12 ms 23828 KB Output is correct
8 Correct 11 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 13 ms 23800 KB Output is correct
11 Correct 12 ms 23824 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 14 ms 23812 KB Output is correct
14 Correct 13 ms 23752 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23744 KB Output is correct
18 Correct 12 ms 23848 KB Output is correct
19 Correct 11 ms 23764 KB Output is correct
20 Correct 11 ms 23748 KB Output is correct
21 Correct 12 ms 23828 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23764 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23812 KB Output is correct
26 Correct 80 ms 38760 KB Output is correct
27 Correct 115 ms 38380 KB Output is correct
28 Correct 14 ms 24208 KB Output is correct
29 Correct 14 ms 23764 KB Output is correct
30 Correct 13 ms 23828 KB Output is correct
31 Correct 105 ms 38548 KB Output is correct
32 Correct 14 ms 24276 KB Output is correct
33 Correct 144 ms 45580 KB Output is correct
34 Correct 116 ms 38412 KB Output is correct
35 Correct 15 ms 24276 KB Output is correct
36 Correct 121 ms 39300 KB Output is correct
37 Correct 14 ms 24312 KB Output is correct
38 Correct 14 ms 24208 KB Output is correct
39 Correct 93 ms 38656 KB Output is correct
40 Correct 14 ms 24468 KB Output is correct
41 Correct 106 ms 38700 KB Output is correct
42 Correct 131 ms 40688 KB Output is correct
43 Correct 12 ms 23764 KB Output is correct
44 Correct 154 ms 46400 KB Output is correct
45 Correct 141 ms 42096 KB Output is correct
46 Correct 14 ms 24272 KB Output is correct
47 Correct 15 ms 24208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 38720 KB Output is correct
2 Correct 68 ms 41236 KB Output is correct
3 Correct 16 ms 24304 KB Output is correct
4 Correct 14 ms 24276 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23808 KB Output is correct
7 Correct 13 ms 24204 KB Output is correct
8 Correct 105 ms 40224 KB Output is correct
9 Correct 16 ms 24276 KB Output is correct
10 Correct 100 ms 39120 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 107 ms 38964 KB Output is correct
13 Correct 130 ms 40080 KB Output is correct
14 Correct 95 ms 41292 KB Output is correct
15 Correct 73 ms 38624 KB Output is correct
16 Correct 14 ms 24316 KB Output is correct
17 Correct 13 ms 23796 KB Output is correct
18 Correct 73 ms 41256 KB Output is correct
19 Correct 123 ms 45140 KB Output is correct
20 Correct 14 ms 24236 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 78 ms 40456 KB Output is correct
23 Correct 14 ms 24252 KB Output is correct
24 Correct 101 ms 39476 KB Output is correct
25 Correct 114 ms 43880 KB Output is correct
26 Correct 13 ms 24308 KB Output is correct
27 Correct 15 ms 24336 KB Output is correct
28 Correct 14 ms 24276 KB Output is correct
29 Correct 14 ms 24276 KB Output is correct
30 Correct 13 ms 24332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23812 KB Output is correct
3 Correct 17 ms 23816 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 12 ms 23740 KB Output is correct
7 Correct 12 ms 23828 KB Output is correct
8 Correct 11 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 13 ms 23800 KB Output is correct
11 Correct 12 ms 23824 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 14 ms 23812 KB Output is correct
14 Correct 13 ms 23752 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23744 KB Output is correct
18 Correct 12 ms 23848 KB Output is correct
19 Correct 11 ms 23764 KB Output is correct
20 Correct 11 ms 23748 KB Output is correct
21 Correct 12 ms 23828 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23764 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23816 KB Output is correct
26 Correct 14 ms 24276 KB Output is correct
27 Correct 14 ms 24208 KB Output is correct
28 Correct 15 ms 24476 KB Output is correct
29 Correct 20 ms 24352 KB Output is correct
30 Correct 14 ms 24276 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 12 ms 24524 KB Output is correct
33 Correct 11 ms 23764 KB Output is correct
34 Correct 14 ms 24268 KB Output is correct
35 Correct 15 ms 24272 KB Output is correct
36 Correct 16 ms 24220 KB Output is correct
37 Correct 14 ms 24404 KB Output is correct
38 Correct 12 ms 23824 KB Output is correct
39 Correct 14 ms 24332 KB Output is correct
40 Correct 14 ms 24232 KB Output is correct
41 Correct 20 ms 24428 KB Output is correct
42 Correct 14 ms 24340 KB Output is correct
43 Correct 14 ms 24552 KB Output is correct
44 Correct 11 ms 23764 KB Output is correct
45 Correct 13 ms 24340 KB Output is correct
46 Correct 14 ms 24212 KB Output is correct
47 Correct 12 ms 23764 KB Output is correct
48 Correct 14 ms 24280 KB Output is correct
49 Correct 13 ms 24360 KB Output is correct
50 Correct 14 ms 24396 KB Output is correct
51 Correct 13 ms 24236 KB Output is correct
52 Correct 13 ms 24276 KB Output is correct
53 Correct 13 ms 24320 KB Output is correct
54 Correct 14 ms 24204 KB Output is correct
55 Correct 12 ms 23812 KB Output is correct
56 Correct 80 ms 38760 KB Output is correct
57 Correct 115 ms 38380 KB Output is correct
58 Correct 14 ms 24208 KB Output is correct
59 Correct 14 ms 23764 KB Output is correct
60 Correct 13 ms 23828 KB Output is correct
61 Correct 105 ms 38548 KB Output is correct
62 Correct 14 ms 24276 KB Output is correct
63 Correct 144 ms 45580 KB Output is correct
64 Correct 116 ms 38412 KB Output is correct
65 Correct 15 ms 24276 KB Output is correct
66 Correct 121 ms 39300 KB Output is correct
67 Correct 14 ms 24312 KB Output is correct
68 Correct 14 ms 24208 KB Output is correct
69 Correct 93 ms 38656 KB Output is correct
70 Correct 14 ms 24468 KB Output is correct
71 Correct 106 ms 38700 KB Output is correct
72 Correct 131 ms 40688 KB Output is correct
73 Correct 12 ms 23764 KB Output is correct
74 Correct 154 ms 46400 KB Output is correct
75 Correct 141 ms 42096 KB Output is correct
76 Correct 14 ms 24272 KB Output is correct
77 Correct 15 ms 24208 KB Output is correct
78 Correct 74 ms 38720 KB Output is correct
79 Correct 68 ms 41236 KB Output is correct
80 Correct 16 ms 24304 KB Output is correct
81 Correct 14 ms 24276 KB Output is correct
82 Correct 12 ms 23764 KB Output is correct
83 Correct 12 ms 23808 KB Output is correct
84 Correct 13 ms 24204 KB Output is correct
85 Correct 105 ms 40224 KB Output is correct
86 Correct 16 ms 24276 KB Output is correct
87 Correct 100 ms 39120 KB Output is correct
88 Correct 13 ms 23764 KB Output is correct
89 Correct 107 ms 38964 KB Output is correct
90 Correct 130 ms 40080 KB Output is correct
91 Correct 95 ms 41292 KB Output is correct
92 Correct 73 ms 38624 KB Output is correct
93 Correct 14 ms 24316 KB Output is correct
94 Correct 13 ms 23796 KB Output is correct
95 Correct 73 ms 41256 KB Output is correct
96 Correct 123 ms 45140 KB Output is correct
97 Correct 14 ms 24236 KB Output is correct
98 Correct 12 ms 23764 KB Output is correct
99 Correct 78 ms 40456 KB Output is correct
100 Correct 14 ms 24252 KB Output is correct
101 Correct 101 ms 39476 KB Output is correct
102 Correct 114 ms 43880 KB Output is correct
103 Correct 13 ms 24308 KB Output is correct
104 Correct 15 ms 24336 KB Output is correct
105 Correct 14 ms 24276 KB Output is correct
106 Correct 14 ms 24276 KB Output is correct
107 Correct 13 ms 24332 KB Output is correct
108 Correct 535 ms 107736 KB Output is correct
109 Correct 1046 ms 116668 KB Output is correct
110 Correct 1052 ms 129256 KB Output is correct
111 Correct 962 ms 135540 KB Output is correct
112 Correct 919 ms 125728 KB Output is correct
113 Correct 493 ms 112552 KB Output is correct
114 Correct 792 ms 98624 KB Output is correct
115 Correct 887 ms 98592 KB Output is correct
116 Correct 854 ms 103712 KB Output is correct
117 Correct 725 ms 112612 KB Output is correct
118 Correct 730 ms 99820 KB Output is correct
119 Correct 702 ms 112516 KB Output is correct
120 Correct 933 ms 126428 KB Output is correct
121 Correct 692 ms 112428 KB Output is correct
122 Correct 784 ms 107980 KB Output is correct
123 Correct 467 ms 112064 KB Output is correct
124 Correct 877 ms 110652 KB Output is correct