Submission #1210395

#TimeUsernameProblemLanguageResultExecution timeMemory
1210395hainam2k9Mergers (JOI19_mergers)C++20
100 / 100
267 ms59328 KiB
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 5e5+5;
const string NAME = "";
int n,k,a[MAXN],last[MAXN],cnt[MAXN],rs=0;
int depth[MAXN],par[MAXN];
int p[MAXN];
vector<int> adj[MAXN];
void dfs(int u){
    for(int& v : adj[u])
        if(v!=par[u]) depth[v]=depth[u]+1, par[v]=u, dfs(v);
}
int Find(int u){
    if(u==p[u]) return u;
    return p[u]=Find(p[u]);
}
void Union(int x, int y){
    x=Find(x), y=Find(y);
    if(x==y) return;
    if(depth[x]>depth[y]) swap(x,y);
    p[y]=x;
}
void Merge(int x, int y){
    while(x!=y){
        if(depth[x]<depth[y]) swap(x,y);
        Union(x,par[x]), x=Find(x);
    }
}
int main()
{
    tt;
    if(fopen((NAME + ".INP").c_str(), "r")) fo;
    cin >> n >> k;
    for(int i = 1; i<n; ++i){
        int x,y;
        cin >> x >> y;
        adj[x].pb(y), adj[y].pb(x);
    }
    dfs(1);
    iota(p+1,p+n+1,1);
    for(int i = 1; i<=n; ++i){
        cin >> a[i];
        if(last[a[i]]) Merge(last[a[i]],i);
        last[a[i]]=i;
    }
    for(int i = 1; i<=n; ++i)
        for(int& j : adj[i])
            if(Find(i)!=Find(j)) ++cnt[Find(i)];
    for(int i = 1; i<=n; ++i)
        rs+=cnt[i]==1;
    cout << (rs+1)/2;
}

Compilation message (stderr)

mergers.cpp: In function 'int main()':
mergers.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:49:45: note: in expansion of macro 'fo'
   49 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
mergers.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |                                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:49:45: note: in expansion of macro 'fo'
   49 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...