답안 #768620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
768620 2023-06-28T10:06:14 Z SanguineChameleon Simurgh (IOI17_simurgh) C++17
51 / 100
126 ms 10708 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;

const int maxN = 5e2 + 20;
const int maxM = maxN * maxN / 2;
pair<int, int> edges[maxM];
vector<pair<int, int>> adj[maxM];
pair<int, int> high[maxN];
int par[maxN];
int par_id[maxN];
int depth[maxN];
int flag[maxM];
vector<int> cycle[maxM];
vector<int> tree;
int N, M;

void dfs(int u, int p) {
	flag[u] = true;
	high[u] = make_pair(depth[u], -1);
	for (auto e: adj[u]) {
		int v = e.first;
		int id = e.second;
		if (v == p) {
			continue;
		}
		if (!flag[v]) {
			depth[v] = depth[u] + 1;
			par[v] = u;
			par_id[v] = id;
			dfs(v, u);
			high[u] = min(high[u], high[v]);
		}
		else {
			high[u] = min(high[u], make_pair(depth[v], id));
		}
	}
}

int query_cycle(int i, int j) {
	tree.erase(find(tree.begin(), tree.end(), j));
	tree.push_back(i);
	int res = count_common_roads(tree);
	tree.pop_back();
	tree.push_back(j);
	return res;
}

int root(int u) {
	if (par[u] == -1) {
		return u;
	}
	else {
		return par[u] = root(par[u]);
	}
}

bool join(int u, int v) {
	int ru = root(u);
	int rv = root(v);
	if (ru == rv) {
		return false;
	}
	if (depth[ru] > depth[rv]) {
		swap(ru, rv);
	}
	par[ru] = rv;
	if (depth[ru] == depth[rv]) {
		depth[rv]++;
	}
	return true;
}

int query_tree(vector<int> query) {
	for (int i = 0; i < N; i++) {
		depth[i] = 0;
		par[i] = -1;
	}
	for (auto id: query) {
		join(edges[id].first, edges[id].second);
	}
	int extra = 0;
	for (auto id: tree) {
		if (join(edges[id].first, edges[id].second)) {
			extra += flag[id];
			query.push_back(id);
		}
	}
	return count_common_roads(query) - extra;
}

