답안 #571667

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
571667 2022-06-02T13:46:44 Z YouAreMyGalaxy Mergers (JOI19_mergers) C++17
100 / 100
1077 ms 135628 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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23804 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 12 ms 23808 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23808 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23812 KB Output is correct
13 Correct 11 ms 23796 KB Output is correct
14 Correct 13 ms 23812 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23732 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23812 KB Output is correct
20 Correct 12 ms 23764 KB Output is correct
21 Correct 13 ms 23764 KB Output is correct
22 Correct 12 ms 23812 KB Output is correct
23 Correct 12 ms 23748 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23804 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 12 ms 23808 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23808 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23812 KB Output is correct
13 Correct 11 ms 23796 KB Output is correct
14 Correct 13 ms 23812 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23732 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23812 KB Output is correct
20 Correct 12 ms 23764 KB Output is correct
21 Correct 13 ms 23764 KB Output is correct
22 Correct 12 ms 23812 KB Output is correct
23 Correct 12 ms 23748 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23816 KB Output is correct
26 Correct 13 ms 24276 KB Output is correct
27 Correct 13 ms 24280 KB Output is correct
28 Correct 15 ms 24392 KB Output is correct
29 Correct 13 ms 24336 KB Output is correct
30 Correct 13 ms 24288 KB Output is correct
31 Correct 12 ms 23828 KB Output is correct
32 Correct 13 ms 24504 KB Output is correct
33 Correct 14 ms 23764 KB Output is correct
34 Correct 15 ms 24208 KB Output is correct
35 Correct 14 ms 24276 KB Output is correct
36 Correct 14 ms 24276 KB Output is correct
37 Correct 15 ms 24276 KB Output is correct
38 Correct 12 ms 23764 KB Output is correct
39 Correct 14 ms 24276 KB Output is correct
40 Correct 14 ms 24292 KB Output is correct
41 Correct 14 ms 24276 KB Output is correct
42 Correct 14 ms 24316 KB Output is correct
43 Correct 14 ms 24532 KB Output is correct
44 Correct 12 ms 23812 KB Output is correct
45 Correct 13 ms 24332 KB Output is correct
46 Correct 14 ms 24228 KB Output is correct
47 Correct 12 ms 23744 KB Output is correct
48 Correct 14 ms 24312 KB Output is correct
49 Correct 17 ms 24356 KB Output is correct
50 Correct 14 ms 24392 KB Output is correct
51 Correct 14 ms 24336 KB Output is correct
52 Correct 13 ms 24208 KB Output is correct
53 Correct 14 ms 24264 KB Output is correct
54 Correct 14 ms 24208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23804 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 12 ms 23808 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23808 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23812 KB Output is correct
13 Correct 11 ms 23796 KB Output is correct
14 Correct 13 ms 23812 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23732 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23812 KB Output is correct
20 Correct 12 ms 23764 KB Output is correct
21 Correct 13 ms 23764 KB Output is correct
22 Correct 12 ms 23812 KB Output is correct
23 Correct 12 ms 23748 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 76 ms 38708 KB Output is correct
27 Correct 117 ms 38396 KB Output is correct
28 Correct 13 ms 24276 KB Output is correct
29 Correct 11 ms 23732 KB Output is correct
30 Correct 12 ms 23844 KB Output is correct
31 Correct 109 ms 38616 KB Output is correct
32 Correct 14 ms 24208 KB Output is correct
33 Correct 140 ms 45540 KB Output is correct
34 Correct 116 ms 38480 KB Output is correct
35 Correct 15 ms 24268 KB Output is correct
36 Correct 129 ms 39420 KB Output is correct
37 Correct 14 ms 24276 KB Output is correct
38 Correct 14 ms 24208 KB Output is correct
39 Correct 74 ms 38656 KB Output is correct
40 Correct 14 ms 24532 KB Output is correct
41 Correct 105 ms 38708 KB Output is correct
42 Correct 152 ms 40704 KB Output is correct
43 Correct 15 ms 23752 KB Output is correct
44 Correct 142 ms 46420 KB Output is correct
45 Correct 140 ms 42120 KB Output is correct
46 Correct 13 ms 24204 KB Output is correct
47 Correct 16 ms 24208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 38804 KB Output is correct
2 Correct 74 ms 41152 KB Output is correct
3 Correct 16 ms 24272 KB Output is correct
4 Correct 15 ms 24240 KB Output is correct
5 Correct 13 ms 23820 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 14 ms 24188 KB Output is correct
8 Correct 106 ms 40268 KB Output is correct
9 Correct 14 ms 24336 KB Output is correct
10 Correct 101 ms 39064 KB Output is correct
11 Correct 12 ms 23740 KB Output is correct
12 Correct 108 ms 38988 KB Output is correct
13 Correct 106 ms 40112 KB Output is correct
14 Correct 101 ms 41200 KB Output is correct
15 Correct 76 ms 38684 KB Output is correct
16 Correct 14 ms 24236 KB Output is correct
17 Correct 12 ms 23808 KB Output is correct
18 Correct 75 ms 41332 KB Output is correct
19 Correct 139 ms 45192 KB Output is correct
20 Correct 14 ms 24248 KB Output is correct
21 Correct 12 ms 23812 KB Output is correct
22 Correct 78 ms 40440 KB Output is correct
23 Correct 13 ms 24228 KB Output is correct
24 Correct 108 ms 39560 KB Output is correct
25 Correct 116 ms 43848 KB Output is correct
26 Correct 13 ms 24340 KB Output is correct
27 Correct 14 ms 24420 KB Output is correct
28 Correct 14 ms 24212 KB Output is correct
29 Correct 13 ms 24212 KB Output is correct
30 Correct 13 ms 24300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23804 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 12 ms 23808 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23808 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23812 KB Output is correct
13 Correct 11 ms 23796 KB Output is correct
14 Correct 13 ms 23812 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23732 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23812 KB Output is correct
20 Correct 12 ms 23764 KB Output is correct
21 Correct 13 ms 23764 KB Output is correct
22 Correct 12 ms 23812 KB Output is correct
23 Correct 12 ms 23748 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23816 KB Output is correct
26 Correct 13 ms 24276 KB Output is correct
27 Correct 13 ms 24280 KB Output is correct
28 Correct 15 ms 24392 KB Output is correct
29 Correct 13 ms 24336 KB Output is correct
30 Correct 13 ms 24288 KB Output is correct
31 Correct 12 ms 23828 KB Output is correct
32 Correct 13 ms 24504 KB Output is correct
33 Correct 14 ms 23764 KB Output is correct
34 Correct 15 ms 24208 KB Output is correct
35 Correct 14 ms 24276 KB Output is correct
36 Correct 14 ms 24276 KB Output is correct
37 Correct 15 ms 24276 KB Output is correct
38 Correct 12 ms 23764 KB Output is correct
39 Correct 14 ms 24276 KB Output is correct
40 Correct 14 ms 24292 KB Output is correct
41 Correct 14 ms 24276 KB Output is correct
42 Correct 14 ms 24316 KB Output is correct
43 Correct 14 ms 24532 KB Output is correct
44 Correct 12 ms 23812 KB Output is correct
45 Correct 13 ms 24332 KB Output is correct
46 Correct 14 ms 24228 KB Output is correct
47 Correct 12 ms 23744 KB Output is correct
48 Correct 14 ms 24312 KB Output is correct
49 Correct 17 ms 24356 KB Output is correct
50 Correct 14 ms 24392 KB Output is correct
51 Correct 14 ms 24336 KB Output is correct
52 Correct 13 ms 24208 KB Output is correct
53 Correct 14 ms 24264 KB Output is correct
54 Correct 14 ms 24208 KB Output is correct
55 Correct 12 ms 23764 KB Output is correct
56 Correct 76 ms 38708 KB Output is correct
57 Correct 117 ms 38396 KB Output is correct
58 Correct 13 ms 24276 KB Output is correct
59 Correct 11 ms 23732 KB Output is correct
60 Correct 12 ms 23844 KB Output is correct
61 Correct 109 ms 38616 KB Output is correct
62 Correct 14 ms 24208 KB Output is correct
63 Correct 140 ms 45540 KB Output is correct
64 Correct 116 ms 38480 KB Output is correct
65 Correct 15 ms 24268 KB Output is correct
66 Correct 129 ms 39420 KB Output is correct
67 Correct 14 ms 24276 KB Output is correct
68 Correct 14 ms 24208 KB Output is correct
69 Correct 74 ms 38656 KB Output is correct
70 Correct 14 ms 24532 KB Output is correct
71 Correct 105 ms 38708 KB Output is correct
72 Correct 152 ms 40704 KB Output is correct
73 Correct 15 ms 23752 KB Output is correct
74 Correct 142 ms 46420 KB Output is correct
75 Correct 140 ms 42120 KB Output is correct
76 Correct 13 ms 24204 KB Output is correct
77 Correct 16 ms 24208 KB Output is correct
78 Correct 79 ms 38804 KB Output is correct
79 Correct 74 ms 41152 KB Output is correct
80 Correct 16 ms 24272 KB Output is correct
81 Correct 15 ms 24240 KB Output is correct
82 Correct 13 ms 23820 KB Output is correct
83 Correct 12 ms 23764 KB Output is correct
84 Correct 14 ms 24188 KB Output is correct
85 Correct 106 ms 40268 KB Output is correct
86 Correct 14 ms 24336 KB Output is correct
87 Correct 101 ms 39064 KB Output is correct
88 Correct 12 ms 23740 KB Output is correct
89 Correct 108 ms 38988 KB Output is correct
90 Correct 106 ms 40112 KB Output is correct
91 Correct 101 ms 41200 KB Output is correct
92 Correct 76 ms 38684 KB Output is correct
93 Correct 14 ms 24236 KB Output is correct
94 Correct 12 ms 23808 KB Output is correct
95 Correct 75 ms 41332 KB Output is correct
96 Correct 139 ms 45192 KB Output is correct
97 Correct 14 ms 24248 KB Output is correct
98 Correct 12 ms 23812 KB Output is correct
99 Correct 78 ms 40440 KB Output is correct
100 Correct 13 ms 24228 KB Output is correct
101 Correct 108 ms 39560 KB Output is correct
102 Correct 116 ms 43848 KB Output is correct
103 Correct 13 ms 24340 KB Output is correct
104 Correct 14 ms 24420 KB Output is correct
105 Correct 14 ms 24212 KB Output is correct
106 Correct 13 ms 24212 KB Output is correct
107 Correct 13 ms 24300 KB Output is correct
108 Correct 532 ms 107704 KB Output is correct
109 Correct 1077 ms 116580 KB Output is correct
110 Correct 1069 ms 129216 KB Output is correct
111 Correct 946 ms 135628 KB Output is correct
112 Correct 888 ms 125736 KB Output is correct
113 Correct 507 ms 112524 KB Output is correct
114 Correct 785 ms 98564 KB Output is correct
115 Correct 789 ms 98632 KB Output is correct
116 Correct 841 ms 103792 KB Output is correct
117 Correct 685 ms 112544 KB Output is correct
118 Correct 844 ms 99956 KB Output is correct
119 Correct 687 ms 112504 KB Output is correct
120 Correct 940 ms 126408 KB Output is correct
121 Correct 696 ms 112536 KB Output is correct
122 Correct 797 ms 107932 KB Output is correct
123 Correct 477 ms 112160 KB Output is correct
124 Correct 878 ms 110652 KB Output is correct