Submission #923935

# Submission time Handle Problem Language Result Execution time Memory
923935 2024-02-08T06:46:12 Z boris_mihov Mergers (JOI19_mergers) C++17
0 / 100
45 ms 45524 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;
    }

    assert(node == 1 || !isBad[node]);
}
 
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: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 Runtime error 21 ms 38232 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 21 ms 38232 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 21 ms 38232 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 24528 KB Output is correct
2 Runtime error 45 ms 45524 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 21 ms 38232 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -