Submission #113992

# Submission time Handle Problem Language Result Execution time Memory
113992 2019-05-29T12:55:54 Z RockyB Unique Cities (JOI19_ho_t5) C++17
0 / 100
2000 ms 18512 KB
/// In The Name Of God

#include <bits/stdc++.h>

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)

#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define nl '\n'
#define ioi exit(0);

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int N = (int)5e5 + 7;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;

const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

using namespace std;

int n, m;
int c[N];
vector <int> g[N];

int A, B;
int mx;
void dfs(int v, int p, int dis, int &D) {
	if (dis > mx) {
		mx = dis;
		D = v;
	}
	for (auto to : g[v]) {
		if (to != p) {
			dfs(to, v, dis + 1, D);
		}
	}
}

int dp[N];
set <int> col;
void pre(int v, int p = -1) {
	dp[v] = 0;
	for (auto to : g[v]) {
		if (to != p) {
			pre(to, v);
			dp[v] = max(dp[v], dp[to] + 1);
		}
	}
}
void go(int v, int p = -1, int ost = 0) {
	if (ost < 0) {
		col.insert(c[v]);
		return;
	}
	pair <int, int> mx = {-1, -1};
	for (auto to : g[v]) {
		if (to != p) {
			if (dp[to] >= mx.f) {
				mx.s = mx.f;
				mx.f = dp[to];
			}
			else if (dp[to] > mx.s) {
				mx.s = dp[to];
			}
		}
	}
	if (mx.f == mx.s) return;
	for (auto to : g[v]) {
		if (to != p) {
			if (dp[to] == mx.f) go(to, v, max(ost - 1, mx.s));
		}
	}
}
void solve(int v) {
	pre(v);
	col.clear();
	go(v);
	cout << sz(col) << nl;
}
int main() {
	#ifdef IOI2018
		freopen ("in.txt", "r", stdin);
	#endif
	Kazakhstan
	// --- reading input
	cin >> n >> m;
	rep(i, 2, n) {
		int v, u;
		cin >> v >> u;
		g[v].pb(u);
		g[u].pb(v);
	}
	rep(i, 1, n) cin >> c[i];
	// --- finding diameter
	dfs(1, 0, 0, A);
	mx = 0;
	dfs(A, 0, 0, B);
	// --- solving part
	rep(i, 1, n) {
		solve(i);
	}
	ioi
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12160 KB Output is correct
2 Incorrect 72 ms 12160 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2057 ms 16632 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2047 ms 18512 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12160 KB Output is correct
2 Incorrect 72 ms 12160 KB Output isn't correct
3 Halted 0 ms 0 KB -