Submission #923951

# Submission time Handle Problem Language Result Execution time Memory
923951 2024-02-08T06:52:39 Z boris_mihov Mergers (JOI19_mergers) C++17
100 / 100
972 ms 69428 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <bitset>
#include <random>
#include <queue>
#include <set>
 
typedef long long llong;
const int MAXN = 500000 + 10;
const int INTINF = 2e9;
const llong INF = 1e18;
const int MAXLOG = 17;
 
int n, k;
int s[MAXN];
int sz[MAXN];
int cnt[MAXN];
int full[MAXN];
int szBad[MAXN];
int szLeaf[MAXN];
bool isBad[MAXN];
std::vector <int> g[MAXN];
 
void buildDFS(int node, int par)
{
    for (int &u : g[node])
    {
        if (u == par)
        {
            std::swap(u, g[node].back());
            g[node].pop_back();
            break;
        }
    }
 
    sz[node] = 1;
    for (const int &u : g[node])
    {
        buildDFS(u, node);
        sz[node] += sz[u];
    }
 
    for (int i = 1 ; i < g[node].size() ; ++i)
    {
        if (sz[g[node][i]] > sz[g[node][0]])
        {
            std::swap(g[node][i], g[node][0]);
        }
    }
}
 
int cntUnfull;
void addNode(int node)
{
    cntUnfull -= (0 < cnt[s[node]] && cnt[s[node]] < full[s[node]]);
    cnt[s[node]]++;
    cntUnfull += (0 < cnt[s[node]] && cnt[s[node]] < full[s[node]]);
}
 
void remNode(int node)
{
    cntUnfull -= (0 < cnt[s[node]] && cnt[s[node]] < full[s[node]]);
    cnt[s[node]]--;
    cntUnfull += (0 < cnt[s[node]] && cnt[s[node]] < full[s[node]]);
}
 
void addDFS(int node)
{
    addNode(node);
    for (const int &u : g[node])
    {
        addDFS(u);
    }
}
 
void remDFS(int node)
{
    remNode(node);
    for (const int &u : g[node])
    {
        remDFS(u);
    }
}
 
void solveDFS(int node)
{
    for (int i = 1 ; i < g[node].size() ; ++i)
    {
        solveDFS(g[node][i]);
        remDFS(g[node][i]);
    }
 
    if (g[node].size())
    {
        solveDFS(g[node][0]);
    }
 
    for (int i = 1 ; i < g[node].size() ; ++i)
    {
        addDFS(g[node][i]);
    }
 
    addNode(node);
    if (cntUnfull == 0)
    {
        isBad[node] = true;
    }
}
 
int leaveCnt;
int coverCnt;
void calcDFS(int node)
{
    szBad[node] = isBad[node];
    for (const int &u : g[node])
    {
        calcDFS(u);
        szBad[node] += szBad[u];
    }
}
 
void ansDFS(int node)
{
    szLeaf[node] = isBad[node] && (szBad[node] == 1);
    for (const int &u : g[node])
    {
        ansDFS(u);
        szLeaf[node] += szLeaf[u];
    }
}
 
void solve()
{
    buildDFS(1, 0);
    solveDFS(1);
    isBad[1] = false;
    calcDFS(1);
    ansDFS(1);
    
    for (int i = 2 ; i <= n ; ++i)
    {
        if (isBad[i])
        {
            coverCnt += (szLeaf[i] > 1 && szLeaf[i] == szLeaf[1]);
            leaveCnt += (szBad[i] == 1);
        }
    }   
 
    // std::cout << "here: " << leaveCnt << ' ' << coverCnt << '\n';
    coverCnt = (coverCnt > 0);
    leaveCnt += coverCnt;
    std::cout << leaveCnt / 2 + (leaveCnt % 2) << '\n';
}
 
void input()
{
    std::cin >> n >> k;
    for (int i = 1 ; i < n ; ++i)
    {
        int u, v;
        std::cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
 
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> s[i];
        full[s[i]]++;
    }
}
 
void fastIOI()
{   
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}
 
int main()
{
    fastIOI();
    input();
    solve();
 
    return 0;
}

Compilation message

