/* I can do this all day */
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
typedef __int128 INT;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;
template < typename T > using Tree = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e6 + 10;
const int LOG = 20;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;
ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m, C[N], H[N], dp[N], mark[N], S[N], Ans[N], node[N];
set < int > st;
vector < int > G[N];
void pre(int v, int P = 0)
{
H[v] = H[P] + 1;
dp[v] = 1;
for(auto u : G[v])
{
if(u == P) continue;
pre(u, v);
dp[v] = max(dp[v], dp[u] + 1);
}
}
bool cmp(int i, int j)
{
return dp[i] < dp[j];
}
void dfs(int v, int P, int tp)
{
node[H[v]] = v;
vector < int > hist;
sort(all(G[v]), cmp);
int Mx1 = 0, Mx2 = 0, id1 = -1;
for(auto u : G[v])
{
if(u == P) continue;
if(dp[u] > Mx1)
{
Mx2 = Mx1;
Mx1 = dp[u];
id1 = u;
}
else if(dp[u] > Mx2)
{
Mx2 = dp[u];
}
}
while(SZ(st))
{
int cu = *st.begin();
cu = -cu;
int U = node[cu];
if(cu >= H[v] - Mx1)
{
hist.push_back(cu);
st.erase(st.begin());
mark[C[U]] = 0;
}
else
{
break;
}
}
if(S[v] == tp) Ans[v] += SZ(st);
if(!mark[C[v]])
{
mark[C[v]] = 1;
st.insert(-H[v]);
}
for(auto u : G[v])
{
if(u == P || u == id1) continue;
dfs(u, v, tp);
}
if(st.count(-H[v]))
{
st.erase(-H[v]);
mark[C[v]] = 0;
}
while(SZ(hist))
{
int cu = hist.back();
int U = node[cu];
if(cu >= H[v] - Mx2)
{
break;
}
mark[C[U]] = 1;
st.insert(-cu);
hist.pop_back();
}
if(!mark[C[v]])
{
mark[C[v]] = 1;
st.insert(-H[v]);
}
if(id1 != -1)
{
dfs(id1, v, tp);
}
if(st.count(-H[v]))
{
st.erase(-H[v]);
mark[C[v]] = 0;
}
while(SZ(hist))
{
int cu = hist.back();
int U = node[cu];
hist.pop_back();
st.insert(-cu);
mark[C[U]] = 1;
}
}
int fir[N], sec[N];
int main()
{
fast_io;
cin >> n >> m;
for(int i = 1; i < n; i ++)
{
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
for(int i = 1; i <= n; i ++)
{
cin >> C[i];
}
pre(1, 0);
int L = 0, R = 0;
for(int i = 1; i <= n; i ++)
{
if(H[i] > H[L]) L = i;
}
pre(L, 0);
///printf("L = %d\n", L);
for(int i = 1; i <= n; i ++)
{
fir[i] = H[i];
if(H[i] > H[R]) R = i;
///printf("i = %d H = %d dp = %d\n", i, H[i], dp[i]);
}
pre(R, 0);
for(int i = 1; i <= n; i ++)
{
sec[i] = H[i];
if(fir[i] > sec[i])
{
S[i] = 0;
}
else
{
S[i] = 1;
}
}
pre(L, 0);
///printf("R = %d\n", R);
dfs(L, 0, 0);
st.clear();
memset(mark, 0, sizeof mark);
pre(R, 0);
for(int i = 1; i <= n; i ++)
{
///printf("i = %d H = %d dp = %d\n", i, H[i], dp[i]);
}
dfs(R, 0, 1);
for(int i = 1; i <= n; i ++)
{
cout << Ans[i] << "\n";
}
return 0;
}
/* check corner case(n = 1?), watch for negetive index or overflow */
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
27648 KB |
Output is correct |
2 |
Correct |
19 ms |
27896 KB |
Output is correct |
3 |
Correct |
59 ms |
28996 KB |
Output is correct |
4 |
Correct |
51 ms |
28624 KB |
Output is correct |
5 |
Correct |
19 ms |
27872 KB |
Output is correct |
6 |
Correct |
20 ms |
28212 KB |
Output is correct |
7 |
Correct |
22 ms |
28048 KB |
Output is correct |
8 |
Correct |
19 ms |
27880 KB |
Output is correct |
9 |
Correct |
21 ms |
27976 KB |
Output is correct |
10 |
Correct |
21 ms |
27916 KB |
Output is correct |
11 |
Correct |
22 ms |
27864 KB |
Output is correct |
12 |
Correct |
21 ms |
27912 KB |
Output is correct |
13 |
Correct |
24 ms |
28284 KB |
Output is correct |
14 |
Correct |
21 ms |
28044 KB |
Output is correct |
15 |
Correct |
21 ms |
28052 KB |
Output is correct |
16 |
Correct |
21 ms |
27908 KB |
Output is correct |
17 |
Correct |
19 ms |
28104 KB |
Output is correct |
18 |
Correct |
20 ms |
28076 KB |
Output is correct |
19 |
Correct |
23 ms |
27908 KB |
Output is correct |
20 |
Correct |
201 ms |
32336 KB |
Output is correct |
21 |
Correct |
68 ms |
29004 KB |
Output is correct |
22 |
Correct |
20 ms |
27856 KB |
Output is correct |
23 |
Correct |
19 ms |
27984 KB |
Output is correct |
24 |
Correct |
21 ms |
27884 KB |
Output is correct |
25 |
Correct |
22 ms |
27852 KB |
Output is correct |
26 |
Correct |
20 ms |
27924 KB |
Output is correct |
27 |
Correct |
109 ms |
30076 KB |
Output is correct |
28 |
Correct |
80 ms |
29528 KB |
Output is correct |
29 |
Correct |
45 ms |
28536 KB |
Output is correct |
30 |
Correct |
20 ms |
27936 KB |
Output is correct |
31 |
Correct |
54 ms |
28852 KB |
Output is correct |
32 |
Correct |
73 ms |
29040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
36492 KB |
Output is correct |
2 |
Correct |
252 ms |
58328 KB |
Output is correct |
3 |
Correct |
45 ms |
32648 KB |
Output is correct |
4 |
Correct |
439 ms |
42984 KB |
Output is correct |
5 |
Correct |
495 ms |
81152 KB |
Output is correct |
6 |
Correct |
541 ms |
62784 KB |
Output is correct |
7 |
Correct |
386 ms |
43004 KB |
Output is correct |
8 |
Correct |
424 ms |
46772 KB |
Output is correct |
9 |
Correct |
450 ms |
45704 KB |
Output is correct |
10 |
Correct |
404 ms |
45536 KB |
Output is correct |
11 |
Correct |
216 ms |
43648 KB |
Output is correct |
12 |
Correct |
453 ms |
66836 KB |
Output is correct |
13 |
Correct |
410 ms |
61516 KB |
Output is correct |
14 |
Correct |
449 ms |
61208 KB |
Output is correct |
15 |
Correct |
204 ms |
43432 KB |
Output is correct |
16 |
Correct |
431 ms |
68696 KB |
Output is correct |
17 |
Correct |
461 ms |
62892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
351 ms |
40440 KB |
Output is correct |
2 |
Execution timed out |
2081 ms |
103104 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
27648 KB |
Output is correct |
2 |
Correct |
19 ms |
27896 KB |
Output is correct |
3 |
Correct |
59 ms |
28996 KB |
Output is correct |
4 |
Correct |
51 ms |
28624 KB |
Output is correct |
5 |
Correct |
19 ms |
27872 KB |
Output is correct |
6 |
Correct |
20 ms |
28212 KB |
Output is correct |
7 |
Correct |
22 ms |
28048 KB |
Output is correct |
8 |
Correct |
19 ms |
27880 KB |
Output is correct |
9 |
Correct |
21 ms |
27976 KB |
Output is correct |
10 |
Correct |
21 ms |
27916 KB |
Output is correct |
11 |
Correct |
22 ms |
27864 KB |
Output is correct |
12 |
Correct |
21 ms |
27912 KB |
Output is correct |
13 |
Correct |
24 ms |
28284 KB |
Output is correct |
14 |
Correct |
21 ms |
28044 KB |
Output is correct |
15 |
Correct |
21 ms |
28052 KB |
Output is correct |
16 |
Correct |
21 ms |
27908 KB |
Output is correct |
17 |
Correct |
19 ms |
28104 KB |
Output is correct |
18 |
Correct |
20 ms |
28076 KB |
Output is correct |
19 |
Correct |
23 ms |
27908 KB |
Output is correct |
20 |
Correct |
201 ms |
32336 KB |
Output is correct |
21 |
Correct |
68 ms |
29004 KB |
Output is correct |
22 |
Correct |
20 ms |
27856 KB |
Output is correct |
23 |
Correct |
19 ms |
27984 KB |
Output is correct |
24 |
Correct |
21 ms |
27884 KB |
Output is correct |
25 |
Correct |
22 ms |
27852 KB |
Output is correct |
26 |
Correct |
20 ms |
27924 KB |
Output is correct |
27 |
Correct |
109 ms |
30076 KB |
Output is correct |
28 |
Correct |
80 ms |
29528 KB |
Output is correct |
29 |
Correct |
45 ms |
28536 KB |
Output is correct |
30 |
Correct |
20 ms |
27936 KB |
Output is correct |
31 |
Correct |
54 ms |
28852 KB |
Output is correct |
32 |
Correct |
73 ms |
29040 KB |
Output is correct |
33 |
Correct |
202 ms |
36492 KB |
Output is correct |
34 |
Correct |
252 ms |
58328 KB |
Output is correct |
35 |
Correct |
45 ms |
32648 KB |
Output is correct |
36 |
Correct |
439 ms |
42984 KB |
Output is correct |
37 |
Correct |
495 ms |
81152 KB |
Output is correct |
38 |
Correct |
541 ms |
62784 KB |
Output is correct |
39 |
Correct |
386 ms |
43004 KB |
Output is correct |
40 |
Correct |
424 ms |
46772 KB |
Output is correct |
41 |
Correct |
450 ms |
45704 KB |
Output is correct |
42 |
Correct |
404 ms |
45536 KB |
Output is correct |
43 |
Correct |
216 ms |
43648 KB |
Output is correct |
44 |
Correct |
453 ms |
66836 KB |
Output is correct |
45 |
Correct |
410 ms |
61516 KB |
Output is correct |
46 |
Correct |
449 ms |
61208 KB |
Output is correct |
47 |
Correct |
204 ms |
43432 KB |
Output is correct |
48 |
Correct |
431 ms |
68696 KB |
Output is correct |
49 |
Correct |
461 ms |
62892 KB |
Output is correct |
50 |
Correct |
351 ms |
40440 KB |
Output is correct |
51 |
Execution timed out |
2081 ms |
103104 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |