#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
//#define left oooooopss
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll b,ll e,ll m){
b%=m;
ll ans = 1;
for (; e; b = b * b % m, e /= 2)
if (e & 1) ans = ans * b % m;
return ans;
}
// debug:
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr<<"(" << #__VA_ARGS__<<"):" , dbg_out(__VA_ARGS__) , cerr << endl
#else
#define dbg(...) 42
#endif
//
const int N = 500100;
vi g[N];
int S[N];
int p[N];
int cnt[N];
int f(int x){return p[x] = (p[x]==x?x:f(p[x]));}
void join(int a,int b){
p[f(a)]=f(b);
}
pii e[N];
void dfs(int v,int p,map<int,int>&mp){
if(cnt[S[v]]!=1)
mp[S[v]] = 1;
for(auto to : g[v])if(to!=p){
map<int,int> nv;
dfs(to,v,nv);
if(sz(nv) > sz(mp))mp.swap(nv);
for(auto it : nv){
mp[it.ff]+=it.ss;
if(mp[it.ff] == cnt[it.ff]){
// todo mundo na subarvore. Cor n importa mais
mp.erase(it.ff);
}
}
}
if(mp.size()!=0 && p!=-1){
// alguem não ta dentro.
// pode juntar com o pai
join(v,p);
}
}
int deg[N];
int32_t main(){
fastio;
int n,k;
cin >> n >> k;
rep(i,0,n-1){
int a,b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
e[i] = pii(a,b);
}
rep(i,1,n+1)p[i]=i;
rep(i,1,n+1)cin >> S[i],cnt[S[i]]++;
map<int,int> mp;
dfs(1,0,mp);
rep(i,0,n-1){
if(f(e[i].ff)!=f(e[i].ss)){
deg[f(e[i].ff)]++;
deg[f(e[i].ss)]++;
}
}
int res=0;
set<int> used;
rep(i,1,n+1)used.insert(f(i));
for(int i : used)
if(deg[i]<=1)res++;
if(res<=1)res=0;
cout << (res + 1)/2 << endl;
// math -> gcd it all
// Did u check N=1? Did you switch N,M?
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12076 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12080 KB |
Output is correct |
7 |
Correct |
7 ms |
12076 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12088 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
12080 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
12108 KB |
Output is correct |
14 |
Correct |
7 ms |
12080 KB |
Output is correct |
15 |
Correct |
7 ms |
12068 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
8 ms |
12080 KB |
Output is correct |
18 |
Correct |
9 ms |
12116 KB |
Output is correct |
19 |
Correct |
9 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
11988 KB |
Output is correct |
21 |
Correct |
7 ms |
12116 KB |
Output is correct |
22 |
Correct |
12 ms |
12040 KB |
Output is correct |
23 |
Correct |
7 ms |
12004 KB |
Output is correct |
24 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12076 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12080 KB |
Output is correct |
7 |
Correct |
7 ms |
12076 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12088 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
12080 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
12108 KB |
Output is correct |
14 |
Correct |
7 ms |
12080 KB |
Output is correct |
15 |
Correct |
7 ms |
12068 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
8 ms |
12080 KB |
Output is correct |
18 |
Correct |
9 ms |
12116 KB |
Output is correct |
19 |
Correct |
9 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
11988 KB |
Output is correct |
21 |
Correct |
7 ms |
12116 KB |
Output is correct |
22 |
Correct |
12 ms |
12040 KB |
Output is correct |
23 |
Correct |
7 ms |
12004 KB |
Output is correct |
24 |
Correct |
7 ms |
11988 KB |
Output is correct |
25 |
Correct |
7 ms |
11988 KB |
Output is correct |
26 |
Correct |
8 ms |
12372 KB |
Output is correct |
27 |
Correct |
9 ms |
12348 KB |
Output is correct |
28 |
Correct |
9 ms |
12604 KB |
Output is correct |
29 |
Correct |
9 ms |
12348 KB |
Output is correct |
30 |
Correct |
9 ms |
12220 KB |
Output is correct |
31 |
Correct |
7 ms |
12076 KB |
Output is correct |
32 |
Correct |
8 ms |
12756 KB |
Output is correct |
33 |
Correct |
7 ms |
11988 KB |
Output is correct |
34 |
Correct |
9 ms |
12244 KB |
Output is correct |
35 |
Correct |
8 ms |
12404 KB |
Output is correct |
36 |
Correct |
18 ms |
12244 KB |
Output is correct |
37 |
Correct |
9 ms |
12244 KB |
Output is correct |
38 |
Correct |
7 ms |
11988 KB |
Output is correct |
39 |
Correct |
10 ms |
12348 KB |
Output is correct |
40 |
Correct |
8 ms |
12244 KB |
Output is correct |
41 |
Correct |
8 ms |
12244 KB |
Output is correct |
42 |
Correct |
9 ms |
12372 KB |
Output is correct |
43 |
Correct |
9 ms |
13012 KB |
Output is correct |
44 |
Correct |
7 ms |
12076 KB |
Output is correct |
45 |
Correct |
8 ms |
12348 KB |
Output is correct |
46 |
Correct |
9 ms |
12340 KB |
Output is correct |
47 |
Correct |
7 ms |
12024 KB |
Output is correct |
48 |
Correct |
8 ms |
12244 KB |
Output is correct |
49 |
Correct |
8 ms |
12372 KB |
Output is correct |
50 |
Correct |
9 ms |
12600 KB |
Output is correct |
51 |
Correct |
8 ms |
12244 KB |
Output is correct |
52 |
Correct |
23 ms |
12220 KB |
Output is correct |
53 |
Correct |
9 ms |
12260 KB |
Output is correct |
54 |
Correct |
10 ms |
12304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12076 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12080 KB |
Output is correct |
7 |
Correct |
7 ms |
12076 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12088 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
12080 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
12108 KB |
Output is correct |
14 |
Correct |
7 ms |
12080 KB |
Output is correct |
15 |
Correct |
7 ms |
12068 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
8 ms |
12080 KB |
Output is correct |
18 |
Correct |
9 ms |
12116 KB |
Output is correct |
19 |
Correct |
9 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
11988 KB |
Output is correct |
21 |
Correct |
7 ms |
12116 KB |
Output is correct |
22 |
Correct |
12 ms |
12040 KB |
Output is correct |
23 |
Correct |
7 ms |
12004 KB |
Output is correct |
24 |
Correct |
7 ms |
11988 KB |
Output is correct |
25 |
Correct |
7 ms |
11988 KB |
Output is correct |
26 |
Correct |
84 ms |
18576 KB |
Output is correct |
27 |
Correct |
89 ms |
18256 KB |
Output is correct |
28 |
Correct |
9 ms |
12320 KB |
Output is correct |
29 |
Correct |
7 ms |
12004 KB |
Output is correct |
30 |
Correct |
8 ms |
12048 KB |
Output is correct |
31 |
Correct |
98 ms |
18456 KB |
Output is correct |
32 |
Correct |
8 ms |
12244 KB |
Output is correct |
33 |
Correct |
85 ms |
41420 KB |
Output is correct |
34 |
Correct |
104 ms |
18372 KB |
Output is correct |
35 |
Correct |
8 ms |
12220 KB |
Output is correct |
36 |
Correct |
69 ms |
21068 KB |
Output is correct |
37 |
Correct |
8 ms |
12244 KB |
Output is correct |
38 |
Correct |
9 ms |
12244 KB |
Output is correct |
39 |
Correct |
52 ms |
18544 KB |
Output is correct |
40 |
Correct |
8 ms |
13012 KB |
Output is correct |
41 |
Correct |
70 ms |
18128 KB |
Output is correct |
42 |
Correct |
86 ms |
25888 KB |
Output is correct |
43 |
Correct |
11 ms |
12088 KB |
Output is correct |
44 |
Correct |
102 ms |
42360 KB |
Output is correct |
45 |
Correct |
90 ms |
31544 KB |
Output is correct |
46 |
Correct |
9 ms |
12244 KB |
Output is correct |
47 |
Correct |
17 ms |
12244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
17324 KB |
Output is correct |
2 |
Correct |
69 ms |
24388 KB |
Output is correct |
3 |
Correct |
9 ms |
12372 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
7 ms |
12088 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
8 ms |
12220 KB |
Output is correct |
8 |
Correct |
100 ms |
19728 KB |
Output is correct |
9 |
Correct |
8 ms |
12244 KB |
Output is correct |
10 |
Correct |
88 ms |
18840 KB |
Output is correct |
11 |
Correct |
7 ms |
12080 KB |
Output is correct |
12 |
Correct |
83 ms |
18884 KB |
Output is correct |
13 |
Correct |
84 ms |
19976 KB |
Output is correct |
14 |
Correct |
80 ms |
24116 KB |
Output is correct |
15 |
Correct |
61 ms |
18580 KB |
Output is correct |
16 |
Correct |
10 ms |
12372 KB |
Output is correct |
17 |
Correct |
7 ms |
12116 KB |
Output is correct |
18 |
Correct |
81 ms |
23448 KB |
Output is correct |
19 |
Correct |
119 ms |
34436 KB |
Output is correct |
20 |
Correct |
9 ms |
12244 KB |
Output is correct |
21 |
Correct |
7 ms |
12116 KB |
Output is correct |
22 |
Correct |
94 ms |
20308 KB |
Output is correct |
23 |
Correct |
8 ms |
12220 KB |
Output is correct |
24 |
Correct |
81 ms |
19276 KB |
Output is correct |
25 |
Correct |
96 ms |
30928 KB |
Output is correct |
26 |
Correct |
8 ms |
12372 KB |
Output is correct |
27 |
Correct |
9 ms |
12600 KB |
Output is correct |
28 |
Correct |
8 ms |
12216 KB |
Output is correct |
29 |
Correct |
8 ms |
12228 KB |
Output is correct |
30 |
Correct |
9 ms |
12244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12076 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12080 KB |
Output is correct |
7 |
Correct |
7 ms |
12076 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12088 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
12080 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
12108 KB |
Output is correct |
14 |
Correct |
7 ms |
12080 KB |
Output is correct |
15 |
Correct |
7 ms |
12068 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
8 ms |
12080 KB |
Output is correct |
18 |
Correct |
9 ms |
12116 KB |
Output is correct |
19 |
Correct |
9 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
11988 KB |
Output is correct |
21 |
Correct |
7 ms |
12116 KB |
Output is correct |
22 |
Correct |
12 ms |
12040 KB |
Output is correct |
23 |
Correct |
7 ms |
12004 KB |
Output is correct |
24 |
Correct |
7 ms |
11988 KB |
Output is correct |
25 |
Correct |
7 ms |
11988 KB |
Output is correct |
26 |
Correct |
8 ms |
12372 KB |
Output is correct |
27 |
Correct |
9 ms |
12348 KB |
Output is correct |
28 |
Correct |
9 ms |
12604 KB |
Output is correct |
29 |
Correct |
9 ms |
12348 KB |
Output is correct |
30 |
Correct |
9 ms |
12220 KB |
Output is correct |
31 |
Correct |
7 ms |
12076 KB |
Output is correct |
32 |
Correct |
8 ms |
12756 KB |
Output is correct |
33 |
Correct |
7 ms |
11988 KB |
Output is correct |
34 |
Correct |
9 ms |
12244 KB |
Output is correct |
35 |
Correct |
8 ms |
12404 KB |
Output is correct |
36 |
Correct |
18 ms |
12244 KB |
Output is correct |
37 |
Correct |
9 ms |
12244 KB |
Output is correct |
38 |
Correct |
7 ms |
11988 KB |
Output is correct |
39 |
Correct |
10 ms |
12348 KB |
Output is correct |
40 |
Correct |
8 ms |
12244 KB |
Output is correct |
41 |
Correct |
8 ms |
12244 KB |
Output is correct |
42 |
Correct |
9 ms |
12372 KB |
Output is correct |
43 |
Correct |
9 ms |
13012 KB |
Output is correct |
44 |
Correct |
7 ms |
12076 KB |
Output is correct |
45 |
Correct |
8 ms |
12348 KB |
Output is correct |
46 |
Correct |
9 ms |
12340 KB |
Output is correct |
47 |
Correct |
7 ms |
12024 KB |
Output is correct |
48 |
Correct |
8 ms |
12244 KB |
Output is correct |
49 |
Correct |
8 ms |
12372 KB |
Output is correct |
50 |
Correct |
9 ms |
12600 KB |
Output is correct |
51 |
Correct |
8 ms |
12244 KB |
Output is correct |
52 |
Correct |
23 ms |
12220 KB |
Output is correct |
53 |
Correct |
9 ms |
12260 KB |
Output is correct |
54 |
Correct |
10 ms |
12304 KB |
Output is correct |
55 |
Correct |
7 ms |
11988 KB |
Output is correct |
56 |
Correct |
84 ms |
18576 KB |
Output is correct |
57 |
Correct |
89 ms |
18256 KB |
Output is correct |
58 |
Correct |
9 ms |
12320 KB |
Output is correct |
59 |
Correct |
7 ms |
12004 KB |
Output is correct |
60 |
Correct |
8 ms |
12048 KB |
Output is correct |
61 |
Correct |
98 ms |
18456 KB |
Output is correct |
62 |
Correct |
8 ms |
12244 KB |
Output is correct |
63 |
Correct |
85 ms |
41420 KB |
Output is correct |
64 |
Correct |
104 ms |
18372 KB |
Output is correct |
65 |
Correct |
8 ms |
12220 KB |
Output is correct |
66 |
Correct |
69 ms |
21068 KB |
Output is correct |
67 |
Correct |
8 ms |
12244 KB |
Output is correct |
68 |
Correct |
9 ms |
12244 KB |
Output is correct |
69 |
Correct |
52 ms |
18544 KB |
Output is correct |
70 |
Correct |
8 ms |
13012 KB |
Output is correct |
71 |
Correct |
70 ms |
18128 KB |
Output is correct |
72 |
Correct |
86 ms |
25888 KB |
Output is correct |
73 |
Correct |
11 ms |
12088 KB |
Output is correct |
74 |
Correct |
102 ms |
42360 KB |
Output is correct |
75 |
Correct |
90 ms |
31544 KB |
Output is correct |
76 |
Correct |
9 ms |
12244 KB |
Output is correct |
77 |
Correct |
17 ms |
12244 KB |
Output is correct |
78 |
Correct |
53 ms |
17324 KB |
Output is correct |
79 |
Correct |
69 ms |
24388 KB |
Output is correct |
80 |
Correct |
9 ms |
12372 KB |
Output is correct |
81 |
Correct |
9 ms |
12244 KB |
Output is correct |
82 |
Correct |
7 ms |
12088 KB |
Output is correct |
83 |
Correct |
7 ms |
12116 KB |
Output is correct |
84 |
Correct |
8 ms |
12220 KB |
Output is correct |
85 |
Correct |
100 ms |
19728 KB |
Output is correct |
86 |
Correct |
8 ms |
12244 KB |
Output is correct |
87 |
Correct |
88 ms |
18840 KB |
Output is correct |
88 |
Correct |
7 ms |
12080 KB |
Output is correct |
89 |
Correct |
83 ms |
18884 KB |
Output is correct |
90 |
Correct |
84 ms |
19976 KB |
Output is correct |
91 |
Correct |
80 ms |
24116 KB |
Output is correct |
92 |
Correct |
61 ms |
18580 KB |
Output is correct |
93 |
Correct |
10 ms |
12372 KB |
Output is correct |
94 |
Correct |
7 ms |
12116 KB |
Output is correct |
95 |
Correct |
81 ms |
23448 KB |
Output is correct |
96 |
Correct |
119 ms |
34436 KB |
Output is correct |
97 |
Correct |
9 ms |
12244 KB |
Output is correct |
98 |
Correct |
7 ms |
12116 KB |
Output is correct |
99 |
Correct |
94 ms |
20308 KB |
Output is correct |
100 |
Correct |
8 ms |
12220 KB |
Output is correct |
101 |
Correct |
81 ms |
19276 KB |
Output is correct |
102 |
Correct |
96 ms |
30928 KB |
Output is correct |
103 |
Correct |
8 ms |
12372 KB |
Output is correct |
104 |
Correct |
9 ms |
12600 KB |
Output is correct |
105 |
Correct |
8 ms |
12216 KB |
Output is correct |
106 |
Correct |
8 ms |
12228 KB |
Output is correct |
107 |
Correct |
9 ms |
12244 KB |
Output is correct |
108 |
Correct |
593 ms |
55096 KB |
Output is correct |
109 |
Correct |
579 ms |
102620 KB |
Output is correct |
110 |
Correct |
591 ms |
138516 KB |
Output is correct |
111 |
Correct |
645 ms |
132452 KB |
Output is correct |
112 |
Correct |
590 ms |
97896 KB |
Output is correct |
113 |
Correct |
452 ms |
70220 KB |
Output is correct |
114 |
Correct |
482 ms |
43704 KB |
Output is correct |
115 |
Correct |
489 ms |
43852 KB |
Output is correct |
116 |
Correct |
774 ms |
48572 KB |
Output is correct |
117 |
Correct |
582 ms |
73312 KB |
Output is correct |
118 |
Correct |
529 ms |
45564 KB |
Output is correct |
119 |
Correct |
604 ms |
73444 KB |
Output is correct |
120 |
Correct |
624 ms |
110540 KB |
Output is correct |
121 |
Correct |
605 ms |
73452 KB |
Output is correct |
122 |
Correct |
919 ms |
57700 KB |
Output is correct |
123 |
Correct |
413 ms |
74896 KB |
Output is correct |
124 |
Correct |
578 ms |
64964 KB |
Output is correct |