답안 #347128

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
347128 2021-01-12T01:32:23 Z wwdd Mergers (JOI19_mergers) C++14
100 / 100
1993 ms 212440 KB
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
//lmao read limits
const int MN = 500200;
const int LOG = 19;
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:16:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |    for(int i=0;i<w.size();i++) {
      |                ~^~~~~~~~~
mergers.cpp: In function 'void dfa(int, int, int, vi&)':
mergers.cpp:53:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |  for(int i=0;i<g[u].size();i++) {
      |              ~^~~~~~~~~~~~
mergers.cpp: In function 'void proc()':
mergers.cpp:62:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |   for(int i=0;i<g.size();i++) {
      |               ~^~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:108:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |   for(int j=0;j<bob[i].size();j++) {
      |               ~^~~~~~~~~~~~~~
mergers.cpp:125: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]
  125 |  for(int i=0;i<eg.size();i++) {
      |              ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 380 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 396 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 364 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 364 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 380 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 396 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 364 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 364 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 6 ms 1260 KB Output is correct
27 Correct 4 ms 1132 KB Output is correct
28 Correct 5 ms 1516 KB Output is correct
29 Correct 5 ms 1516 KB Output is correct
30 Correct 4 ms 1132 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 1132 KB Output is correct
35 Correct 5 ms 1516 KB Output is correct
36 Correct 4 ms 1004 KB Output is correct
37 Correct 6 ms 1132 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 6 ms 1132 KB Output is correct
40 Correct 4 ms 1004 KB Output is correct
41 Correct 4 ms 1132 KB Output is correct
42 Correct 5 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 4 ms 1132 KB Output is correct
49 Correct 5 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 1132 KB Output is correct
53 Correct 4 ms 1132 KB Output is correct
54 Correct 4 ms 1004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 380 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 396 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 364 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 364 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 159 ms 24372 KB Output is correct
27 Correct 190 ms 24160 KB Output is correct
28 Correct 4 ms 1132 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 187 ms 24160 KB Output is correct
32 Correct 4 ms 1132 KB Output is correct
33 Correct 204 ms 31232 KB Output is correct
34 Correct 188 ms 24032 KB Output is correct
35 Correct 4 ms 1004 KB Output is correct
36 Correct 196 ms 24920 KB Output is correct
37 Correct 4 ms 1004 KB Output is correct
38 Correct 4 ms 1132 KB Output is correct
39 Correct 151 ms 24288 KB Output is correct
40 Correct 4 ms 1260 KB Output is correct
41 Correct 176 ms 24416 KB Output is correct
42 Correct 217 ms 26464 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 187 ms 31840 KB Output is correct
45 Correct 215 ms 27744 KB Output is correct
46 Correct 4 ms 1132 KB Output is correct
47 Correct 4 ms 1004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 24408 KB Output is correct
2 Correct 286 ms 38964 KB Output is correct
3 Correct 5 ms 1260 KB Output is correct
4 Correct 4 ms 1132 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 1132 KB Output is correct
8 Correct 229 ms 27784 KB Output is correct
9 Correct 4 ms 1004 KB Output is correct
10 Correct 187 ms 24288 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 199 ms 24416 KB Output is correct
13 Correct 212 ms 28512 KB Output is correct
14 Correct 274 ms 38752 KB Output is correct
15 Correct 161 ms 24416 KB Output is correct
16 Correct 5 ms 1132 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 249 ms 36576 KB Output is correct
19 Correct 295 ms 42592 KB Output is correct
20 Correct 7 ms 1132 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 182 ms 28504 KB Output is correct
23 Correct 4 ms 1132 KB Output is correct
24 Correct 203 ms 25668 KB Output is correct
25 Correct 283 ms 41312 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 1132 KB Output is correct
30 Correct 5 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 380 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 396 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 364 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 364 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 6 ms 1260 KB Output is correct
27 Correct 4 ms 1132 KB Output is correct
28 Correct 5 ms 1516 KB Output is correct
29 Correct 5 ms 1516 KB Output is correct
30 Correct 4 ms 1132 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 1132 KB Output is correct
35 Correct 5 ms 1516 KB Output is correct
36 Correct 4 ms 1004 KB Output is correct
37 Correct 6 ms 1132 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 6 ms 1132 KB Output is correct
40 Correct 4 ms 1004 KB Output is correct
41 Correct 4 ms 1132 KB Output is correct
42 Correct 5 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 4 ms 1132 KB Output is correct
49 Correct 5 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 1132 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 159 ms 24372 KB Output is correct
57 Correct 190 ms 24160 KB Output is correct
58 Correct 4 ms 1132 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 187 ms 24160 KB Output is correct
62 Correct 4 ms 1132 KB Output is correct
63 Correct 204 ms 31232 KB Output is correct
64 Correct 188 ms 24032 KB Output is correct
65 Correct 4 ms 1004 KB Output is correct
66 Correct 196 ms 24920 KB Output is correct
67 Correct 4 ms 1004 KB Output is correct
68 Correct 4 ms 1132 KB Output is correct
69 Correct 151 ms 24288 KB Output is correct
70 Correct 4 ms 1260 KB Output is correct
71 Correct 176 ms 24416 KB Output is correct
72 Correct 217 ms 26464 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 187 ms 31840 KB Output is correct
75 Correct 215 ms 27744 KB Output is correct
76 Correct 4 ms 1132 KB Output is correct
77 Correct 4 ms 1004 KB Output is correct
78 Correct 152 ms 24408 KB Output is correct
79 Correct 286 ms 38964 KB Output is correct
80 Correct 5 ms 1260 KB Output is correct
81 Correct 4 ms 1132 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 1132 KB Output is correct
85 Correct 229 ms 27784 KB Output is correct
86 Correct 4 ms 1004 KB Output is correct
87 Correct 187 ms 24288 KB Output is correct
88 Correct 1 ms 364 KB Output is correct
89 Correct 199 ms 24416 KB Output is correct
90 Correct 212 ms 28512 KB Output is correct
91 Correct 274 ms 38752 KB Output is correct
92 Correct 161 ms 24416 KB Output is correct
93 Correct 5 ms 1132 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 249 ms 36576 KB Output is correct
96 Correct 295 ms 42592 KB Output is correct
97 Correct 7 ms 1132 KB Output is correct
98 Correct 1 ms 364 KB Output is correct
99 Correct 182 ms 28504 KB Output is correct
100 Correct 4 ms 1132 KB Output is correct
101 Correct 203 ms 25668 KB Output is correct
102 Correct 283 ms 41312 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 1132 KB Output is correct
107 Correct 5 ms 1132 KB Output is correct
108 Correct 1167 ms 132744 KB Output is correct
109 Correct 1396 ms 138200 KB Output is correct
110 Correct 1416 ms 150036 KB Output is correct
111 Correct 1967 ms 212440 KB Output is correct
112 Correct 1884 ms 184536 KB Output is correct
113 Correct 1845 ms 182616 KB Output is correct
114 Correct 1121 ms 120316 KB Output is correct
115 Correct 1124 ms 120664 KB Output is correct
116 Correct 1442 ms 125528 KB Output is correct
117 Correct 1902 ms 193112 KB Output is correct
118 Correct 1218 ms 118016 KB Output is correct
119 Correct 1920 ms 193112 KB Output is correct
120 Correct 1992 ms 206936 KB Output is correct
121 Correct 1915 ms 192936 KB Output is correct
122 Correct 1561 ms 143192 KB Output is correct
123 Correct 1993 ms 194504 KB Output is correct
124 Correct 1754 ms 173012 KB Output is correct