Submission #923923

# Submission time Handle Problem Language Result Execution time Memory
923923 2024-02-08T06:40:28 Z boris_mihov Mergers (JOI19_mergers) C++17
0 / 100
37 ms 26752 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])
    {
        if (u == par)
        {
            continue;
        }
 
        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])
    {
        addDFS(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);
        }
    }   
 
    coverCnt = (coverCnt > 0);
    std::cout << leaveCnt / 2 + (leaveCnt % 2) + coverCnt << '\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:51:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for (int i = 1 ; i < g[node].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~
mergers.cpp: In function 'void solveDFS(int)':
mergers.cpp:95:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |     for (int i = 1 ; i < g[node].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~
mergers.cpp:106:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |     for (int i = 1 ; i < g[node].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 21084 KB Output is correct
2 Incorrect 4 ms 21084 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 21084 KB Output is correct
2 Incorrect 4 ms 21084 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 21084 KB Output is correct
2 Incorrect 4 ms 21084 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 31 ms 26068 KB Output is correct
2 Correct 37 ms 26752 KB Output is correct
3 Correct 5 ms 21080 KB Output is correct
4 Incorrect 5 ms 21084 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 21084 KB Output is correct
2 Incorrect 4 ms 21084 KB Output isn't correct
3 Halted 0 ms 0 KB -