#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.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
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5240 KB |
Output is correct |
3 |
Correct |
7 ms |
5240 KB |
Output is correct |
4 |
Incorrect |
8 ms |
5240 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
13792 KB |
Output is correct |
2 |
Correct |
197 ms |
23256 KB |
Output is correct |
3 |
Incorrect |
38 ms |
8312 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
268 ms |
18296 KB |
Output is correct |
2 |
Correct |
473 ms |
37708 KB |
Output is correct |
3 |
Incorrect |
41 ms |
9080 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5240 KB |
Output is correct |
3 |
Correct |
7 ms |
5240 KB |
Output is correct |
4 |
Incorrect |
8 ms |
5240 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |