//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 5e5+10;
int n,m;
vector <int> edge[maxn];
int dep[maxn];
int f[maxn];
int deg[maxn];
vector <int> node[maxn];
int col[maxn];
int par[maxn];
void dfs(int u,int fa) {
par[u] = fa;
dep[u] = dep[fa] + 1;
for (int v:edge[u]) {
if (v==fa) continue;
dfs(v,u);
}
return;
}
int getf(int x) {
if (f[x]==x) return x;
else return f[x] = getf(f[x]);
}
void merge(int x,int y) {
f[getf(x)] = getf(y);
}
void update(int u,int v) {
// debug(u),debug(v);
int tmp = col[u];
if (dep[u]<dep[v]) swap(u,v);
while (dep[u]>dep[v]) u = par[u],merge(col[u],tmp);
while (u!=v) {
u = par[u],merge(col[u],tmp);
v = par[v],merge(col[v],tmp);
}
return;
}
map <pii,int> mp;
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
rep(i,1,m+1) f[i] = i;
rep(i,1,n) {
int u,v;
cin>>u>>v;
edge[u].pb(v);
edge[v].pb(u);
}
dfs(1,0);
rep(i,1,n+1) {
int c;cin>>c;
col[i] = c;
node[c].pb(i);
}
// rep(i,1,n+1)
// rep(j,1,i) if (col[i]==col[j]) update(i,j);
rep(i,1,m+1) {
if (node[i].empty()) continue;
int tmp = node[i][0];
rep(j,1,SZ(node[i])) update(tmp,node[i][j]);
}
rep(i,1,n+1)
for (int v:edge[i]) {
int fx = getf(col[i]), fy = getf(col[v]);
if (fx==fy) continue;
if (fx>fy) swap(fx,fy);
if (mp.find({fx,fy})!=mp.end()) continue;
mp[{fx,fy}]=1;
deg[fx]++;
deg[fy]++;
}
int hii = 0;
int cnt = 0;
rep(i,1,m+1) {
if (getf(i)==i) hii++;
if (getf(i)==i and deg[i]==1) cnt++;
}
assert(SZ(mp)==hii-1);
cout<<(cnt+1)/2;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23820 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23848 KB |
Output is correct |
5 |
Correct |
13 ms |
23728 KB |
Output is correct |
6 |
Correct |
14 ms |
23824 KB |
Output is correct |
7 |
Correct |
15 ms |
23816 KB |
Output is correct |
8 |
Correct |
13 ms |
23840 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
17 ms |
23776 KB |
Output is correct |
12 |
Correct |
13 ms |
23768 KB |
Output is correct |
13 |
Correct |
13 ms |
23780 KB |
Output is correct |
14 |
Correct |
16 ms |
23764 KB |
Output is correct |
15 |
Correct |
16 ms |
23728 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23788 KB |
Output is correct |
18 |
Correct |
13 ms |
23824 KB |
Output is correct |
19 |
Correct |
16 ms |
23764 KB |
Output is correct |
20 |
Correct |
13 ms |
23820 KB |
Output is correct |
21 |
Correct |
12 ms |
23812 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23824 KB |
Output is correct |
24 |
Correct |
12 ms |
23824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23820 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23848 KB |
Output is correct |
5 |
Correct |
13 ms |
23728 KB |
Output is correct |
6 |
Correct |
14 ms |
23824 KB |
Output is correct |
7 |
Correct |
15 ms |
23816 KB |
Output is correct |
8 |
Correct |
13 ms |
23840 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
17 ms |
23776 KB |
Output is correct |
12 |
Correct |
13 ms |
23768 KB |
Output is correct |
13 |
Correct |
13 ms |
23780 KB |
Output is correct |
14 |
Correct |
16 ms |
23764 KB |
Output is correct |
15 |
Correct |
16 ms |
23728 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23788 KB |
Output is correct |
18 |
Correct |
13 ms |
23824 KB |
Output is correct |
19 |
Correct |
16 ms |
23764 KB |
Output is correct |
20 |
Correct |
13 ms |
23820 KB |
Output is correct |
21 |
Correct |
12 ms |
23812 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23824 KB |
Output is correct |
24 |
Correct |
12 ms |
23824 KB |
Output is correct |
25 |
Correct |
11 ms |
23764 KB |
Output is correct |
26 |
Correct |
14 ms |
24096 KB |
Output is correct |
27 |
Correct |
14 ms |
23960 KB |
Output is correct |
28 |
Correct |
16 ms |
24268 KB |
Output is correct |
29 |
Correct |
20 ms |
24308 KB |
Output is correct |
30 |
Correct |
15 ms |
24000 KB |
Output is correct |
31 |
Correct |
15 ms |
23892 KB |
Output is correct |
32 |
Correct |
16 ms |
24268 KB |
Output is correct |
33 |
Correct |
13 ms |
23824 KB |
Output is correct |
34 |
Correct |
14 ms |
23964 KB |
Output is correct |
35 |
Correct |
15 ms |
24268 KB |
Output is correct |
36 |
Correct |
14 ms |
24020 KB |
Output is correct |
37 |
Correct |
14 ms |
24092 KB |
Output is correct |
38 |
Correct |
14 ms |
23764 KB |
Output is correct |
39 |
Correct |
18 ms |
24020 KB |
Output is correct |
40 |
Correct |
15 ms |
23952 KB |
Output is correct |
41 |
Correct |
15 ms |
23964 KB |
Output is correct |
42 |
Correct |
14 ms |
24056 KB |
Output is correct |
43 |
Correct |
58 ms |
24160 KB |
Output is correct |
44 |
Correct |
15 ms |
23764 KB |
Output is correct |
45 |
Correct |
14 ms |
24148 KB |
Output is correct |
46 |
Correct |
14 ms |
24096 KB |
Output is correct |
47 |
Correct |
13 ms |
23828 KB |
Output is correct |
48 |
Correct |
14 ms |
24148 KB |
Output is correct |
49 |
Correct |
16 ms |
24316 KB |
Output is correct |
50 |
Correct |
15 ms |
24276 KB |
Output is correct |
51 |
Correct |
16 ms |
24052 KB |
Output is correct |
52 |
Correct |
15 ms |
23952 KB |
Output is correct |
53 |
Correct |
17 ms |
24020 KB |
Output is correct |
54 |
Correct |
14 ms |
23968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23820 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23848 KB |
Output is correct |
5 |
Correct |
13 ms |
23728 KB |
Output is correct |
6 |
Correct |
14 ms |
23824 KB |
Output is correct |
7 |
Correct |
15 ms |
23816 KB |
Output is correct |
8 |
Correct |
13 ms |
23840 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
17 ms |
23776 KB |
Output is correct |
12 |
Correct |
13 ms |
23768 KB |
Output is correct |
13 |
Correct |
13 ms |
23780 KB |
Output is correct |
14 |
Correct |
16 ms |
23764 KB |
Output is correct |
15 |
Correct |
16 ms |
23728 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23788 KB |
Output is correct |
18 |
Correct |
13 ms |
23824 KB |
Output is correct |
19 |
Correct |
16 ms |
23764 KB |
Output is correct |
20 |
Correct |
13 ms |
23820 KB |
Output is correct |
21 |
Correct |
12 ms |
23812 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23824 KB |
Output is correct |
24 |
Correct |
12 ms |
23824 KB |
Output is correct |
25 |
Correct |
15 ms |
23824 KB |
Output is correct |
26 |
Correct |
52 ms |
30444 KB |
Output is correct |
27 |
Correct |
88 ms |
30136 KB |
Output is correct |
28 |
Correct |
14 ms |
24020 KB |
Output is correct |
29 |
Correct |
15 ms |
23892 KB |
Output is correct |
30 |
Correct |
14 ms |
23764 KB |
Output is correct |
31 |
Correct |
96 ms |
30136 KB |
Output is correct |
32 |
Correct |
15 ms |
23960 KB |
Output is correct |
33 |
Execution timed out |
3075 ms |
34380 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
29256 KB |
Output is correct |
2 |
Correct |
133 ms |
40396 KB |
Output is correct |
3 |
Correct |
15 ms |
24152 KB |
Output is correct |
4 |
Correct |
15 ms |
23944 KB |
Output is correct |
5 |
Correct |
14 ms |
23820 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
18 ms |
24040 KB |
Output is correct |
8 |
Correct |
100 ms |
32508 KB |
Output is correct |
9 |
Correct |
14 ms |
23964 KB |
Output is correct |
10 |
Correct |
70 ms |
30248 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
105 ms |
30212 KB |
Output is correct |
13 |
Correct |
92 ms |
33076 KB |
Output is correct |
14 |
Correct |
161 ms |
40240 KB |
Output is correct |
15 |
Correct |
51 ms |
30464 KB |
Output is correct |
16 |
Correct |
18 ms |
24040 KB |
Output is correct |
17 |
Correct |
17 ms |
23764 KB |
Output is correct |
18 |
Correct |
125 ms |
38884 KB |
Output is correct |
19 |
Correct |
153 ms |
42444 KB |
Output is correct |
20 |
Correct |
14 ms |
24020 KB |
Output is correct |
21 |
Correct |
13 ms |
23832 KB |
Output is correct |
22 |
Correct |
71 ms |
33404 KB |
Output is correct |
23 |
Correct |
15 ms |
23964 KB |
Output is correct |
24 |
Correct |
87 ms |
31304 KB |
Output is correct |
25 |
Correct |
165 ms |
41668 KB |
Output is correct |
26 |
Correct |
16 ms |
24224 KB |
Output is correct |
27 |
Correct |
17 ms |
24276 KB |
Output is correct |
28 |
Correct |
18 ms |
24044 KB |
Output is correct |
29 |
Correct |
13 ms |
24020 KB |
Output is correct |
30 |
Correct |
15 ms |
24092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23820 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23848 KB |
Output is correct |
5 |
Correct |
13 ms |
23728 KB |
Output is correct |
6 |
Correct |
14 ms |
23824 KB |
Output is correct |
7 |
Correct |
15 ms |
23816 KB |
Output is correct |
8 |
Correct |
13 ms |
23840 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
17 ms |
23776 KB |
Output is correct |
12 |
Correct |
13 ms |
23768 KB |
Output is correct |
13 |
Correct |
13 ms |
23780 KB |
Output is correct |
14 |
Correct |
16 ms |
23764 KB |
Output is correct |
15 |
Correct |
16 ms |
23728 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23788 KB |
Output is correct |
18 |
Correct |
13 ms |
23824 KB |
Output is correct |
19 |
Correct |
16 ms |
23764 KB |
Output is correct |
20 |
Correct |
13 ms |
23820 KB |
Output is correct |
21 |
Correct |
12 ms |
23812 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23824 KB |
Output is correct |
24 |
Correct |
12 ms |
23824 KB |
Output is correct |
25 |
Correct |
11 ms |
23764 KB |
Output is correct |
26 |
Correct |
14 ms |
24096 KB |
Output is correct |
27 |
Correct |
14 ms |
23960 KB |
Output is correct |
28 |
Correct |
16 ms |
24268 KB |
Output is correct |
29 |
Correct |
20 ms |
24308 KB |
Output is correct |
30 |
Correct |
15 ms |
24000 KB |
Output is correct |
31 |
Correct |
15 ms |
23892 KB |
Output is correct |
32 |
Correct |
16 ms |
24268 KB |
Output is correct |
33 |
Correct |
13 ms |
23824 KB |
Output is correct |
34 |
Correct |
14 ms |
23964 KB |
Output is correct |
35 |
Correct |
15 ms |
24268 KB |
Output is correct |
36 |
Correct |
14 ms |
24020 KB |
Output is correct |
37 |
Correct |
14 ms |
24092 KB |
Output is correct |
38 |
Correct |
14 ms |
23764 KB |
Output is correct |
39 |
Correct |
18 ms |
24020 KB |
Output is correct |
40 |
Correct |
15 ms |
23952 KB |
Output is correct |
41 |
Correct |
15 ms |
23964 KB |
Output is correct |
42 |
Correct |
14 ms |
24056 KB |
Output is correct |
43 |
Correct |
58 ms |
24160 KB |
Output is correct |
44 |
Correct |
15 ms |
23764 KB |
Output is correct |
45 |
Correct |
14 ms |
24148 KB |
Output is correct |
46 |
Correct |
14 ms |
24096 KB |
Output is correct |
47 |
Correct |
13 ms |
23828 KB |
Output is correct |
48 |
Correct |
14 ms |
24148 KB |
Output is correct |
49 |
Correct |
16 ms |
24316 KB |
Output is correct |
50 |
Correct |
15 ms |
24276 KB |
Output is correct |
51 |
Correct |
16 ms |
24052 KB |
Output is correct |
52 |
Correct |
15 ms |
23952 KB |
Output is correct |
53 |
Correct |
17 ms |
24020 KB |
Output is correct |
54 |
Correct |
14 ms |
23968 KB |
Output is correct |
55 |
Correct |
15 ms |
23824 KB |
Output is correct |
56 |
Correct |
52 ms |
30444 KB |
Output is correct |
57 |
Correct |
88 ms |
30136 KB |
Output is correct |
58 |
Correct |
14 ms |
24020 KB |
Output is correct |
59 |
Correct |
15 ms |
23892 KB |
Output is correct |
60 |
Correct |
14 ms |
23764 KB |
Output is correct |
61 |
Correct |
96 ms |
30136 KB |
Output is correct |
62 |
Correct |
15 ms |
23960 KB |
Output is correct |
63 |
Execution timed out |
3075 ms |
34380 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |