이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define LOCAL
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7) //0.0000001 the value
#define PI (acos((ld)-1.0))
#define MAXN (500006)
#define ll /*long long*/ int
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
#define space " "
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define ph push
#define btinpct(x) __builtin_popcountll((x))
#define MSB(bm) ((bm==0)?-1:(63-__builtin_clzll((bm))))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;}string to_string(bool b){return (b ? "true" : "false");}template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}void degug_out() { cerr << endl; }template <typename Head, typename... Tail>void degug_out(Head H, Tail... T) {cerr << " " << to_string(H);degug_out(T...);}inline ll gcd(ll a,ll b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#ifdef LOCAL
// #define degug(...) cerr << "[" << #__VA_ARGS__ << "]:", degug_out(__VA_ARGS__)
#else
// #define degug(...) 663
#define cerr if(0)cout
#endif
#warning "// FOR has been inclusive now!!!!"
ll n, k, A[MAXN];
vector<pi> v[MAXN];
vector<ll> g[MAXN];
int o[MAXN];
bitset<MAXN>cut;
struct ufds_ {
int p[MAXN];
ufds_ (){
FOR(i,0,MAXN-1) p[i]=i;
}
void merge(ll x,ll y) { p[find(y)]=find(x); }
ll find(ll x) { return (p[x]==x)?x:p[x]=find(p[x]); }
bool same(ll a,ll b){ return find(a)==find(b); }
} ufds;
// vector<ll> rs; bool special[MAXN];
// void add_up(ll x,ll p) {
// ++ cnt[A[x]];
// if((cnt[A[x]] ^ o[A[x]]) && cnt[A[x]]==1) -- corr;
// else if(cnt[A[x]]==o[A[x]]&&cnt[A[x]]-1 != 0) ++ corr;
// for(auto i:v[x]) if(i.f^p){
// add_up(i.f, x);
// }
// }
map<ll, ll> *mp[MAXN]; int corr[MAXN], sz[MAXN];
void dpdp(ll x,ll p) {
for(auto i:v[x]) if(i.f^p) dpdp(i.f,x);
mp[x]=new map<ll,ll>;
corr[x]=k;
ll ptr=x;
for(auto i:v[x]) if(i.f^p){
if(sz[i.f]>sz[ptr]) ptr=i.f;
}
mp[x]=mp[ptr]; corr[x]=corr[ptr]; sz[x]=sz[ptr];
for(auto i:v[x])if(i.f!=p&&i.f!=ptr){ sz[x]+=sz[i.f];
for(auto j:*mp[i.f]){
FOR(i,1,j.s){
++ (*mp[x])[j.f];
if((mp[x]->at(j.f) ^ o[j.f]) && mp[x]->at(j.f)==1) -- corr[x];
else if(mp[x]->at(j.f)==o[j.f]&&mp[x]->at(j.f)-1 != 0) ++ corr[x];
}
}
}
++ (*mp[x])[A[x]]; sz[x]++;
if((mp[x]->at(A[x]) ^ o[A[x]]) && mp[x]->at(A[x])==1) -- corr[x];
else if(mp[x]->at(A[x])==o[A[x]]&&mp[x]->at(A[x])-1 != 0) ++ corr[x];
}
void dfs(ll x,ll p) {
for(auto i:v[x]) if(i.f^p) {
if(corr[i.f]==k) {
cut[i.s]=1;
} else ufds.merge(A[x], A[i.f]);
dfs(i.f,x);
}
return;
}
// bitset<MAXN> vis;
// void solve(ll x, ll tc, ll num) {
// if(vis[x]) return;
// if(special[x] && num^1) return;
// vis[x]=1;
// ufds.merge(A[x], tc);
// for(auto i:v[x]){
// solve(i, tc, num+1);
// }
// }
vector<pi> e;
// bitset<MAXN> vis;
// void tryy(ll x){
// if(vis[x]) return;
// vis[x]=1;
// for(auto i:v2[x]) if(!cut[i.s]) {
// ufds.merge(A[x], A[i.f]); tryy(i.f);
// }
// }
int main()
{
FAST
cin>>n>>k;
FOR(i,1,n-1){
ll a, b;cin>>a>>b;
v[a].eb(b, i), v[b].eb(a, i);
e.eb(a, b);
}
FOR(i,1,n) cin>>A[i];
FOR(i,1,n) ++ o[A[i]];
dpdp(1,1);
dfs(1,1);
// FOR(i,1,n) tryy(i);
// for(auto i:rs) { if(0)cerr<<i<<'\n'; solve(i, A[i], 1); }
for(auto i:e){
ll a=ufds.find(A[i.f]), b=ufds.find(A[i.s]);
if(a==b) continue;
g[a].eb(b), g[b].eb(a);
}
FOR(i,1,k) sort(all(g[i])), g[i].resize(unique(all(g[i]))-g[i].begin());
ll leaves=0; FOR(i,1,k) if(ufds.find(i)==i&&g[i].size()==1) ++leaves;
cout<<(ll)ceill(leaves/(ld)2);
}
컴파일 시 표준 에러 (stderr) 메시지
mergers.cpp:42:2: warning: #warning "// FOR has been inclusive now!!!!" [-Wcpp]
#warning "// FOR has been inclusive now!!!!"
^~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |