답안 #130099

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
130099 2019-07-14T00:00:19 Z qkxwsm Unique Cities (JOI19_ho_t5) C++14
0 / 100
471 ms 34084 KB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 200013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, M, S, T, D;
int arr[MAXN];
vi edge[MAXN];
int depth[MAXN], h[MAXN];
int dist[2][MAXN];
int freq[MAXN];
int ans[2][MAXN];
int stor;
vi cur;

void ins(int u)
{
	cur.PB(u);
	if (freq[arr[u]]) stor--;
	freq[arr[u]]++;
	stor++;
}
void del()
{
	int u = cur.back(); cur.pop_back();
	stor--;
	freq[arr[u]]--;
	if (freq[arr[u]]) stor++;
}
void dfs(int u, int p)
{
	h[u] = 0;
	for (int v : edge[u])
	{
		if (v == p) continue;
		depth[v] = depth[u] + 1;
		dfs(v, u);
		ckmax(h[u], h[v] + 1);
	}
}
void solve(int u, int p, bool f)
{
	int mx2 = -1, c = -1;
	for (int v : edge[u])
	{
		if (v == p) continue;
		if (h[v] == h[u] - 1)
		{
			c = v;
			break;
		}
	}
	for (int v : edge[u])
	{
		if (v == p || v == c) continue;
		ckmax(mx2, h[v] + 1);
	}
	if (c != -1)
	{
		while(!cur.empty() && depth[cur.back()] >= depth[u] - mx2) del();
		ins(u);
		solve(c, u, f);
	}
	for (int v : edge[u])
	{
		if (v == p || v == c) continue;
		while(!cur.empty() && depth[cur.back()] >= depth[u] - h[u]) del();
		if (cur.empty() || cur.back() != u) ins(u);
		solve(v, u, f);
	}
	while(!cur.empty() && depth[cur.back()] >= depth[u] - h[u]) del();
	ans[f][u] = stor;
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> M;
	FOR(i, 0, N - 1)
	{
		int u, v; cin >> u >> v; u--; v--;
		edge[u].PB(v);
		edge[v].PB(u);
	}
	FOR(i, 0, N)
	{
		cin >> arr[i]; arr[i]--;
	}
	depth[0] = 0;
	dfs(0, N);
	FOR(i, 0, N)
	{
		if (depth[i] > depth[S]) S = i;
	}
	depth[S] = 0;
	dfs(S, N);
	FOR(i, 0, N)
	{
		if (depth[i] > depth[T]) T = i;
	}
	FOR(i, 0, N)
	{
		dist[0][i] = depth[i];
	}
	// cerr << "S = " << S << endl;
	solve(S, N, 0);
	while(!cur.empty()) del();
	depth[T] = 0;
	dfs(T, N);
	FOR(i, 0, N)
	{
		dist[1][i] = depth[i];
	}
	solve(T, N, 1);
	FOR(i, 0, N)
	{
		// cerr << dist[0][i] << ' ' << dist[1][i] << ' ' << ans[0][i] << ' ' << ans[1][i] << endl;
 		if (dist[0][i] == dist[1][i])
		{
			cout << "0\n";
			continue;
		}
		cout << (dist[0][i] > dist[1][i] ? ans[0][i] : ans[1][i]) << '\n';
	}
	return 0;
	//then what do you do? dfsfrom S≤ and maintain unique cities
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5112 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Incorrect 7 ms 5240 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 161 ms 12116 KB Output is correct
2 Correct 235 ms 21616 KB Output is correct
3 Incorrect 34 ms 8008 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 15352 KB Output is correct
2 Correct 471 ms 34084 KB Output is correct
3 Incorrect 39 ms 8440 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5112 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Incorrect 7 ms 5240 KB Output isn't correct
5 Halted 0 ms 0 KB -