답안 #154288

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
154288 2019-09-20T09:49:06 Z ryansee Unique Cities (JOI19_ho_t5) C++14
0 / 100
298 ms 17180 KB
#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#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)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}

#define ll /*long long*/ int 
#define ld long double
#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)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;

#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (200006)
ll n, m;
vector<int> v[MAXN];
int A[MAXN], p[MAXN];
bool F[MAXN];
ll fdist[MAXN], depth[MAXN], rdist[MAXN];
void init(ll x,ll par) {
	rdist[x] = fdist[x] = depth[x]; p[x]=par;
	pi mx=pi(depth[x], 1);
	pi fmx=pi(depth[x], 1);
	for(auto i:v[x]) if(i^par) {
		depth[i]=depth[x]+1; init(i, x);
		rdist[x]=max(rdist[x], rdist[i]);
		if(rdist[i] > mx.f) mx.f=rdist[i], mx.s=1;
		else if(rdist[i] == mx.f) ++ mx.s;
		// fmx=max(fmx, fdist[i]);
		if(fdist[i] > fmx.f) fmx.f=fdist[i], fmx.s=1;
		else if(fdist[i] == fmx.f) ++ fmx.s;
	}
	ll rmx=0;
	for(auto i:v[x]) if(i^par) {
		if(fdist[i] ^ fmx.f) rmx=max(rmx, rdist[i]);
	}
	if(mx.s == 1 && fmx.f > rmx && fmx.s==1) fdist[x]=fmx.f;
}
ll ans[MAXN];
bool can(set<pi,greater<pi>> s) {
	if(s.empty()) return 1;
	ll x = s.begin()->f;
	ll times=1;
	s.erase(s.begin());
	for(auto i:s) {
		if(i.f == x) ++ times;
	}
	return times==1;
}
void dfs(ll x,ll par,ll dist,ll rup) {
	pi mx = pi(dist , 1);
	ll rmx=0;
	for(auto i:v[x]) if(i^par) {
		fdist[i] -= depth[x];
		if(fdist[i] > mx.f) mx.f = fdist[i], mx.s=1;
		else if(fdist[i] == mx.f) ++ mx.s;
		fdist[i] += depth[x];
		rmx=max(rmx, rdist[i]-depth[x]);
	}
	if(mx.f != dist) rmx=rup;
	for(auto i:v[x]) if(i^par) if(mx.f != fdist[i]-depth[x]) rmx=max(rmx, rdist[i]-depth[x]);
	// if(x==4) cerr<<mx.f<<' '<<mx.s<<' '<<rmx<<' '<<dist<<' '<<rup<<"\n";
	if(mx.s == 1 && mx.f > rmx) {
		ans[x] = 1;
	}
	multiset<ll,greater<ll>> ms;
	multiset<ll,greater<ll>> R;
	ms.ins(dist); R.ins(rup);
	for(auto i:v[x]) if(i^par) {
		ms.ins(fdist[i]-depth[x]);
		R.ins(rdist[i]-depth[x]);
	}
	while(ms.size() > 4) ms.erase(--ms.end());
	while(R.size() > 4) R.erase(--R.end());
	ll o=ms.size(); ll oR=R.size();
	for(auto i:v[x])if(i^par){
		if(*ms.begin() == fdist[i]-depth[x]) ms.erase(ms.begin());
		if(*R.begin() == rdist[i]-depth[x]) R.erase(R.begin());
		if(ms.size() >= 2 && *ms.begin() == *next(ms.begin()))
			dfs(i, x, 1, *R.begin() + 1);
		else 
			dfs(i, x, *(ms.begin()) + 1, *R.begin() + 1);
		if(siz(ms) ^ o) ms.ins(fdist[i]-depth[x]);
		if(siz(R) ^ oR) R.ins(rdist[i]-depth[x]);
	}
}
void comp(ll i) {
	for(auto j:v[i]) if(j^p[i]) comp(j);
	pi ans=pi(0, 1);
	bool gg=0;
	for(auto j:v[i]) if(j^p[i]) { gg |= (!F[j]);
		if(fdist[j] > ans.f) ans.f=fdist[j], ans.s=1;
		else if(fdist[j]==ans.f) ++ ans.s;
	} 
	F[i] = (ans.s == 1);
	if(gg) F[i]=0;
}
int main()
{
	FAST
	cin>>n>>m;
	FOR(i,0,n-2) {
		ll a, b; cin>>a>>b;
		v[a].eb(b), v[b].eb(a);
	}
	FOR(i,1,n) cin>>A[i];
	init(1, 1);
	// comp(1);
	FOR(i,1,n) {
		// cerr<<i<<": "<<fdist[i]<<"\n";
	}
	dfs(1, 1, 0, 0);
	FOR(i,1,n) cout<<ans[i]<<"\n";
}
/*
10 10
2 6
5 8
10 8
1 4
10 6
4 5
10 7
6 9
3 7
1 2 3 4 5 6 7 8 9 10
*/ 
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Incorrect 9 ms 5232 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 226 ms 13432 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 298 ms 17180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Incorrect 9 ms 5232 KB Output isn't correct
3 Halted 0 ms 0 KB -