답안 #887010

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
887010 2023-12-13T12:36:28 Z 42kangaroo 참나무 (IOI23_beechtree) C++17
31 / 100
309 ms 160144 KB
//  D. Beech Tree

#include<bits/stdc++.h>
#include "beechtree.h"

using namespace std;

using g_t = vector<vector<int>>;

struct dVal {
	map<int,int> de;
	list<set<pair<int,int>>> se;
	int seSi;
};
vector<int> de;
vector<int> de2;


dVal dfs(int n, g_t &g, vector<bool> &po, vector<int> &c) {
	map<int, int> r;
	map<int, vector<pair<int, int>>> ve;
	list<set<pair<int, int>>> se;
	map<int, map<int,int>>  OmiR;
	int seSi = 0;
	for (auto e: g[n]) {
		if (r.find(c[e]) != r.end()) po[n] = false;
		r[c[e]] = 1;
		ve[c[e]] = {};
	}
	for (auto e: g[n]) {
		auto [re, li, ssi] = dfs(e, g, po, c);
		if (!po[e]) {
			po[n] = false;
		}
		if (!po[n]) continue;
		for (auto &[co, d]: re) {
			if (r.find(co) == r.end()) {
				po[n] = false;
				break;
			}
			if (co == c[e]) r[c[e]] = d + 1;
			ve[co].emplace_back(d, e);
		}
		if (!po[n]) continue;
		if (ssi > seSi) {
			swap(se, li);
			swap(ssi, seSi);
		}
		auto itS = se.begin();
		for (auto &it: li) {
			while (itS != se.end() && itS->size() <= it.size()) {
				for (auto cos: *itS) {
					if (it.erase(cos) == 0) {
						po[n] = false;
						break;
					}
				}
				if (!po[n]) break;
				itS++;
			}
			if (!po[n]) break;
			if (!it.empty()) {
				auto act = se.insert(itS, set<pair<int, int>>());
				for (auto cos: it) {
					if (itS->erase(cos) == 0) {
						po[n] = false;
						break;
					}
					act->insert(cos);
				}
			}
			if (itS->empty()) itS++;
			if (!po[n]) break;
		}
	}
	if (!po[n]) return {};
	se.emplace_back();
	for (auto &[co, vec]: ve) {
		bool dD = true;
		for (auto [d, e]: vec) {
			if (d > r[co]) {
				po[n] = false;
				return {};
			} else if (d == r[co]) {
				dD = false;
				break;
			}
		}
		if (dD) {
			se.back().insert({co, r[co]});
			seSi++;
		}
	}
	if (se.back().empty()) se.pop_back();
	de[n] = seSi;
	return {std::move(r), std::move(se), seSi};
}

pair<array<map<int,map<int,int>>,2>, int> sdfs(int n, g_t& g, vector<bool>& po, vector<int>& c, vector<bool>&vis) {
	if (vis[n]) return {};
	vis[n] = true;
	array<map<int,map<int,int>>,2> res;
	int reSi = 0;
	for (auto e: g[n]) {
		auto [re, si] = sdfs(e, g, po, c,vis);
		if (!po[e]) po[n] = false;
		if (!po[n]) continue;
		re[0][c[e]][de[e]] = re[1][c[e]][de[e]] = de[n];
		si++;
		if (si > reSi) {
			swap(re, res);
			swap(reSi, si);
		}
		for (auto &[co, ma]: re[1]) {
			for (auto &[don, par]: ma) {
				if (res[0][co].upper_bound(don) != res[0][co].end() && res[0][co].upper_bound(don)->second < par) {
					po[n] = false;
					break;
				}
				if (res[1][co].find(don) == res[1][co].end()) reSi++;
				res[1][co][don] = min(res[1][co][don], par);
			}
			if (!po[n]) break;
		}
		if (!po[n]) break;
		for (auto &[co, ma]: re[0]) {
			for (auto &[don, par]: ma) {
				res[0][co][don] = max(res[0][co][don], par);
			}
		}
	}
	return {std::move(res), reSi};
}

void bfs(int n, g_t& g, vector<bool>& vi) {
	int d= 0;
	int pre = -1;
	int preD = de[n];
	priority_queue<tuple<int,int,int>> pq;
	pq.push({de[n], 1, n});
	while (!pq.empty()) {
		auto [dep, pr, t] = pq.top();
		pq.pop();
		if (vi[t]) continue;
		vi[t] = true;
		pr = -pr;
		if (pre != pr || preD != de[t]) d++;
		de2[t] = d;
		pre = pr;
		preD = de[t];
		for (auto e: g[t]) {
			pq.push({de[e], -d, e});
		}
	}
}

vector<int> beechtree(int N, int M, vector<int> P, vector<int> C) {
	g_t g(N);
	for (int i = 1; i < N; ++i) {
		g[P[i]].push_back(i);
	}
	de = vector<int>(N);
	de2 = vector<int>(N, -1);
	vector<bool> po(N, true);
	dfs(0, g, po, C);
	vector<bool> vis(N, false);
	for (int i = 0; i < N; ++i) {
		if (po[i]) {
			bfs(i, g, vis);
		}
	}
	std::fill(vis.begin(), vis.end(),false);
	for (int i = 0; i < N; ++i) {
		if (po[i]) {
			sdfs(i, g, po, C, vis);
		}
	}
	return {po.begin(), po.end()};
}

#ifndef EVAL
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m; cin >> n >> m;
	vector<int> p(n), c(n);
	for (auto &e: p) {
		cin >> e;
	}
	for (auto &e: c) {
		cin >> e;
	}
	auto res = beechtree(n, m, p, c);
	for (auto e: res) {
		cout << e << " ";
	}
	cout << "\n";
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 388 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 504 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 170 ms 160064 KB Output is correct
8 Correct 174 ms 160084 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 2 ms 1884 KB Output is correct
14 Correct 2 ms 1884 KB Output is correct
15 Correct 2 ms 1884 KB Output is correct
16 Correct 2 ms 1884 KB Output is correct
17 Correct 309 ms 160144 KB Output is correct
18 Correct 251 ms 160084 KB Output is correct
19 Correct 186 ms 160080 KB Output is correct
20 Correct 204 ms 159964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Correct 163 ms 7108 KB Output is correct
16 Correct 131 ms 6004 KB Output is correct
17 Correct 134 ms 6196 KB Output is correct
18 Correct 211 ms 7840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 170 ms 160064 KB Output is correct
6 Correct 174 ms 160084 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 141 ms 23224 KB Output is correct
12 Correct 255 ms 13508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 388 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 504 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 0 ms 344 KB Output is correct
45 Correct 1 ms 344 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 600 KB Output is correct
50 Correct 0 ms 344 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 344 KB Output is correct
55 Incorrect 1 ms 348 KB 2nd lines differ - on the 82nd token, expected: '0', found: '1'
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 388 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 504 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 1884 KB Output is correct
26 Correct 2 ms 1884 KB Output is correct
27 Correct 2 ms 2024 KB Output is correct
28 Correct 2 ms 1884 KB Output is correct
29 Correct 3 ms 1884 KB Output is correct
30 Correct 2 ms 600 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 444 KB Output is correct
33 Incorrect 2 ms 604 KB 2nd lines differ - on the 905th token, expected: '0', found: '1'
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 388 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 504 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 2 ms 1884 KB Output is correct
43 Correct 2 ms 1884 KB Output is correct
44 Correct 2 ms 1884 KB Output is correct
45 Correct 2 ms 1884 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 344 KB Output is correct
49 Correct 1 ms 344 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 1 ms 600 KB Output is correct
54 Correct 0 ms 344 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 2 ms 348 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 2 ms 344 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 2 ms 604 KB Output is correct
63 Correct 2 ms 348 KB Output is correct
64 Correct 0 ms 344 KB Output is correct
65 Incorrect 1 ms 348 KB 2nd lines differ - on the 82nd token, expected: '0', found: '1'
66 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 388 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 504 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 1884 KB Output is correct
26 Correct 2 ms 1884 KB Output is correct
27 Correct 2 ms 2024 KB Output is correct
28 Correct 2 ms 1884 KB Output is correct
29 Correct 3 ms 1884 KB Output is correct
30 Correct 2 ms 600 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 444 KB Output is correct
33 Incorrect 2 ms 604 KB 2nd lines differ - on the 905th token, expected: '0', found: '1'
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 388 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 504 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 170 ms 160064 KB Output is correct
39 Correct 174 ms 160084 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 344 KB Output is correct
44 Correct 2 ms 1884 KB Output is correct
45 Correct 2 ms 1884 KB Output is correct
46 Correct 2 ms 1884 KB Output is correct
47 Correct 2 ms 1884 KB Output is correct
48 Correct 309 ms 160144 KB Output is correct
49 Correct 251 ms 160084 KB Output is correct
50 Correct 186 ms 160080 KB Output is correct
51 Correct 204 ms 159964 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 0 ms 344 KB Output is correct
55 Correct 1 ms 344 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 600 KB Output is correct
60 Correct 0 ms 344 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 2 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 2 ms 344 KB Output is correct
66 Correct 163 ms 7108 KB Output is correct
67 Correct 131 ms 6004 KB Output is correct
68 Correct 134 ms 6196 KB Output is correct
69 Correct 211 ms 7840 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 2 ms 604 KB Output is correct
73 Correct 2 ms 348 KB Output is correct
74 Correct 141 ms 23224 KB Output is correct
75 Correct 255 ms 13508 KB Output is correct
76 Correct 0 ms 344 KB Output is correct
77 Incorrect 1 ms 348 KB 2nd lines differ - on the 82nd token, expected: '0', found: '1'
78 Halted 0 ms 0 KB -