답안 #154324

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
154324 2019-09-20T16:25:01 Z ryansee Unique Cities (JOI19_ho_t5) C++14
4 / 100
1127 ms 274436 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 <pi, ll> 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; pi roots;
vector<int> v[MAXN];
int A[MAXN], p[MAXN][21]; int st[MAXN], en[MAXN], co;
// pi fdist[MAXN], rdist[MAXN];
ll depth[MAXN];
void init(ll x,ll par) { p[x][0]=par; st[x]=co++;
	// rdist[x] = fdist[x] = pi(depth[x], x); 
	// spi mx=spi(pi(depth[x], x), 1);
	// spi fmx=spi(pi(depth[x], 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].f > mx.f.f) mx.f=rdist[i], mx.s=1;
		// else if(rdist[i].f == mx.f.f) ++ mx.s;
		// fmx=max(fmx, fdist[i]);
		// if(fdist[i].f > fmx.f.f) fmx.f=fdist[i], fmx.s=1;
		// else if(fdist[i].f == fmx.f.f) ++ fmx.s;
	}
	// ll rmx=-1;
	// for(auto i:v[x]) if(i^par) {
		// if(fdist[i].f != fmx.f.f) rmx=max(rmx, rdist[i].f);
	// }
	// if(mx.s == 1 && fmx.f.f > rmx && fmx.s==1) fdist[x]=fmx.f; 
	en[x]=co;
}
void t2k() { FOR(j,1,20) FOR(i,1,n) p[i][j]=p[p[i][j-1]][j-1]; }
bool isp(ll a,ll b){return st[a]<=st[b]&&en[a]>=en[b];}
ll lca(ll x,ll y) {
	if(isp(x,y))return x; if(isp(y,x))return y;
	for(ll i=20;i>=0;--i) {
		if(!isp(p[x][i],y))x=p[x][i];
	}
	return p[x][0];
}
// ll ans[MAXN]; 
ll get_dist(ll a,ll b) { return depth[a]+depth[b]-2*depth[lca(a,b)]; }
struct diam_tree {
	int root;
	vector<ll> ans;
	vector <pi> dq;
	ll cur=0;
	ll mp[MAXN];
	ll rdist[MAXN], depth[MAXN];
	void add(ll x, ll d) {
		// cerr<<x<<" in\n";
		if(!mp[x]) ++ cur;
		++ mp[x];
		dq.eb(x, d);
	}
	void del() {
		// cerr<<dq.back().f<<" out\n";
		ll x=dq.back().f;
		-- mp[x];
		if(mp[x] == 0) -- cur;
		dq.pop_back();
	}
	void main() {
		ans.resize(n+1, 0); mmst(mp,0); dq.clear(); mmst(depth,0); mmst(rdist,0);
		assert(root);
		cur=0;
		pre(root, root); dfs(root, root); //cerr<<"r: "<<root<<"\n";
		// FOR(i,1,n) cerr<<i<<": "<<ans[i]<<"\n";
	}
	void dfs(ll x,ll p) {
// 		add(A[x], depth[x]);
		// ans[x]=cur;
		pi one, two;
		one=pi(-1, -1);
		two=pi(-1, -1);
		for(auto i:v[x]) if(i^p) {
		    two=max(two, pi(rdist[i]-depth[x], i));
		    if(two > one) swap(one, two);
		}
		vector<pi> cabbage;
		if(one.f!=-1) {
			ll until=depth[x]-one.f;
// 			del();
			while(dq.size() && dq.back().s >= until) cabbage.pb(dq.back()), del();
// 			add(A[x], depth[x]);
		}
		ans[x]=cur;
		// reverse(all(cabbage));
		// del();
		// for(auto i:cabbage) add(i.f,i.s);
		add(A[x],depth[x]);
		for(auto i:v[x]) if(i^p) {
			if(one.s == i) continue;
			// vector<pi> cabbage;
			// if(pq.size()) {
				// ll until=depth[x]-pq.top().f;
				// assert(dq.back().f==A[x]);
				// del();
				// while(dq.size() && dq.back().s >= until) cabbage.pb(dq.back()), del();
				// add(A[x], depth[x]);
			// }
			dfs(i, x);
			// reverse(all(cabbage)); del();
			// for(auto i:cabbage) add(i.f, i.s);
			// add(A[x], depth[x]);
		}
		reverse(all(cabbage));
		del();
		for(auto i:cabbage) add(i.f,i.s);
		add(A[x],depth[x]);
		for(auto i:v[x]) if((i^p) && i==one.s) {
			vector<pi> cabbage;
			if(~two.f) {
				ll until=depth[x]-two.f;
				// assert(dq.back().f==A[x]);
				del();
				while(dq.size() && dq.back().s >= until) cabbage.pb(dq.back()), del();
				add(A[x], depth[x]);
			}
			dfs(i, x);
			reverse(all(cabbage)); del();
			for(auto i:cabbage) add(i.f, i.s);
			add(A[x], depth[x]);
			break;
		}
// 		assert(dq.back().f==A[x]);
		del();
	}
	void pre(ll x,ll p) {
		rdist[x]=depth[x];
		for(auto i:v[x]) if(i^p){
			depth[i]=depth[x]+1;
			pre(i,x);
			rdist[x]=max(rdist[x],rdist[i]);
		}
	}
} r1, r2;
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;
}
bool decide(ll x){
	if(get_dist(x, roots.f) >= get_dist(x, roots.s)) return 1;
	else return 0;
}
/*void dfs(ll x,ll par,pi dist,pi rup) {
	spi mx = spi(dist, 1);
	ll rmx=0;
	for(auto i:v[x]) if(i^par) {
		fdist[i].f -= depth[x];
		if(fdist[i].f > mx.f.f) mx.f = fdist[i], mx.s=1;
		else if(fdist[i].f == mx.f.f) ++ mx.s;
		fdist[i].f += depth[x];
		// rmx=max(rmx, rdist[i].f-depth[x]);
	}
	if(mx.f.f != dist.f) rmx=rup.f;
	for(auto i:v[x]) if(i^par) if(mx.f.f != fdist[i].f-depth[x]) rmx=max(rmx, rdist[i].f-depth[x]);
	// if(x==3) cerr<<mx.f<<' '<<mx.s<<' '<<rmx<<' '<<dist<<' '<<rup<<"\n";
	if(mx.s == 1 && mx.f.f > rmx) {
		// cerr<<x<<' '<<mx.f.s<<' '<<get_dist(x, mx.f.s)<<' '<<rmx<<' '<<dist.s<<'\n';
		// cerr<<x<<' '<<decide(x)<<'\n';
		if(decide(x)) ans[x]=r1.ans[x];
		else ans[x]=r2.ans[x];
		// ans[x]=1;
	}
	set<spi,greater<spi>> ms;
	multiset<pi,greater<pi>> R;
	ms.ins(spi(dist, x)); R.ins(rup);
	for(auto i:v[x]) if(i^par) {
		ms.ins(spi(pi(fdist[i].f-depth[x], fdist[i].s), i));
		R.ins(pi(rdist[i].f-depth[x],rdist[i].s));
	}
	// 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){
		ms.erase(spi(pi(fdist[i].f-depth[x], fdist[i].s), i));
		if(ms.begin()->s != x) R.erase(R.find(pi(rdist[ms.begin()->s].f-depth[x],rdist[ms.begin()->s].s))); else R.erase(R.find(rup));
		if(*R.begin() == pi(rdist[i].f-depth[x], rdist[i].s)) R.erase(R.begin());
		if((ms.size() >= 2 && ms.begin()->f.f == next(ms.begin())->f.f) || (R.size()&&ms.begin()->f.f <= R.begin()->f)) {
			if(ms.begin()->s != x) R.ins(pi(rdist[ms.begin()->s].f-depth[x],rdist[ms.begin()->s].s));
			else R.ins(rup);
			dfs(i, x, pi(1,x), pi(R.begin()->f + 1, R.begin()->s));
		} else {
			if(ms.begin()->s != x) R.ins(pi(rdist[ms.begin()->s].f-depth[x],rdist[ms.begin()->s].s));
			else R.ins(rup);
			dfs(i, x, pi((ms.begin()->f.f) + 1, ms.begin()->f.s), pi(R.begin()->f + 1, R.begin()->s));
		}
		if(siz(ms) ^ o) ms.ins(spi(pi(fdist[i].f-depth[x],fdist[i].s),i));
		if(siz(R) ^ oR) R.ins(pi(rdist[i].f-depth[x],rdist[i].s));
	}
}*/
struct diam_find {
	vector<ll> dist;
	pi main() {
		dist.resize(n+1, -1);
		dist[1]=0; dfs(1, 1);
		ll r1=1, r2=-1;
		FOR(i,1,n) if(dist[i] > dist[r1]) r1=i;
		r2=r1;
		dist[r1]=0; dfs(r1, r1);
		FOR(i,1,n) if(dist[i] > dist[r2]) r2=i;
		return pi(r1, r2);
	}
	void dfs(ll x,ll p) {
		for(auto i:v[x]) if(i^p) dist[i]=dist[x]+1, dfs(i, x);
	}
} fd;
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];
	roots=fd.main(); init(1, 1); t2k();
	r1.root=roots.f, r2.root=roots.s; r1.main(); r2.main();
	// comp(1);
	FOR(i,1,n) {
		// cerr<<i<<": "<<fdist[i]<<"\n";
	}
	// dfs(1, 1, pi(0,1), pi(0,1));
	FOR(i,1,n) cout<<(decide(i)?r1.ans[i]:r2.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
*/

Compilation message

joi2019_ho_t5.cpp: In function 'int lca(int, int)':
joi2019_ho_t5.cpp:60:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  if(isp(x,y))return x; if(isp(y,x))return y;
  ^~
joi2019_ho_t5.cpp:60:24: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  if(isp(x,y))return x; if(isp(y,x))return y;
                        ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 9724 KB Output is correct
2 Correct 14 ms 10104 KB Output is correct
3 Correct 27 ms 12620 KB Output is correct
4 Correct 29 ms 12004 KB Output is correct
5 Correct 14 ms 10136 KB Output is correct
6 Correct 72 ms 21160 KB Output is correct
7 Correct 31 ms 12312 KB Output is correct
8 Correct 14 ms 10104 KB Output is correct
9 Correct 14 ms 10104 KB Output is correct
10 Correct 14 ms 10196 KB Output is correct
11 Correct 14 ms 10104 KB Output is correct
12 Correct 13 ms 10104 KB Output is correct
13 Correct 64 ms 19948 KB Output is correct
14 Correct 18 ms 11128 KB Output is correct
15 Correct 19 ms 10800 KB Output is correct
16 Correct 13 ms 10104 KB Output is correct
17 Correct 38 ms 15592 KB Output is correct
18 Correct 31 ms 12284 KB Output is correct
19 Correct 16 ms 10104 KB Output is correct
20 Correct 81 ms 21160 KB Output is correct
21 Correct 32 ms 12352 KB Output is correct
22 Correct 14 ms 10104 KB Output is correct
23 Correct 14 ms 10104 KB Output is correct
24 Correct 14 ms 10104 KB Output is correct
25 Correct 14 ms 10104 KB Output is correct
26 Correct 13 ms 10104 KB Output is correct
27 Correct 38 ms 14968 KB Output is correct
28 Correct 32 ms 13944 KB Output is correct
29 Correct 23 ms 11452 KB Output is correct
30 Correct 14 ms 10104 KB Output is correct
31 Correct 39 ms 15592 KB Output is correct
32 Correct 32 ms 12284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 331 ms 28152 KB Output is correct
2 Runtime error 981 ms 274436 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 588 ms 35556 KB Output is correct
2 Runtime error 1127 ms 274432 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 9724 KB Output is correct
2 Correct 14 ms 10104 KB Output is correct
3 Correct 27 ms 12620 KB Output is correct
4 Correct 29 ms 12004 KB Output is correct
5 Correct 14 ms 10136 KB Output is correct
6 Correct 72 ms 21160 KB Output is correct
7 Correct 31 ms 12312 KB Output is correct
8 Correct 14 ms 10104 KB Output is correct
9 Correct 14 ms 10104 KB Output is correct
10 Correct 14 ms 10196 KB Output is correct
11 Correct 14 ms 10104 KB Output is correct
12 Correct 13 ms 10104 KB Output is correct
13 Correct 64 ms 19948 KB Output is correct
14 Correct 18 ms 11128 KB Output is correct
15 Correct 19 ms 10800 KB Output is correct
16 Correct 13 ms 10104 KB Output is correct
17 Correct 38 ms 15592 KB Output is correct
18 Correct 31 ms 12284 KB Output is correct
19 Correct 16 ms 10104 KB Output is correct
20 Correct 81 ms 21160 KB Output is correct
21 Correct 32 ms 12352 KB Output is correct
22 Correct 14 ms 10104 KB Output is correct
23 Correct 14 ms 10104 KB Output is correct
24 Correct 14 ms 10104 KB Output is correct
25 Correct 14 ms 10104 KB Output is correct
26 Correct 13 ms 10104 KB Output is correct
27 Correct 38 ms 14968 KB Output is correct
28 Correct 32 ms 13944 KB Output is correct
29 Correct 23 ms 11452 KB Output is correct
30 Correct 14 ms 10104 KB Output is correct
31 Correct 39 ms 15592 KB Output is correct
32 Correct 32 ms 12284 KB Output is correct
33 Correct 331 ms 28152 KB Output is correct
34 Runtime error 981 ms 274436 KB Execution killed with signal 9 (could be triggered by violating memory limits)
35 Halted 0 ms 0 KB -