mergers.cpp: In function 'void buildDFS(int, int)':
mergers.cpp:46:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for (int i = 1 ; i < g[node].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~
mergers.cpp: In function 'void solveDFS(int)':
mergers.cpp:90:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |     for (int i = 1 ; i < g[node].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~
mergers.cpp:101:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |     for (int i = 1 ; i < g[node].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 4 ms 21152 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 4 ms 21084 KB Output is correct
7 Correct 5 ms 21148 KB Output is correct
8 Correct 4 ms 21084 KB Output is correct
9 Correct 4 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 4 ms 21084 KB Output is correct
13 Correct 4 ms 21084 KB Output is correct
14 Correct 4 ms 21084 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 5 ms 21336 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 5 ms 21100 KB Output is correct
19 Correct 4 ms 21144 KB Output is correct
20 Correct 4 ms 21144 KB Output is correct
21 Correct 4 ms 21080 KB Output is correct
22 Correct 4 ms 21080 KB Output is correct
23 Correct 4 ms 21084 KB Output is correct
24 Correct 4 ms 21172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 4 ms 21152 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 4 ms 21084 KB Output is correct
7 Correct 5 ms 21148 KB Output is correct
8 Correct 4 ms 21084 KB Output is correct
9 Correct 4 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 4 ms 21084 KB Output is correct
13 Correct 4 ms 21084 KB Output is correct
14 Correct 4 ms 21084 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 5 ms 21336 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 5 ms 21100 KB Output is correct
19 Correct 4 ms 21144 KB Output is correct
20 Correct 4 ms 21144 KB Output is correct
21 Correct 4 ms 21080 KB Output is correct
22 Correct 4 ms 21080 KB Output is correct
23 Correct 4 ms 21084 KB Output is correct
24 Correct 4 ms 21172 KB Output is correct
25 Correct 4 ms 21084 KB Output is correct
26 Correct 5 ms 21080 KB Output is correct
27 Correct 5 ms 21084 KB Output is correct
28 Correct 5 ms 21160 KB Output is correct
29 Correct 6 ms 21160 KB Output is correct
30 Correct 5 ms 21080 KB Output is correct
31 Correct 4 ms 21092 KB Output is correct
32 Correct 5 ms 21336 KB Output is correct
33 Correct 4 ms 21084 KB Output is correct
34 Correct 5 ms 21156 KB Output is correct
35 Correct 6 ms 21156 KB Output is correct
36 Correct 5 ms 21084 KB Output is correct
37 Correct 7 ms 21084 KB Output is correct
38 Correct 5 ms 21148 KB Output is correct
39 Correct 5 ms 21084 KB Output is correct
40 Correct 5 ms 21084 KB Output is correct
41 Correct 5 ms 21084 KB Output is correct
42 Correct 5 ms 21084 KB Output is correct
43 Correct 5 ms 21344 KB Output is correct
44 Correct 4 ms 21084 KB Output is correct
45 Correct 5 ms 21108 KB Output is correct
46 Correct 6 ms 21084 KB Output is correct
47 Correct 5 ms 21080 KB Output is correct
48 Correct 6 ms 21080 KB Output is correct
49 Correct 6 ms 21084 KB Output is correct
50 Correct 5 ms 21364 KB Output is correct
51 Correct 5 ms 21084 KB Output is correct
52 Correct 5 ms 21084 KB Output is correct
53 Correct 5 ms 21084 KB Output is correct
54 Correct 5 ms 21080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 4 ms 21152 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 4 ms 21084 KB Output is correct
7 Correct 5 ms 21148 KB Output is correct
8 Correct 4 ms 21084 KB Output is correct
9 Correct 4 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 4 ms 21084 KB Output is correct
13 Correct 4 ms 21084 KB Output is correct
14 Correct 4 ms 21084 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 5 ms 21336 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 5 ms 21100 KB Output is correct
19 Correct 4 ms 21144 KB Output is correct
20 Correct 4 ms 21144 KB Output is correct
21 Correct 4 ms 21080 KB Output is correct
22 Correct 4 ms 21080 KB Output is correct
23 Correct 4 ms 21084 KB Output is correct
24 Correct 4 ms 21172 KB Output is correct
25 Correct 4 ms 21084 KB Output is correct
26 Correct 32 ms 26144 KB Output is correct
27 Correct 59 ms 25688 KB Output is correct
28 Correct 6 ms 21084 KB Output is correct
29 Correct 5 ms 21084 KB Output is correct
30 Correct 4 ms 21080 KB Output is correct
31 Correct 59 ms 25700 KB Output is correct
32 Correct 5 ms 21336 KB Output is correct
33 Correct 57 ms 31312 KB Output is correct
34 Correct 59 ms 26068 KB Output is correct
35 Correct 6 ms 21332 KB Output is correct
36 Correct 52 ms 26196 KB Output is correct
37 Correct 5 ms 21084 KB Output is correct
38 Correct 5 ms 21160 KB Output is correct
39 Correct 31 ms 25932 KB Output is correct
40 Correct 5 ms 21340 KB Output is correct
41 Correct 51 ms 25736 KB Output is correct
42 Correct 61 ms 27648 KB Output is correct
43 Correct 4 ms 21084 KB Output is correct
44 Correct 67 ms 31836 KB Output is correct
45 Correct 55 ms 28740 KB Output is correct
46 Correct 5 ms 21084 KB Output is correct
47 Correct 5 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 24528 KB Output is correct
2 Correct 39 ms 25044 KB Output is correct
3 Correct 5 ms 21080 KB Output is correct
4 Correct 5 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 5 ms 21084 KB Output is correct
7 Correct 5 ms 21084 KB Output is correct
8 Correct 64 ms 24404 KB Output is correct
9 Correct 5 ms 21084 KB Output is correct
10 Correct 77 ms 24376 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 77 ms 24324 KB Output is correct
13 Correct 61 ms 24324 KB Output is correct
14 Correct 66 ms 24776 KB Output is correct
15 Correct 47 ms 24648 KB Output is correct
16 Correct 5 ms 21084 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 36 ms 24996 KB Output is correct
19 Correct 63 ms 27732 KB Output is correct
20 Correct 6 ms 21084 KB Output is correct
21 Correct 4 ms 21084 KB Output is correct
22 Correct 40 ms 26456 KB Output is correct
23 Correct 5 ms 21080 KB Output is correct
24 Correct 96 ms 25952 KB Output is correct
25 Correct 47 ms 28496 KB Output is correct
26 Correct 6 ms 21232 KB Output is correct
27 Correct 5 ms 21160 KB Output is correct
28 Correct 5 ms 21084 KB Output is correct
29 Correct 6 ms 21084 KB Output is correct
30 Correct 5 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 4 ms 21152 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
6 Correct 4 ms 21084 KB Output is correct
7 Correct 5 ms 21148 KB Output is correct
8 Correct 4 ms 21084 KB Output is correct
9 Correct 4 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 4 ms 21084 KB Output is correct
13 Correct 4 ms 21084 KB Output is correct
14 Correct 4 ms 21084 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 5 ms 21336 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 5 ms 21100 KB Output is correct
19 Correct 4 ms 21144 KB Output is correct
20 Correct 4 ms 21144 KB Output is correct
21 Correct 4 ms 21080 KB Output is correct
22 Correct 4 ms 21080 KB Output is correct
23 Correct 4 ms 21084 KB Output is correct
24 Correct 4 ms 21172 KB Output is correct
25 Correct 4 ms 21084 KB Output is correct
26 Correct 5 ms 21080 KB Output is correct
27 Correct 5 ms 21084 KB Output is correct
28 Correct 5 ms 21160 KB Output is correct
29 Correct 6 ms 21160 KB Output is correct
30 Correct 5 ms 21080 KB Output is correct
31 Correct 4 ms 21092 KB Output is correct
32 Correct 5 ms 21336 KB Output is correct
33 Correct 4 ms 21084 KB Output is correct
34 Correct 5 ms 21156 KB Output is correct
35 Correct 6 ms 21156 KB Output is correct
36 Correct 5 ms 21084 KB Output is correct
37 Correct 7 ms 21084 KB Output is correct
38 Correct 5 ms 21148 KB Output is correct
39 Correct 5 ms 21084 KB Output is correct
40 Correct 5 ms 21084 KB Output is correct
41 Correct 5 ms 21084 KB Output is correct
42 Correct 5 ms 21084 KB Output is correct
43 Correct 5 ms 21344 KB Output is correct
44 Correct 4 ms 21084 KB Output is correct
45 Correct 5 ms 21108 KB Output is correct
46 Correct 6 ms 21084 KB Output is correct
47 Correct 5 ms 21080 KB Output is correct
48 Correct 6 ms 21080 KB Output is correct
49 Correct 6 ms 21084 KB Output is correct
50 Correct 5 ms 21364 KB Output is correct
51 Correct 5 ms 21084 KB Output is correct
52 Correct 5 ms 21084 KB Output is correct
53 Correct 5 ms 21084 KB Output is correct
54 Correct 5 ms 21080 KB Output is correct
55 Correct 4 ms 21084 KB Output is correct
56 Correct 32 ms 26144 KB Output is correct
57 Correct 59 ms 25688 KB Output is correct
58 Correct 6 ms 21084 KB Output is correct
59 Correct 5 ms 21084 KB Output is correct
60 Correct 4 ms 21080 KB Output is correct
61 Correct 59 ms 25700 KB Output is correct
62 Correct 5 ms 21336 KB Output is correct
63 Correct 57 ms 31312 KB Output is correct
64 Correct 59 ms 26068 KB Output is correct
65 Correct 6 ms 21332 KB Output is correct
66 Correct 52 ms 26196 KB Output is correct
67 Correct 5 ms 21084 KB Output is correct
68 Correct 5 ms 21160 KB Output is correct
69 Correct 31 ms 25932 KB Output is correct
70 Correct 5 ms 21340 KB Output is correct
71 Correct 51 ms 25736 KB Output is correct
72 Correct 61 ms 27648 KB Output is correct
73 Correct 4 ms 21084 KB Output is correct
74 Correct 67 ms 31836 KB Output is correct
75 Correct 55 ms 28740 KB Output is correct
76 Correct 5 ms 21084 KB Output is correct
77 Correct 5 ms 21084 KB Output is correct
78 Correct 32 ms 24528 KB Output is correct
79 Correct 39 ms 25044 KB Output is correct
80 Correct 5 ms 21080 KB Output is correct
81 Correct 5 ms 21084 KB Output is correct
82 Correct 4 ms 21084 KB Output is correct
83 Correct 5 ms 21084 KB Output is correct
84 Correct 5 ms 21084 KB Output is correct
85 Correct 64 ms 24404 KB Output is correct
86 Correct 5 ms 21084 KB Output is correct
87 Correct 77 ms 24376 KB Output is correct
88 Correct 4 ms 21084 KB Output is correct
89 Correct 77 ms 24324 KB Output is correct
90 Correct 61 ms 24324 KB Output is correct
91 Correct 66 ms 24776 KB Output is correct
92 Correct 47 ms 24648 KB Output is correct
93 Correct 5 ms 21084 KB Output is correct
94 Correct 5 ms 21084 KB Output is correct
95 Correct 36 ms 24996 KB Output is correct
96 Correct 63 ms 27732 KB Output is correct
97 Correct 6 ms 21084 KB Output is correct
98 Correct 4 ms 21084 KB Output is correct
99 Correct 40 ms 26456 KB Output is correct
100 Correct 5 ms 21080 KB Output is correct
101 Correct 96 ms 25952 KB Output is correct
102 Correct 47 ms 28496 KB Output is correct
103 Correct 6 ms 21232 KB Output is correct
104 Correct 5 ms 21160 KB Output is correct
105 Correct 5 ms 21084 KB Output is correct
106 Correct 6 ms 21084 KB Output is correct
107 Correct 5 ms 21084 KB Output is correct
108 Correct 252 ms 51688 KB Output is correct
109 Correct 930 ms 60444 KB Output is correct
110 Correct 815 ms 69428 KB Output is correct
111 Correct 948 ms 68872 KB Output is correct
112 Correct 726 ms 61052 KB Output is correct
113 Correct 315 ms 51980 KB Output is correct
114 Correct 678 ms 46160 KB Output is correct
115 Correct 659 ms 46168 KB Output is correct
116 Correct 643 ms 48212 KB Output is correct
117 Correct 779 ms 50548 KB Output is correct
118 Correct 687 ms 46784 KB Output is correct
119 Correct 677 ms 50536 KB Output is correct
120 Correct 733 ms 61680 KB Output is correct
121 Correct 769 ms 50644 KB Output is correct
122 Correct 725 ms 50612 KB Output is correct
123 Correct 278 ms 51924 KB Output is correct
124 Correct 972 ms 49776 KB Output is correct