Submission #848317

# Submission time Handle Problem Language Result Execution time Memory
848317 2023-09-12T05:42:18 Z damwuan Mergers (JOI19_mergers) C++17
0 / 100
51 ms 36296 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=5e5+5;
const ll offset=1e18;
const ll block_sz=317;
const ll inf=1e18;
const ll mod=1e9+7;
int n,k,in[maxn],par[maxn],up[maxn],Time;
vector<int> adj[maxn],L[maxn];
set<pii> S;
set<pii>::iterator x;
//bool vis[maxn];
int Find(int u)
{
    return par[u]<0?u:par[u]=Find(par[u]);
}
bool Union(int u,int v)
{
    if ((u=Find(u))==(v=Find(v)))
    {
        return 0;
    }
    //if (par[u]>par[v]) swap(u,v);
    par[u]+=par[v];
    par[v]=u;
    return 1;
}
void dfs(int u,int pre)
{
    up[u]=pre;
    in[u]=in[pre]+1;
    for(int v:adj[u])
    {
        if (v==pre) continue;
        dfs(v,u);
    }
}
void sol()
{
    cin >> n>> k;
    for1(i,1,n-1)
    {
        int u,v;cin >>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1,1);
    for1(i,1,n)
    {
        par[i]=-1;
        int g;cin >> g;L[g].pb(i);
    }
    for1(i,1,k)
    {
        S.clear();
        for (int v:L[i])
        {
            S.insert({in[Find(v)],Find(v)});
        }
        while(S.size()>1)
        {
            auto x=(--S.end());
            S.erase(x);
            Union(up[x->se],x-> se);
            S.insert({in[Find(up[x->se])],Find(up[x->se])});
        }
    }
//    for1(i,1,n) cout << par[i]<<' ';
//    cout << '\n';
    int cnt=0;
    for1(u,1,n)
    {
        int x=0;
        for(int v: adj[u])
        {
            if (Find(u)!=Find(v))
            {
                //if (u==3) cout << v<<'\n';
                x++;
            }
        }
        if (x==1)
        {
            //cerr<<u<<'\n';
            cnt++;
        }
    }
    cout << (cnt+1)/2;



}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("GROUPDIV.inp","r",stdin);
//    freopen("GROUPDIV.out","w",stdout);

    int t=1;//cin >> t;
    while (t--)
    {
        sol();
    }
}
/*

3 1
12345678
?11
*/
# Verdict Execution time Memory Grader output
1 Correct 7 ms 29272 KB Output is correct
2 Correct 6 ms 29528 KB Output is correct
3 Incorrect 8 ms 29272 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 29272 KB Output is correct
2 Correct 6 ms 29528 KB Output is correct
3 Incorrect 8 ms 29272 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 29272 KB Output is correct
2 Correct 6 ms 29528 KB Output is correct
3 Incorrect 8 ms 29272 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 44 ms 34504 KB Output is correct
2 Correct 51 ms 36296 KB Output is correct
3 Incorrect 7 ms 29528 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 29272 KB Output is correct
2 Correct 6 ms 29528 KB Output is correct
3 Incorrect 8 ms 29272 KB Output isn't correct
4 Halted 0 ms 0 KB -