Submission #743347

# Submission time Handle Problem Language Result Execution time Memory
743347 2023-05-17T10:12:47 Z myrcella Mergers (JOI19_mergers) C++17
100 / 100
1448 ms 119944 KB
//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];
int fnode[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]);
}

int getfnode(int x) {
	if (fnode[x]==x) return x;
	else return fnode[x] = getfnode(fnode[x]);
}

void merge(int x,int y) {
	f[getf(x)] = getf(y);
}

void update(int u,int v) {
	u = getfnode(u), v = getfnode(v);
	while (u!=v) {
		int uu = u,vv=v;
		if (dep[u]>dep[v]) u = getfnode(par[u]),fnode[getfnode(uu)] = getfnode(u);
		else v = getfnode(par[v]),fnode[getfnode(vv)] = getfnode(v);
		merge(col[u],col[v]);
		
	}
	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+1) fnode[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;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 12 ms 23792 KB Output is correct
4 Correct 14 ms 23832 KB Output is correct
5 Correct 12 ms 23780 KB Output is correct
6 Correct 12 ms 23860 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23852 KB Output is correct
9 Correct 12 ms 23848 KB Output is correct
10 Correct 12 ms 23824 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 12 ms 23776 KB Output is correct
14 Correct 14 ms 23764 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23780 KB Output is correct
19 Correct 12 ms 23836 KB Output is correct
20 Correct 12 ms 23832 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23848 KB Output is correct
24 Correct 13 ms 23852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 12 ms 23792 KB Output is correct
4 Correct 14 ms 23832 KB Output is correct
5 Correct 12 ms 23780 KB Output is correct
6 Correct 12 ms 23860 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23852 KB Output is correct
9 Correct 12 ms 23848 KB Output is correct
10 Correct 12 ms 23824 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 12 ms 23776 KB Output is correct
14 Correct 14 ms 23764 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23780 KB Output is correct
19 Correct 12 ms 23836 KB Output is correct
20 Correct 12 ms 23832 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23848 KB Output is correct
24 Correct 13 ms 23852 KB Output is correct
25 Correct 13 ms 23756 KB Output is correct
26 Correct 14 ms 24168 KB Output is correct
27 Correct 13 ms 24128 KB Output is correct
28 Correct 14 ms 24276 KB Output is correct
29 Correct 15 ms 24304 KB Output is correct
30 Correct 13 ms 24020 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 13 ms 24208 KB Output is correct
33 Correct 12 ms 23768 KB Output is correct
34 Correct 15 ms 24020 KB Output is correct
35 Correct 15 ms 24300 KB Output is correct
36 Correct 14 ms 23900 KB Output is correct
37 Correct 17 ms 24020 KB Output is correct
38 Correct 13 ms 23764 KB Output is correct
39 Correct 14 ms 24060 KB Output is correct
40 Correct 14 ms 23936 KB Output is correct
41 Correct 14 ms 24040 KB Output is correct
42 Correct 14 ms 24020 KB Output is correct
43 Correct 14 ms 24132 KB Output is correct
44 Correct 15 ms 23764 KB Output is correct
45 Correct 15 ms 24148 KB Output is correct
46 Correct 14 ms 24004 KB Output is correct
47 Correct 13 ms 23796 KB Output is correct
48 Correct 15 ms 23948 KB Output is correct
49 Correct 16 ms 24276 KB Output is correct
50 Correct 15 ms 24276 KB Output is correct
51 Correct 14 ms 24020 KB Output is correct
52 Correct 13 ms 23932 KB Output is correct
53 Correct 14 ms 24020 KB Output is correct
54 Correct 13 ms 24028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 12 ms 23792 KB Output is correct
4 Correct 14 ms 23832 KB Output is correct
5 Correct 12 ms 23780 KB Output is correct
6 Correct 12 ms 23860 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23852 KB Output is correct
9 Correct 12 ms 23848 KB Output is correct
10 Correct 12 ms 23824 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 12 ms 23776 KB Output is correct
14 Correct 14 ms 23764 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23780 KB Output is correct
19 Correct 12 ms 23836 KB Output is correct
20 Correct 12 ms 23832 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23848 KB Output is correct
24 Correct 13 ms 23852 KB Output is correct
25 Correct 16 ms 23892 KB Output is correct
26 Correct 51 ms 29512 KB Output is correct
27 Correct 69 ms 29176 KB Output is correct
28 Correct 14 ms 24020 KB Output is correct
29 Correct 13 ms 23764 KB Output is correct
30 Correct 13 ms 23764 KB Output is correct
31 Correct 66 ms 29044 KB Output is correct
32 Correct 15 ms 24020 KB Output is correct
33 Correct 70 ms 33304 KB Output is correct
34 Correct 69 ms 30660 KB Output is correct
35 Correct 15 ms 24020 KB Output is correct
36 Correct 69 ms 30952 KB Output is correct
37 Correct 14 ms 24020 KB Output is correct
38 Correct 15 ms 23992 KB Output is correct
39 Correct 53 ms 30848 KB Output is correct
40 Correct 14 ms 24096 KB Output is correct
41 Correct 63 ms 30452 KB Output is correct
42 Correct 75 ms 31908 KB Output is correct
43 Correct 13 ms 23764 KB Output is correct
44 Correct 69 ms 34880 KB Output is correct
45 Correct 69 ms 32744 KB Output is correct
46 Correct 13 ms 23964 KB Output is correct
47 Correct 15 ms 23964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 29472 KB Output is correct
2 Correct 138 ms 39048 KB Output is correct
3 Correct 15 ms 24148 KB Output is correct
4 Correct 14 ms 24020 KB Output is correct
5 Correct 12 ms 23824 KB Output is correct
6 Correct 14 ms 23820 KB Output is correct
7 Correct 13 ms 23936 KB Output is correct
8 Correct 84 ms 31180 KB Output is correct
9 Correct 14 ms 24020 KB Output is correct
10 Correct 61 ms 29132 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 61 ms 29128 KB Output is correct
13 Correct 101 ms 31692 KB Output is correct
14 Correct 162 ms 38744 KB Output is correct
15 Correct 52 ms 29472 KB Output is correct
16 Correct 14 ms 24020 KB Output is correct
17 Correct 13 ms 23804 KB Output is correct
18 Correct 118 ms 37396 KB Output is correct
19 Correct 174 ms 41036 KB Output is correct
20 Correct 14 ms 24072 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 74 ms 32024 KB Output is correct
23 Correct 14 ms 24020 KB Output is correct
24 Correct 84 ms 30100 KB Output is correct
25 Correct 189 ms 40236 KB Output is correct
26 Correct 16 ms 24276 KB Output is correct
27 Correct 16 ms 24316 KB Output is correct
28 Correct 14 ms 24020 KB Output is correct
29 Correct 15 ms 24048 KB Output is correct
30 Correct 14 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 12 ms 23792 KB Output is correct
4 Correct 14 ms 23832 KB Output is correct
5 Correct 12 ms 23780 KB Output is correct
6 Correct 12 ms 23860 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23852 KB Output is correct
9 Correct 12 ms 23848 KB Output is correct
10 Correct 12 ms 23824 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 12 ms 23776 KB Output is correct
14 Correct 14 ms 23764 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23780 KB Output is correct
19 Correct 12 ms 23836 KB Output is correct
20 Correct 12 ms 23832 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 12 ms 23848 KB Output is correct
24 Correct 13 ms 23852 KB Output is correct
25 Correct 13 ms 23756 KB Output is correct
26 Correct 14 ms 24168 KB Output is correct
27 Correct 13 ms 24128 KB Output is correct
28 Correct 14 ms 24276 KB Output is correct
29 Correct 15 ms 24304 KB Output is correct
30 Correct 13 ms 24020 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 13 ms 24208 KB Output is correct
33 Correct 12 ms 23768 KB Output is correct
34 Correct 15 ms 24020 KB Output is correct
35 Correct 15 ms 24300 KB Output is correct
36 Correct 14 ms 23900 KB Output is correct
37 Correct 17 ms 24020 KB Output is correct
38 Correct 13 ms 23764 KB Output is correct
39 Correct 14 ms 24060 KB Output is correct
40 Correct 14 ms 23936 KB Output is correct
41 Correct 14 ms 24040 KB Output is correct
42 Correct 14 ms 24020 KB Output is correct
43 Correct 14 ms 24132 KB Output is correct
44 Correct 15 ms 23764 KB Output is correct
45 Correct 15 ms 24148 KB Output is correct
46 Correct 14 ms 24004 KB Output is correct
47 Correct 13 ms 23796 KB Output is correct
48 Correct 15 ms 23948 KB Output is correct
49 Correct 16 ms 24276 KB Output is correct
50 Correct 15 ms 24276 KB Output is correct
51 Correct 14 ms 24020 KB Output is correct
52 Correct 13 ms 23932 KB Output is correct
53 Correct 14 ms 24020 KB Output is correct
54 Correct 13 ms 24028 KB Output is correct
55 Correct 16 ms 23892 KB Output is correct
56 Correct 51 ms 29512 KB Output is correct
57 Correct 69 ms 29176 KB Output is correct
58 Correct 14 ms 24020 KB Output is correct
59 Correct 13 ms 23764 KB Output is correct
60 Correct 13 ms 23764 KB Output is correct
61 Correct 66 ms 29044 KB Output is correct
62 Correct 15 ms 24020 KB Output is correct
63 Correct 70 ms 33304 KB Output is correct
64 Correct 69 ms 30660 KB Output is correct
65 Correct 15 ms 24020 KB Output is correct
66 Correct 69 ms 30952 KB Output is correct
67 Correct 14 ms 24020 KB Output is correct
68 Correct 15 ms 23992 KB Output is correct
69 Correct 53 ms 30848 KB Output is correct
70 Correct 14 ms 24096 KB Output is correct
71 Correct 63 ms 30452 KB Output is correct
72 Correct 75 ms 31908 KB Output is correct
73 Correct 13 ms 23764 KB Output is correct
74 Correct 69 ms 34880 KB Output is correct
75 Correct 69 ms 32744 KB Output is correct
76 Correct 13 ms 23964 KB Output is correct
77 Correct 15 ms 23964 KB Output is correct
78 Correct 49 ms 29472 KB Output is correct
79 Correct 138 ms 39048 KB Output is correct
80 Correct 15 ms 24148 KB Output is correct
81 Correct 14 ms 24020 KB Output is correct
82 Correct 12 ms 23824 KB Output is correct
83 Correct 14 ms 23820 KB Output is correct
84 Correct 13 ms 23936 KB Output is correct
85 Correct 84 ms 31180 KB Output is correct
86 Correct 14 ms 24020 KB Output is correct
87 Correct 61 ms 29132 KB Output is correct
88 Correct 13 ms 23764 KB Output is correct
89 Correct 61 ms 29128 KB Output is correct
90 Correct 101 ms 31692 KB Output is correct
91 Correct 162 ms 38744 KB Output is correct
92 Correct 52 ms 29472 KB Output is correct
93 Correct 14 ms 24020 KB Output is correct
94 Correct 13 ms 23804 KB Output is correct
95 Correct 118 ms 37396 KB Output is correct
96 Correct 174 ms 41036 KB Output is correct
97 Correct 14 ms 24072 KB Output is correct
98 Correct 12 ms 23764 KB Output is correct
99 Correct 74 ms 32024 KB Output is correct
100 Correct 14 ms 24020 KB Output is correct
101 Correct 84 ms 30100 KB Output is correct
102 Correct 189 ms 40236 KB Output is correct
103 Correct 16 ms 24276 KB Output is correct
104 Correct 16 ms 24316 KB Output is correct
105 Correct 14 ms 24020 KB Output is correct
106 Correct 15 ms 24048 KB Output is correct
107 Correct 14 ms 24020 KB Output is correct
108 Correct 527 ms 69892 KB Output is correct
109 Correct 537 ms 68252 KB Output is correct
110 Correct 522 ms 75004 KB Output is correct
111 Correct 1375 ms 119944 KB Output is correct
112 Correct 1118 ms 102004 KB Output is correct
113 Correct 1065 ms 102192 KB Output is correct
114 Correct 411 ms 57432 KB Output is correct
115 Correct 413 ms 57712 KB Output is correct
116 Correct 565 ms 62932 KB Output is correct
117 Correct 1291 ms 108656 KB Output is correct
118 Correct 445 ms 59008 KB Output is correct
119 Correct 1295 ms 108788 KB Output is correct
120 Correct 1401 ms 116944 KB Output is correct
121 Correct 1448 ms 108624 KB Output is correct
122 Correct 738 ms 75024 KB Output is correct
123 Correct 1276 ms 110192 KB Output is correct
124 Correct 1105 ms 95936 KB Output is correct