Submission #138035

# Submission time Handle Problem Language Result Execution time Memory
138035 2019-07-29T06:13:00 Z rajarshi_basu Mergers (JOI19_mergers) C++14
100 / 100
924 ms 96872 KB
#include <bits/stdc++.h>

#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i = a;i<=b;i++)
#define ll long long int
#define pb push_back
#define vi vector<int>
#define ff first
#define ss second
#define vv vector
#define ii pair<int,int>

using namespace std;

const int MAXN = 5e5 + 1;
const int LOGN = 20;

int n,k;


vi g[MAXN];
int color[MAXN];
vi verticesOfColor[MAXN];
int sparseTable[LOGN][MAXN];
int depth[MAXN];
int initialLCA[MAXN];
int degree[MAXN];

void dfs_setup(int node,int p = -1){
	sparseTable[0][node] = p;
	if(p == -1)depth[node] = -1;else depth[node] = depth[p] + 1;
	for(auto e : g[node]){
		if(e != p)dfs_setup(e,node);
	}
}

struct DSU{
	int parent[MAXN];
	int ID[MAXN];
	void init(){
		FOR(i,n) parent[i] = i;
		FOR(i,n) ID[i] = i;
	}
	int find(int a){
		if(parent[a] != a)parent[a] = find(parent[a]);
		return parent[a];
	}
	void merge(int a,int b){
		int pa = find(a);
		int pb = find(b);
		if(depth[pa] > depth[pb])swap(pa,pb);
		parent[pb] = pa;
		//parent[pa] = pb;
		//if(depth[pa] < depth[pb])ID[pb] = pa;else ID[pb] = pa;
	}
	int id(int a){
		return find(a);
		//return ID[find(a)];
	}
	bool isSame(int a,int b){
		return find(a) == find(b);
	}
} dsu;

bool done[MAXN];

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> k;
	vv<ii> allE;
	FOR(i,n-1){
		int a,b;cin >> a >> b;a--;b--;
		g[a].pb(b);g[b].pb(a);
		allE.pb({a,b});
	}
	FOR(i,k)initialLCA[i] = -1;
	dfs_setup(0);
	FOR(i,n){
		int a;cin >> a;a--;
		color[i] = a;
		verticesOfColor[a].pb(i);
	}
	dsu.init();
	FOR(i,k){
		if(verticesOfColor[i].size() == 1)continue;
		FORE(j,1,(int)verticesOfColor[i].size()-1){
			int node1 = dsu.id(verticesOfColor[i][j]);
			int node2 = dsu.id(verticesOfColor[i][j-1]);
			while(node1 != node2){
				if(depth[node1] > depth[node2])swap(node1,node2);
				int p = dsu.id(sparseTable[0][node2]);
				if(p == -1)p = 0;
				dsu.merge(p,node2);
		//		cout << node2 << " " << node1 << " " << p << endl;
				node2 = p;
				
			}
		}
	}
	//FOR(i,n)cout << dsu.find(i) << endl;
	for(auto e : allE)if(!dsu.isSame(e.ff,e.ss))degree[dsu.find(e.ff)]++,degree[dsu.find(e.ss)]++;
	int cnt = 1;
	FOR(i,n)if(degree[i] == 1)cnt++;
	cout << cnt/2 << endl;
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB Output is correct
2 Correct 25 ms 23928 KB Output is correct
3 Correct 24 ms 23928 KB Output is correct
4 Correct 24 ms 23928 KB Output is correct
5 Correct 29 ms 23800 KB Output is correct
6 Correct 29 ms 23800 KB Output is correct
7 Correct 24 ms 23928 KB Output is correct
8 Correct 31 ms 23800 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 23 ms 23800 KB Output is correct
13 Correct 24 ms 23804 KB Output is correct
14 Correct 24 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 24 ms 23800 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23800 KB Output is correct
20 Correct 23 ms 23800 KB Output is correct
21 Correct 24 ms 23860 KB Output is correct
22 Correct 24 ms 23800 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 24 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB Output is correct
2 Correct 25 ms 23928 KB Output is correct
3 Correct 24 ms 23928 KB Output is correct
4 Correct 24 ms 23928 KB Output is correct
5 Correct 29 ms 23800 KB Output is correct
6 Correct 29 ms 23800 KB Output is correct
7 Correct 24 ms 23928 KB Output is correct
8 Correct 31 ms 23800 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 23 ms 23800 KB Output is correct
13 Correct 24 ms 23804 KB Output is correct
14 Correct 24 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 24 ms 23800 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23800 KB Output is correct
20 Correct 23 ms 23800 KB Output is correct
21 Correct 24 ms 23860 KB Output is correct
22 Correct 24 ms 23800 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 24 ms 23928 KB Output is correct
25 Correct 24 ms 23916 KB Output is correct
26 Correct 26 ms 24184 KB Output is correct
27 Correct 26 ms 24184 KB Output is correct
28 Correct 27 ms 24312 KB Output is correct
29 Correct 26 ms 24184 KB Output is correct
30 Correct 26 ms 24184 KB Output is correct
31 Correct 30 ms 23800 KB Output is correct
32 Correct 26 ms 24300 KB Output is correct
33 Correct 24 ms 23928 KB Output is correct
34 Correct 25 ms 24056 KB Output is correct
35 Correct 26 ms 24184 KB Output is correct
36 Correct 26 ms 24056 KB Output is correct
37 Correct 26 ms 24184 KB Output is correct
38 Correct 24 ms 23928 KB Output is correct
39 Correct 27 ms 24184 KB Output is correct
40 Correct 26 ms 24056 KB Output is correct
41 Correct 26 ms 24056 KB Output is correct
42 Correct 26 ms 24184 KB Output is correct
43 Correct 26 ms 24184 KB Output is correct
44 Correct 24 ms 23928 KB Output is correct
45 Correct 27 ms 24264 KB Output is correct
46 Correct 26 ms 24184 KB Output is correct
47 Correct 24 ms 23928 KB Output is correct
48 Correct 27 ms 24184 KB Output is correct
49 Correct 26 ms 24188 KB Output is correct
50 Correct 27 ms 24184 KB Output is correct
51 Correct 26 ms 24184 KB Output is correct
52 Correct 28 ms 24056 KB Output is correct
53 Correct 25 ms 24184 KB Output is correct
54 Correct 26 ms 24056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB Output is correct
2 Correct 25 ms 23928 KB Output is correct
3 Correct 24 ms 23928 KB Output is correct
4 Correct 24 ms 23928 KB Output is correct
5 Correct 29 ms 23800 KB Output is correct
6 Correct 29 ms 23800 KB Output is correct
7 Correct 24 ms 23928 KB Output is correct
8 Correct 31 ms 23800 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 23 ms 23800 KB Output is correct
13 Correct 24 ms 23804 KB Output is correct
14 Correct 24 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 24 ms 23800 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23800 KB Output is correct
20 Correct 23 ms 23800 KB Output is correct
21 Correct 24 ms 23860 KB Output is correct
22 Correct 24 ms 23800 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 24 ms 23928 KB Output is correct
25 Correct 24 ms 23928 KB Output is correct
26 Correct 89 ms 32256 KB Output is correct
27 Correct 111 ms 31788 KB Output is correct
28 Correct 26 ms 24056 KB Output is correct
29 Correct 24 ms 23800 KB Output is correct
30 Correct 24 ms 24056 KB Output is correct
31 Correct 101 ms 31860 KB Output is correct
32 Correct 26 ms 24056 KB Output is correct
33 Correct 120 ms 36072 KB Output is correct
34 Correct 104 ms 31848 KB Output is correct
35 Correct 25 ms 24056 KB Output is correct
36 Correct 120 ms 32236 KB Output is correct
37 Correct 26 ms 24188 KB Output is correct
38 Correct 26 ms 24184 KB Output is correct
39 Correct 93 ms 32108 KB Output is correct
40 Correct 26 ms 24312 KB Output is correct
41 Correct 106 ms 32096 KB Output is correct
42 Correct 116 ms 33128 KB Output is correct
43 Correct 24 ms 23928 KB Output is correct
44 Correct 150 ms 36588 KB Output is correct
45 Correct 115 ms 34028 KB Output is correct
46 Correct 30 ms 24056 KB Output is correct
47 Correct 50 ms 24056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 30692 KB Output is correct
2 Correct 111 ms 34008 KB Output is correct
3 Correct 26 ms 24184 KB Output is correct
4 Correct 27 ms 24148 KB Output is correct
5 Correct 24 ms 23800 KB Output is correct
6 Correct 28 ms 23928 KB Output is correct
7 Correct 27 ms 24060 KB Output is correct
8 Correct 131 ms 33832 KB Output is correct
9 Correct 26 ms 24184 KB Output is correct
10 Correct 111 ms 32364 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 159 ms 32360 KB Output is correct
13 Correct 130 ms 33644 KB Output is correct
14 Correct 122 ms 35656 KB Output is correct
15 Correct 94 ms 31980 KB Output is correct
16 Correct 30 ms 24204 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 143 ms 35504 KB Output is correct
19 Correct 142 ms 37868 KB Output is correct
20 Correct 31 ms 24184 KB Output is correct
21 Correct 24 ms 23900 KB Output is correct
22 Correct 104 ms 34008 KB Output is correct
23 Correct 26 ms 24184 KB Output is correct
24 Correct 125 ms 32748 KB Output is correct
25 Correct 135 ms 36972 KB Output is correct
26 Correct 26 ms 24312 KB Output is correct
27 Correct 26 ms 24312 KB Output is correct
28 Correct 26 ms 24184 KB Output is correct
29 Correct 26 ms 24156 KB Output is correct
30 Correct 26 ms 24184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB Output is correct
2 Correct 25 ms 23928 KB Output is correct
3 Correct 24 ms 23928 KB Output is correct
4 Correct 24 ms 23928 KB Output is correct
5 Correct 29 ms 23800 KB Output is correct
6 Correct 29 ms 23800 KB Output is correct
7 Correct 24 ms 23928 KB Output is correct
8 Correct 31 ms 23800 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 23 ms 23800 KB Output is correct
13 Correct 24 ms 23804 KB Output is correct
14 Correct 24 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 24 ms 23800 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23800 KB Output is correct
20 Correct 23 ms 23800 KB Output is correct
21 Correct 24 ms 23860 KB Output is correct
22 Correct 24 ms 23800 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 24 ms 23928 KB Output is correct
25 Correct 24 ms 23916 KB Output is correct
26 Correct 26 ms 24184 KB Output is correct
27 Correct 26 ms 24184 KB Output is correct
28 Correct 27 ms 24312 KB Output is correct
29 Correct 26 ms 24184 KB Output is correct
30 Correct 26 ms 24184 KB Output is correct
31 Correct 30 ms 23800 KB Output is correct
32 Correct 26 ms 24300 KB Output is correct
33 Correct 24 ms 23928 KB Output is correct
34 Correct 25 ms 24056 KB Output is correct
35 Correct 26 ms 24184 KB Output is correct
36 Correct 26 ms 24056 KB Output is correct
37 Correct 26 ms 24184 KB Output is correct
38 Correct 24 ms 23928 KB Output is correct
39 Correct 27 ms 24184 KB Output is correct
40 Correct 26 ms 24056 KB Output is correct
41 Correct 26 ms 24056 KB Output is correct
42 Correct 26 ms 24184 KB Output is correct
43 Correct 26 ms 24184 KB Output is correct
44 Correct 24 ms 23928 KB Output is correct
45 Correct 27 ms 24264 KB Output is correct
46 Correct 26 ms 24184 KB Output is correct
47 Correct 24 ms 23928 KB Output is correct
48 Correct 27 ms 24184 KB Output is correct
49 Correct 26 ms 24188 KB Output is correct
50 Correct 27 ms 24184 KB Output is correct
51 Correct 26 ms 24184 KB Output is correct
52 Correct 28 ms 24056 KB Output is correct
53 Correct 25 ms 24184 KB Output is correct
54 Correct 26 ms 24056 KB Output is correct
55 Correct 24 ms 23928 KB Output is correct
56 Correct 89 ms 32256 KB Output is correct
57 Correct 111 ms 31788 KB Output is correct
58 Correct 26 ms 24056 KB Output is correct
59 Correct 24 ms 23800 KB Output is correct
60 Correct 24 ms 24056 KB Output is correct
61 Correct 101 ms 31860 KB Output is correct
62 Correct 26 ms 24056 KB Output is correct
63 Correct 120 ms 36072 KB Output is correct
64 Correct 104 ms 31848 KB Output is correct
65 Correct 25 ms 24056 KB Output is correct
66 Correct 120 ms 32236 KB Output is correct
67 Correct 26 ms 24188 KB Output is correct
68 Correct 26 ms 24184 KB Output is correct
69 Correct 93 ms 32108 KB Output is correct
70 Correct 26 ms 24312 KB Output is correct
71 Correct 106 ms 32096 KB Output is correct
72 Correct 116 ms 33128 KB Output is correct
73 Correct 24 ms 23928 KB Output is correct
74 Correct 150 ms 36588 KB Output is correct
75 Correct 115 ms 34028 KB Output is correct
76 Correct 30 ms 24056 KB Output is correct
77 Correct 50 ms 24056 KB Output is correct
78 Correct 86 ms 30692 KB Output is correct
79 Correct 111 ms 34008 KB Output is correct
80 Correct 26 ms 24184 KB Output is correct
81 Correct 27 ms 24148 KB Output is correct
82 Correct 24 ms 23800 KB Output is correct
83 Correct 28 ms 23928 KB Output is correct
84 Correct 27 ms 24060 KB Output is correct
85 Correct 131 ms 33832 KB Output is correct
86 Correct 26 ms 24184 KB Output is correct
87 Correct 111 ms 32364 KB Output is correct
88 Correct 24 ms 23928 KB Output is correct
89 Correct 159 ms 32360 KB Output is correct
90 Correct 130 ms 33644 KB Output is correct
91 Correct 122 ms 35656 KB Output is correct
92 Correct 94 ms 31980 KB Output is correct
93 Correct 30 ms 24204 KB Output is correct
94 Correct 24 ms 23928 KB Output is correct
95 Correct 143 ms 35504 KB Output is correct
96 Correct 142 ms 37868 KB Output is correct
97 Correct 31 ms 24184 KB Output is correct
98 Correct 24 ms 23900 KB Output is correct
99 Correct 104 ms 34008 KB Output is correct
100 Correct 26 ms 24184 KB Output is correct
101 Correct 125 ms 32748 KB Output is correct
102 Correct 135 ms 36972 KB Output is correct
103 Correct 26 ms 24312 KB Output is correct
104 Correct 26 ms 24312 KB Output is correct
105 Correct 26 ms 24184 KB Output is correct
106 Correct 26 ms 24156 KB Output is correct
107 Correct 26 ms 24184 KB Output is correct
108 Correct 595 ms 74440 KB Output is correct
109 Correct 706 ms 75848 KB Output is correct
110 Correct 763 ms 82788 KB Output is correct
111 Correct 816 ms 96872 KB Output is correct
112 Correct 830 ms 91184 KB Output is correct
113 Correct 677 ms 83136 KB Output is correct
114 Correct 592 ms 64708 KB Output is correct
115 Correct 595 ms 65372 KB Output is correct
116 Correct 785 ms 70412 KB Output is correct
117 Correct 743 ms 83380 KB Output is correct
118 Correct 618 ms 66208 KB Output is correct
119 Correct 737 ms 83324 KB Output is correct
120 Correct 775 ms 91536 KB Output is correct
121 Correct 732 ms 83280 KB Output is correct
122 Correct 924 ms 76308 KB Output is correct
123 Correct 549 ms 84912 KB Output is correct
124 Correct 803 ms 81888 KB Output is correct