답안 #203983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
203983 2020-02-23T12:13:28 Z ics0503 Unique Cities (JOI19_ho_t5) C++17
4 / 100
2000 ms 36840 KB
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>edge[212121]; int color[212121], depth[2][212121], D[2][212121], mx, mxw;
void dfs(int fl, int w, int bef, int dep) {
	D[fl][w] = depth[fl][w] = dep;
	if (mx < dep)mx = dep, mxw = w;
	for (int nxt : edge[w])if (nxt != bef) {
		dfs(fl, nxt, w, dep + 1);
		D[fl][w] = max(D[fl][w], D[fl][nxt]);
	}
}
int P[2][212121], ans[2][212121], cnt, colorCnt, cN[212121], prC[212121];
void coloring(int color, int cnt) {
	int befC = !!cN[color]; cN[color] += cnt;
	if (befC != !!cN[color]) colorCnt += cnt;
}
void prColor(int fl, int w, int len, int cnt) {
	int now = w;
	for (int i = 0; i < len; i++) if (now > 0) {
		int befC = !!prC[now]; prC[now] += cnt;
		if (befC != !!prC[now])coloring(color[now], -cnt);
		now = P[fl][now];
	}
}
void get_ans(int fl, int w, int bef) {
	P[fl][w] = bef; vector<pair<int, int>>L;
	for (int nxt : edge[w]) if (nxt != bef) L.push_back({ -(D[fl][nxt] - depth[fl][w]), nxt });
	sort(L.begin(), L.end());
	if (L.size() == 0) { ans[fl][w] = colorCnt; return; }
	int mx = -L.front().first; prColor(fl, P[fl][w], mx, 1);
	ans[fl][w] = colorCnt;
	coloring(color[w], 1);
	
	for (int i = 1; i < L.size(); i++) get_ans(fl, L[i].second, w);
	prColor(fl, P[fl][w], mx, -1);

	mx = (L.size() > 1)? -L[1].first: 0; prColor(fl, P[fl][w], mx, 1);
	get_ans(fl, L[0].second, w);
	prColor(fl, P[fl][w], mx, -1);

	coloring(color[w], -1);
}
int main() {
	int n, m, i, j, l, r; scanf("%d%d", &n, &m);
	for (i = 0; i < n - 1; i++) {
		int s, e; scanf("%d%d", &s, &e);
		edge[s].push_back(e); edge[e].push_back(s);
	}
	for (i = 1; i <= n; i++)scanf("%d", &color[i]);
	mx = mxw = -1; dfs(0, 1, -1, 0); l = mxw;
	mx = mxw = -1; dfs(0, l, -1, 0); r = mxw;
	dfs(1, r, -1, 0);

	get_ans(0, l, -1); get_ans(1, r, -1);
	for (i = 1; i <= n; i++) {
		if (depth[0][i] < depth[1][i]) printf("%d\n", ans[1][i]);
		else printf("%d\n", ans[0][i]);
	}
	return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'void get_ans(int, int, int)':
joi2019_ho_t5.cpp:36:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 1; i < L.size(); i++) get_ans(fl, L[i].second, w);
                  ~~^~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:46:15: warning: unused variable 'j' [-Wunused-variable]
  int n, m, i, j, l, r; scanf("%d%d", &n, &m);
               ^
joi2019_ho_t5.cpp:46:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n, m, i, j, l, r; scanf("%d%d", &n, &m);
                        ~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:48:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int s, e; scanf("%d%d", &s, &e);
             ~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:51:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (i = 1; i <= n; i++)scanf("%d", &color[i]);
                          ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5368 KB Output is correct
2 Correct 10 ms 5496 KB Output is correct
3 Correct 16 ms 5624 KB Output is correct
4 Correct 18 ms 5624 KB Output is correct
5 Correct 10 ms 5496 KB Output is correct
6 Correct 39 ms 5880 KB Output is correct
7 Correct 19 ms 5624 KB Output is correct
8 Correct 10 ms 5496 KB Output is correct
9 Correct 11 ms 5496 KB Output is correct
10 Correct 10 ms 5628 KB Output is correct
11 Correct 10 ms 5500 KB Output is correct
12 Correct 9 ms 5496 KB Output is correct
13 Correct 36 ms 5880 KB Output is correct
14 Correct 14 ms 5624 KB Output is correct
15 Correct 14 ms 5752 KB Output is correct
16 Correct 9 ms 5496 KB Output is correct
17 Correct 23 ms 5752 KB Output is correct
18 Correct 18 ms 5624 KB Output is correct
19 Correct 10 ms 5496 KB Output is correct
20 Correct 46 ms 5880 KB Output is correct
21 Correct 20 ms 5752 KB Output is correct
22 Correct 9 ms 5496 KB Output is correct
23 Correct 10 ms 5624 KB Output is correct
24 Correct 10 ms 5500 KB Output is correct
25 Correct 11 ms 5484 KB Output is correct
26 Correct 10 ms 5624 KB Output is correct
27 Correct 29 ms 5844 KB Output is correct
28 Correct 21 ms 5752 KB Output is correct
29 Correct 16 ms 5624 KB Output is correct
30 Correct 10 ms 5560 KB Output is correct
31 Correct 25 ms 5752 KB Output is correct
32 Correct 19 ms 5752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 238 ms 15480 KB Output is correct
2 Execution timed out 2059 ms 23404 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 409 ms 20604 KB Output is correct
2 Execution timed out 2080 ms 36840 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5368 KB Output is correct
2 Correct 10 ms 5496 KB Output is correct
3 Correct 16 ms 5624 KB Output is correct
4 Correct 18 ms 5624 KB Output is correct
5 Correct 10 ms 5496 KB Output is correct
6 Correct 39 ms 5880 KB Output is correct
7 Correct 19 ms 5624 KB Output is correct
8 Correct 10 ms 5496 KB Output is correct
9 Correct 11 ms 5496 KB Output is correct
10 Correct 10 ms 5628 KB Output is correct
11 Correct 10 ms 5500 KB Output is correct
12 Correct 9 ms 5496 KB Output is correct
13 Correct 36 ms 5880 KB Output is correct
14 Correct 14 ms 5624 KB Output is correct
15 Correct 14 ms 5752 KB Output is correct
16 Correct 9 ms 5496 KB Output is correct
17 Correct 23 ms 5752 KB Output is correct
18 Correct 18 ms 5624 KB Output is correct
19 Correct 10 ms 5496 KB Output is correct
20 Correct 46 ms 5880 KB Output is correct
21 Correct 20 ms 5752 KB Output is correct
22 Correct 9 ms 5496 KB Output is correct
23 Correct 10 ms 5624 KB Output is correct
24 Correct 10 ms 5500 KB Output is correct
25 Correct 11 ms 5484 KB Output is correct
26 Correct 10 ms 5624 KB Output is correct
27 Correct 29 ms 5844 KB Output is correct
28 Correct 21 ms 5752 KB Output is correct
29 Correct 16 ms 5624 KB Output is correct
30 Correct 10 ms 5560 KB Output is correct
31 Correct 25 ms 5752 KB Output is correct
32 Correct 19 ms 5752 KB Output is correct
33 Correct 238 ms 15480 KB Output is correct
34 Execution timed out 2059 ms 23404 KB Time limit exceeded
35 Halted 0 ms 0 KB -