vector<int> find_roads(int _N, vector<int> U, vector<int> V) {
	N = _N;
	M = U.size();
	for (int i = 0; i < M; i++) {
		edges[i] = make_pair(U[i], V[i]);
		adj[U[i]].emplace_back(V[i], i);
		adj[V[i]].emplace_back(U[i], i);
	}
	dfs(0, -1);
	for (int i = 0; i < N; i++) {
		adj[i].clear();
	}
	for (int i = 0; i < M; i++) {
		flag[i] = -1;
	}
	for (int i = 1; i < N; i++) {
		tree.push_back(par_id[i]);
		if (high[i].second == -1) {
			flag[par_id[i]] = 1;
		}
		else {
			cycle[high[i].second].push_back(par_id[i]);
		}
	}
	int orig = count_common_roads(tree);
	for (int i = 0; i < M; i++) {
		if (cycle[i].empty()) {
			continue;
		}
		int u = U[i];
		int v = V[i];
		if (depth[u] < depth[v]) {
			swap(u, v);
		}
		while (par[u] != v) {
			u = par[u];
		}
		vector<pair<int, int>> cnt;
		cnt.emplace_back(i, orig);
		cnt.emplace_back(par_id[u], query_cycle(i, par_id[u]));
		for (auto j: cycle[i]) {
			cnt.emplace_back(j, query_cycle(i, j));
		}
		for (auto p1: cnt) {
			for (auto p2: cnt) {
				adj[p1.first].emplace_back(p2.first, p1.second - p2.second);
			}
		}
	}
	for (int i = 0; i < M; i++) {
		if (flag[i] != -1) {
			continue;
		}
		for (auto e: adj[i]) {
			if (e.second == -1) {
				flag[i] = 1;
			}
			if (e.second == 1) {
				flag[i] = 0;
			}
		}
		if (flag[i] != -1) {
			deque<int> q;
			q.push_back(i);
			while (!q.empty()) {
				int u = q.front();
				q.pop_front();
				for (auto e: adj[u]) {
					int v = e.first;
					if (flag[v] == -1) {
						flag[v] = flag[u] + e.second;
						q.push_back(v);
					}
				}
			}
		}
	}
	for (auto id: tree) {
		if (flag[id] == -1) {
			flag[id] = 0;
		}
	}
	vector<int> res;
	for (int i = 0; i < M; i++) {
		if (query_tree(vector<int>(1, i))) {
			res.push_back(i);
		}
	}
	return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6612 KB correct
2 Correct 4 ms 6672 KB correct
3 Correct 4 ms 6628 KB correct
4 Correct 3 ms 6612 KB correct
5 Correct 4 ms 6612 KB correct
6 Correct 3 ms 6612 KB correct
7 Correct 3 ms 6612 KB correct
8 Correct 4 ms 6612 KB correct
9 Correct 3 ms 6612 KB correct
10 Correct 3 ms 6612 KB correct
11 Correct 3 ms 6660 KB correct
12 Correct 3 ms 6660 KB correct
13 Correct 3 ms 6612 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6612 KB correct
2 Correct 4 ms 6672 KB correct
3 Correct 4 ms 6628 KB correct
4 Correct 3 ms 6612 KB correct
5 Correct 4 ms 6612 KB correct
6 Correct 3 ms 6612 KB correct
7 Correct 3 ms 6612 KB correct
8 Correct 4 ms 6612 KB correct
9 Correct 3 ms 6612 KB correct
10 Correct 3 ms 6612 KB correct
11 Correct 3 ms 6660 KB correct
12 Correct 3 ms 6660 KB correct
13 Correct 3 ms 6612 KB correct
14 Correct 4 ms 6668 KB correct
15 Correct 5 ms 6704 KB correct
16 Correct 6 ms 6664 KB correct
17 Correct 6 ms 6740 KB correct
18 Correct 5 ms 6612 KB correct
19 Correct 5 ms 6740 KB correct
20 Correct 4 ms 6740 KB correct
21 Correct 4 ms 6740 KB correct
22 Correct 4 ms 6668 KB correct
23 Correct 4 ms 6660 KB correct
24 Correct 4 ms 6612 KB correct
25 Correct 3 ms 6692 KB correct
26 Correct 4 ms 6660 KB correct
27 Correct 4 ms 6656 KB correct
28 Correct 4 ms 6612 KB correct
29 Correct 3 ms 6660 KB correct
30 Correct 4 ms 6612 KB correct
31 Correct 4 ms 6612 KB correct
32 Correct 4 ms 6612 KB correct
33 Correct 4 ms 6716 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6612 KB correct
2 Correct 4 ms 6672 KB correct
3 Correct 4 ms 6628 KB correct
4 Correct 3 ms 6612 KB correct
5 Correct 4 ms 6612 KB correct
6 Correct 3 ms 6612 KB correct
7 Correct 3 ms 6612 KB correct
8 Correct 4 ms 6612 KB correct
9 Correct 3 ms 6612 KB correct
10 Correct 3 ms 6612 KB correct
11 Correct 3 ms 6660 KB correct
12 Correct 3 ms 6660 KB correct
13 Correct 3 ms 6612 KB correct
14 Correct 4 ms 6668 KB correct
15 Correct 5 ms 6704 KB correct
16 Correct 6 ms 6664 KB correct
17 Correct 6 ms 6740 KB correct
18 Correct 5 ms 6612 KB correct
19 Correct 5 ms 6740 KB correct
20 Correct 4 ms 6740 KB correct
21 Correct 4 ms 6740 KB correct
22 Correct 4 ms 6668 KB correct
23 Correct 4 ms 6660 KB correct
24 Correct 4 ms 6612 KB correct
25 Correct 3 ms 6692 KB correct
26 Correct 4 ms 6660 KB correct
27 Correct 4 ms 6656 KB correct
28 Correct 4 ms 6612 KB correct
29 Correct 3 ms 6660 KB correct
30 Correct 4 ms 6612 KB correct
31 Correct 4 ms 6612 KB correct
32 Correct 4 ms 6612 KB correct
33 Correct 4 ms 6716 KB correct
34 Correct 123 ms 8428 KB correct
35 Correct 120 ms 8404 KB correct
36 Correct 82 ms 7892 KB correct
37 Correct 9 ms 7124 KB correct
38 Correct 121 ms 8352 KB correct
39 Correct 113 ms 8216 KB correct
40 Correct 81 ms 7892 KB correct
41 Correct 126 ms 8248 KB correct
42 Correct 124 ms 8332 KB correct
43 Correct 67 ms 7672 KB correct
44 Correct 54 ms 7388 KB correct
45 Correct 64 ms 7440 KB correct
46 Correct 50 ms 7368 KB correct
47 Correct 25 ms 6996 KB correct
48 Correct 6 ms 6612 KB correct
49 Correct 10 ms 7048 KB correct
50 Correct 25 ms 6996 KB correct
51 Correct 63 ms 7508 KB correct
52 Correct 53 ms 7412 KB correct
53 Correct 48 ms 7380 KB correct
54 Correct 66 ms 7568 KB correct
55 Correct 65 ms 7640 KB correct
56 Correct 67 ms 7508 KB correct
57 Correct 65 ms 7508 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6612 KB correct
2 Correct 3 ms 6612 KB correct
3 Incorrect 91 ms 10708 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6612 KB correct
2 Correct 4 ms 6672 KB correct
3 Correct 4 ms 6628 KB correct
4 Correct 3 ms 6612 KB correct
5 Correct 4 ms 6612 KB correct
6 Correct 3 ms 6612 KB correct
7 Correct 3 ms 6612 KB correct
8 Correct 4 ms 6612 KB correct
9 Correct 3 ms 6612 KB correct
10 Correct 3 ms 6612 KB correct
11 Correct 3 ms 6660 KB correct
12 Correct 3 ms 6660 KB correct
13 Correct 3 ms 6612 KB correct
14 Correct 4 ms 6668 KB correct
15 Correct 5 ms 6704 KB correct
16 Correct 6 ms 6664 KB correct
17 Correct 6 ms 6740 KB correct
18 Correct 5 ms 6612 KB correct
19 Correct 5 ms 6740 KB correct
20 Correct 4 ms 6740 KB correct
21 Correct 4 ms 6740 KB correct
22 Correct 4 ms 6668 KB correct
23 Correct 4 ms 6660 KB correct
24 Correct 4 ms 6612 KB correct
25 Correct 3 ms 6692 KB correct
26 Correct 4 ms 6660 KB correct
27 Correct 4 ms 6656 KB correct
28 Correct 4 ms 6612 KB correct
29 Correct 3 ms 6660 KB correct
30 Correct 4 ms 6612 KB correct
31 Correct 4 ms 6612 KB correct
32 Correct 4 ms 6612 KB correct
33 Correct 4 ms 6716 KB correct
34 Correct 123 ms 8428 KB correct
35 Correct 120 ms 8404 KB correct
36 Correct 82 ms 7892 KB correct
37 Correct 9 ms 7124 KB correct
38 Correct 121 ms 8352 KB correct
39 Correct 113 ms 8216 KB correct
40 Correct 81 ms 7892 KB correct
41 Correct 126 ms 8248 KB correct
42 Correct 124 ms 8332 KB correct
43 Correct 67 ms 7672 KB correct
44 Correct 54 ms 7388 KB correct
45 Correct 64 ms 7440 KB correct
46 Correct 50 ms 7368 KB correct
47 Correct 25 ms 6996 KB correct
48 Correct 6 ms 6612 KB correct
49 Correct 10 ms 7048 KB correct
50 Correct 25 ms 6996 KB correct
51 Correct 63 ms 7508 KB correct
52 Correct 53 ms 7412 KB correct
53 Correct 48 ms 7380 KB correct
54 Correct 66 ms 7568 KB correct
55 Correct 65 ms 7640 KB correct
56 Correct 67 ms 7508 KB correct
57 Correct 65 ms 7508 KB correct
58 Correct 3 ms 6612 KB correct
59 Correct 3 ms 6612 KB correct
60 Incorrect 91 ms 10708 KB WA in grader: NO
61 Halted 0 ms 0 KB -