/// 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]);
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
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12160 KB |
Output is correct |
2 |
Correct |
75 ms |
12280 KB |
Output is correct |
3 |
Correct |
84 ms |
12288 KB |
Output is correct |
4 |
Correct |
94 ms |
12288 KB |
Output is correct |
5 |
Correct |
87 ms |
12408 KB |
Output is correct |
6 |
Correct |
196 ms |
12424 KB |
Output is correct |
7 |
Correct |
89 ms |
12288 KB |
Output is correct |
8 |
Correct |
79 ms |
12280 KB |
Output is correct |
9 |
Correct |
104 ms |
12160 KB |
Output is correct |
10 |
Correct |
109 ms |
12152 KB |
Output is correct |
11 |
Correct |
102 ms |
12160 KB |
Output is correct |
12 |
Correct |
47 ms |
12252 KB |
Output is correct |
13 |
Correct |
200 ms |
12512 KB |
Output is correct |
14 |
Correct |
113 ms |
12412 KB |
Output is correct |
15 |
Correct |
94 ms |
12280 KB |
Output is correct |
16 |
Correct |
41 ms |
12416 KB |
Output is correct |
17 |
Correct |
118 ms |
12288 KB |
Output is correct |
18 |
Correct |
82 ms |
12288 KB |
Output is correct |
19 |
Correct |
106 ms |
12160 KB |
Output is correct |
20 |
Correct |
427 ms |
12416 KB |
Output is correct |
21 |
Correct |
123 ms |
12288 KB |
Output is correct |
22 |
Correct |
83 ms |
12160 KB |
Output is correct |
23 |
Correct |
109 ms |
12320 KB |
Output is correct |
24 |
Correct |
100 ms |
12280 KB |
Output is correct |
25 |
Correct |
111 ms |
12288 KB |
Output is correct |
26 |
Correct |
47 ms |
12412 KB |
Output is correct |
27 |
Correct |
327 ms |
12532 KB |
Output is correct |
28 |
Correct |
203 ms |
12480 KB |
Output is correct |
29 |
Correct |
94 ms |
12408 KB |
Output is correct |
30 |
Correct |
39 ms |
12284 KB |
Output is correct |
31 |
Correct |
167 ms |
12408 KB |
Output is correct |
32 |
Correct |
97 ms |
12408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2048 ms |
18424 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2027 ms |
21288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12160 KB |
Output is correct |
2 |
Correct |
75 ms |
12280 KB |
Output is correct |
3 |
Correct |
84 ms |
12288 KB |
Output is correct |
4 |
Correct |
94 ms |
12288 KB |
Output is correct |
5 |
Correct |
87 ms |
12408 KB |
Output is correct |
6 |
Correct |
196 ms |
12424 KB |
Output is correct |
7 |
Correct |
89 ms |
12288 KB |
Output is correct |
8 |
Correct |
79 ms |
12280 KB |
Output is correct |
9 |
Correct |
104 ms |
12160 KB |
Output is correct |
10 |
Correct |
109 ms |
12152 KB |
Output is correct |
11 |
Correct |
102 ms |
12160 KB |
Output is correct |
12 |
Correct |
47 ms |
12252 KB |
Output is correct |
13 |
Correct |
200 ms |
12512 KB |
Output is correct |
14 |
Correct |
113 ms |
12412 KB |
Output is correct |
15 |
Correct |
94 ms |
12280 KB |
Output is correct |
16 |
Correct |
41 ms |
12416 KB |
Output is correct |
17 |
Correct |
118 ms |
12288 KB |
Output is correct |
18 |
Correct |
82 ms |
12288 KB |
Output is correct |
19 |
Correct |
106 ms |
12160 KB |
Output is correct |
20 |
Correct |
427 ms |
12416 KB |
Output is correct |
21 |
Correct |
123 ms |
12288 KB |
Output is correct |
22 |
Correct |
83 ms |
12160 KB |
Output is correct |
23 |
Correct |
109 ms |
12320 KB |
Output is correct |
24 |
Correct |
100 ms |
12280 KB |
Output is correct |
25 |
Correct |
111 ms |
12288 KB |
Output is correct |
26 |
Correct |
47 ms |
12412 KB |
Output is correct |
27 |
Correct |
327 ms |
12532 KB |
Output is correct |
28 |
Correct |
203 ms |
12480 KB |
Output is correct |
29 |
Correct |
94 ms |
12408 KB |
Output is correct |
30 |
Correct |
39 ms |
12284 KB |
Output is correct |
31 |
Correct |
167 ms |
12408 KB |
Output is correct |
32 |
Correct |
97 ms |
12408 KB |
Output is correct |
33 |
Execution timed out |
2048 ms |
18424 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |