Submission #347127

# Submission time Handle Problem Language Result Execution time Memory
347127 2021-01-12T01:31:24 Z wwdd Mergers (JOI19_mergers) C++14
70 / 100
477 ms 78824 KB
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
const int MN = 200200;
const int LOG = 18;
class U {
	private:
		vi rk,p,lev,ro;
	public:
		U(const vi& w) {
			lev = w;
			rk.assign(w.size(),0);
			for(int i=0;i<w.size();i++) {
				p.push_back(i);
				ro.push_back(i);
			}
		}
		int find(int a) {
			if(a == p[a]) {return a;}
			return p[a] = find(p[a]);
		}
		bool same(int a, int b) {
			return find(a) == find(b);
		}
		bool un(int a, int b) {
			if(same(a,b)) {return false;}
			a = find(a);b = find(b);
			if(rk[a] < rk[b]) {swap(a,b);}
			if(rk[a] == rk[b]) {rk[a]++;}
			if(lev[a] > lev[b]) {
				ro[a] = ro[b];
			}
			lev[a] = min(lev[a],lev[b]);
			p[b] = a;
			return true;
		}
		int rep(int u) {
			return ro[find(u)];
		}
};
vi lev;
vvi g;
int pa[MN][LOG];
int en[MN],ex[MN];
int dord;
void dfa(int u, int p, int lu, vi& le) {
	pa[u][0] = p;
	lev[u] = lu;
	en[u] = dord;dord++;
	for(int i=0;i<g[u].size();i++) {
		int v = g[u][i];
		if(v == p) {continue;}
		dfa(v,u,lu+1,le);
	}
	ex[u] = dord;dord++;
}
void proc() {
	for(int j=1;j<LOG;j++) {
		for(int i=0;i<g.size();i++) {
			pa[i][j] = pa[pa[i][j-1]][j-1];
		}
	}
}
bool anc(int a, int b) {
	return en[a] <= en[b] && ex[a] >= ex[b];
}
int lca(int a, int b) {
	if(anc(a,b)) {return a;}
	if(anc(b,a)) {return b;}
	for(int i=LOG-1;i>=0;i--) {
		if(!anc(pa[a][i],b)) {
			a = pa[a][i];
		}
	}
	return pa[a][0];
}
int main() {
	int n,k;
	cin >> n >> k;
	g.assign(n,vi());
	lev.assign(n,0);
	dord = 0;
	vector<ii> eg;
	for(int i=1;i<n;i++) {
		int a,b;
		cin >> a >> b;
		a--;b--;
		g[a].push_back(b);
		g[b].push_back(a);
		eg.emplace_back(a,b);
	}
	dfa(0,0,0,lev);
	U bs(lev);
	proc();
	vvi bob(k,vi());
	for(int i=0;i<n;i++) {
		int co;
		cin >> co;
		co--;
		bob[co].push_back(i);
	}
	vi rob(n,0);
	for(int i=0;i<k;i++) {
		int sz = bob[i].size();
		for(int j=0;j<bob[i].size();j++) {
			int u = bob[i][j];
			int v = bob[i][(j+1)%sz];
			int lu = lca(u,v);
			rob[u] = max(rob[u],lev[u]-lev[lu]);
			rob[v] = max(rob[v],lev[v]-lev[lu]);
		}
	}
	for(int i=0;i<n;i++) {
		int gol = lev[i]-rob[i];
		int u = bs.rep(i);
		while(lev[u] > gol) {
			bs.un(u,pa[u][0]);
			u = bs.rep(u);
		}
	}
	vector<set<int>> ha(n);
	for(int i=0;i<eg.size();i++) {
		int a = bs.find(eg[i].first);
		int b = bs.find(eg[i].second);
		if(a != b) {
			ha[a].insert(b);
			ha[b].insert(a);
		}
	}
	int luf = 0;
	for(int i=0;i<n;i++) {
		if(ha[i].size() == 1) {luf++;}
	}
	int res = (luf+1)/2;
	cout << res << '\n';
}

Compilation message

mergers.cpp: In constructor 'U::U(const vi&)':
mergers.cpp:15:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |    for(int i=0;i<w.size();i++) {
      |                ~^~~~~~~~~
mergers.cpp: In function 'void dfa(int, int, int, vi&)':
mergers.cpp:52:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |  for(int i=0;i<g[u].size();i++) {
      |              ~^~~~~~~~~~~~
mergers.cpp: In function 'void proc()':
mergers.cpp:61:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |   for(int i=0;i<g.size();i++) {
      |               ~^~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:107:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |   for(int j=0;j<bob[i].size();j++) {
      |               ~^~~~~~~~~~~~~~
mergers.cpp:124:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |  for(int i=0;i<eg.size();i++) {
      |              ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 5 ms 1260 KB Output is correct
27 Correct 4 ms 1004 KB Output is correct
28 Correct 5 ms 1516 KB Output is correct
29 Correct 6 ms 1420 KB Output is correct
30 Correct 4 ms 1004 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 5 ms 1388 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 4 ms 1004 KB Output is correct
35 Correct 5 ms 1536 KB Output is correct
36 Correct 4 ms 1004 KB Output is correct
37 Correct 5 ms 1132 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 5 ms 1132 KB Output is correct
40 Correct 4 ms 1004 KB Output is correct
41 Correct 4 ms 1004 KB Output is correct
42 Correct 4 ms 1132 KB Output is correct
43 Correct 4 ms 1260 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 5 ms 1260 KB Output is correct
46 Correct 5 ms 1132 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 6 ms 1132 KB Output is correct
49 Correct 6 ms 1516 KB Output is correct
50 Correct 5 ms 1516 KB Output is correct
51 Correct 4 ms 1132 KB Output is correct
52 Correct 4 ms 1004 KB Output is correct
53 Correct 4 ms 1132 KB Output is correct
54 Correct 4 ms 1004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 148 ms 24024 KB Output is correct
27 Correct 188 ms 23648 KB Output is correct
28 Correct 4 ms 1020 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 197 ms 23648 KB Output is correct
32 Correct 4 ms 1004 KB Output is correct
33 Correct 196 ms 30816 KB Output is correct
34 Correct 189 ms 23776 KB Output is correct
35 Correct 4 ms 1004 KB Output is correct
36 Correct 201 ms 24420 KB Output is correct
37 Correct 4 ms 1004 KB Output is correct
38 Correct 4 ms 1004 KB Output is correct
39 Correct 148 ms 23904 KB Output is correct
40 Correct 4 ms 1260 KB Output is correct
41 Correct 174 ms 24032 KB Output is correct
42 Correct 216 ms 26080 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 190 ms 31592 KB Output is correct
45 Correct 215 ms 27488 KB Output is correct
46 Correct 4 ms 1004 KB Output is correct
47 Correct 4 ms 1004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 24152 KB Output is correct
2 Correct 273 ms 38616 KB Output is correct
3 Correct 5 ms 1260 KB Output is correct
4 Correct 4 ms 1004 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 4 ms 1004 KB Output is correct
8 Correct 226 ms 27308 KB Output is correct
9 Correct 5 ms 1004 KB Output is correct
10 Correct 191 ms 24044 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 192 ms 23904 KB Output is correct
13 Correct 211 ms 28000 KB Output is correct
14 Correct 279 ms 38572 KB Output is correct
15 Correct 158 ms 23904 KB Output is correct
16 Correct 5 ms 1132 KB Output is correct
17 Correct 1 ms 508 KB Output is correct
18 Correct 277 ms 36192 KB Output is correct
19 Correct 318 ms 42208 KB Output is correct
20 Correct 5 ms 1132 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 195 ms 28068 KB Output is correct
23 Correct 4 ms 1132 KB Output is correct
24 Correct 201 ms 25304 KB Output is correct
25 Correct 284 ms 40984 KB Output is correct
26 Correct 5 ms 1516 KB Output is correct
27 Correct 5 ms 1516 KB Output is correct
28 Correct 4 ms 1132 KB Output is correct
29 Correct 4 ms 1004 KB Output is correct
30 Correct 5 ms 1132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 5 ms 1260 KB Output is correct
27 Correct 4 ms 1004 KB Output is correct
28 Correct 5 ms 1516 KB Output is correct
29 Correct 6 ms 1420 KB Output is correct
30 Correct 4 ms 1004 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 5 ms 1388 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 4 ms 1004 KB Output is correct
35 Correct 5 ms 1536 KB Output is correct
36 Correct 4 ms 1004 KB Output is correct
37 Correct 5 ms 1132 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 5 ms 1132 KB Output is correct
40 Correct 4 ms 1004 KB Output is correct
41 Correct 4 ms 1004 KB Output is correct
42 Correct 4 ms 1132 KB Output is correct
43 Correct 4 ms 1260 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 5 ms 1260 KB Output is correct
46 Correct 5 ms 1132 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 6 ms 1132 KB Output is correct
49 Correct 6 ms 1516 KB Output is correct
50 Correct 5 ms 1516 KB Output is correct
51 Correct 4 ms 1132 KB Output is correct
52 Correct 4 ms 1004 KB Output is correct
53 Correct 4 ms 1132 KB Output is correct
54 Correct 4 ms 1004 KB Output is correct
55 Correct 1 ms 364 KB Output is correct
56 Correct 148 ms 24024 KB Output is correct
57 Correct 188 ms 23648 KB Output is correct
58 Correct 4 ms 1020 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 197 ms 23648 KB Output is correct
62 Correct 4 ms 1004 KB Output is correct
63 Correct 196 ms 30816 KB Output is correct
64 Correct 189 ms 23776 KB Output is correct
65 Correct 4 ms 1004 KB Output is correct
66 Correct 201 ms 24420 KB Output is correct
67 Correct 4 ms 1004 KB Output is correct
68 Correct 4 ms 1004 KB Output is correct
69 Correct 148 ms 23904 KB Output is correct
70 Correct 4 ms 1260 KB Output is correct
71 Correct 174 ms 24032 KB Output is correct
72 Correct 216 ms 26080 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 190 ms 31592 KB Output is correct
75 Correct 215 ms 27488 KB Output is correct
76 Correct 4 ms 1004 KB Output is correct
77 Correct 4 ms 1004 KB Output is correct
78 Correct 151 ms 24152 KB Output is correct
79 Correct 273 ms 38616 KB Output is correct
80 Correct 5 ms 1260 KB Output is correct
81 Correct 4 ms 1004 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 4 ms 1004 KB Output is correct
85 Correct 226 ms 27308 KB Output is correct
86 Correct 5 ms 1004 KB Output is correct
87 Correct 191 ms 24044 KB Output is correct
88 Correct 1 ms 492 KB Output is correct
89 Correct 192 ms 23904 KB Output is correct
90 Correct 211 ms 28000 KB Output is correct
91 Correct 279 ms 38572 KB Output is correct
92 Correct 158 ms 23904 KB Output is correct
93 Correct 5 ms 1132 KB Output is correct
94 Correct 1 ms 508 KB Output is correct
95 Correct 277 ms 36192 KB Output is correct
96 Correct 318 ms 42208 KB Output is correct
97 Correct 5 ms 1132 KB Output is correct
98 Correct 1 ms 364 KB Output is correct
99 Correct 195 ms 28068 KB Output is correct
100 Correct 4 ms 1132 KB Output is correct
101 Correct 201 ms 25304 KB Output is correct
102 Correct 284 ms 40984 KB Output is correct
103 Correct 5 ms 1516 KB Output is correct
104 Correct 5 ms 1516 KB Output is correct
105 Correct 4 ms 1132 KB Output is correct
106 Correct 4 ms 1004 KB Output is correct
107 Correct 5 ms 1132 KB Output is correct
108 Runtime error 477 ms 78824 KB Execution killed with signal 11 (could be triggered by violating memory limits)
109 Halted 0 ms 0 KB -