Submission #768636

# Submission time Handle Problem Language Result Execution time Memory
768636 2023-06-28T10:19:30 Z SanguineChameleon Simurgh (IOI17_simurgh) C++17
100 / 100
131 ms 15164 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];
vector<int> adj_id[maxN];
int deg[maxN];
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;
		}
	}
	for (int i = 0; i < M; i++) {
		adj_id[U[i]].push_back(i);
		adj_id[V[i]].push_back(i);
	}
	for (int i = 0; i < N; i++) {
		deg[i] = query_tree(adj_id[i]);
	}
	vector<int> res;
	for (int i = 0; i < N - 1; i++) {
		for (int u = 0; u < N; u++) {
			if (deg[u] != 1) {
				continue;
			}
			vector<int> cand = adj_id[u];
			while ((int)cand.size() > 1) {
				int sz = cand.size();
				vector<int> left(cand.begin(), cand.begin() + sz / 2);
				vector<int> right(cand.begin() + sz / 2, cand.end());
				if (query_tree(left)) {
					cand = left;
				}
				else {
					cand = right;
				}
			}
			int id = cand[0];
			int v = (U[id] == u ? V[id] : U[id]);
			adj_id[u].erase(find(adj_id[u].begin(), adj_id[u].end(), id));
			adj_id[v].erase(find(adj_id[v].begin(), adj_id[v].end(), id));
			deg[u]--;
			deg[v]--;
			res.push_back(id);
		}
	}
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6616 KB correct
2 Correct 4 ms 6612 KB correct
3 Correct 3 ms 6696 KB correct
4 Correct 3 ms 6644 KB correct
5 Correct 3 ms 6616 KB correct
6 Correct 3 ms 6612 KB correct
7 Correct 3 ms 6616 KB correct
8 Correct 3 ms 6616 KB correct
9 Correct 3 ms 6616 KB correct
10 Correct 4 ms 6684 KB correct
11 Correct 3 ms 6620 KB correct
12 Correct 3 ms 6612 KB correct
13 Correct 4 ms 6616 KB correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6616 KB correct
2 Correct 4 ms 6612 KB correct
3 Correct 3 ms 6696 KB correct
4 Correct 3 ms 6644 KB correct
5 Correct 3 ms 6616 KB correct
6 Correct 3 ms 6612 KB correct
7 Correct 3 ms 6616 KB correct
8 Correct 3 ms 6616 KB correct
9 Correct 3 ms 6616 KB correct
10 Correct 4 ms 6684 KB correct
11 Correct 3 ms 6620 KB correct
12 Correct 3 ms 6612 KB correct
13 Correct 4 ms 6616 KB correct
14 Correct 4 ms 6784 KB correct
15 Correct 4 ms 6744 KB correct
16 Correct 5 ms 6684 KB correct
17 Correct 4 ms 6744 KB correct
18 Correct 3 ms 6744 KB correct
19 Correct 4 ms 6744 KB correct
20 Correct 3 ms 6752 KB correct
21 Correct 4 ms 6752 KB correct
22 Correct 5 ms 6672 KB correct
23 Correct 4 ms 6752 KB correct
24 Correct 5 ms 6640 KB correct
25 Correct 3 ms 6624 KB correct
26 Correct 4 ms 6748 KB correct
27 Correct 3 ms 6740 KB correct
28 Correct 3 ms 6612 KB correct
29 Correct 3 ms 6612 KB correct
30 Correct 4 ms 6740 KB correct
31 Correct 4 ms 6740 KB correct
32 Correct 4 ms 6704 KB correct
33 Correct 3 ms 6740 KB correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6616 KB correct
2 Correct 4 ms 6612 KB correct
3 Correct 3 ms 6696 KB correct
4 Correct 3 ms 6644 KB correct
5 Correct 3 ms 6616 KB correct
6 Correct 3 ms 6612 KB correct
7 Correct 3 ms 6616 KB correct
8 Correct 3 ms 6616 KB correct
9 Correct 3 ms 6616 KB correct
10 Correct 4 ms 6684 KB correct
11 Correct 3 ms 6620 KB correct
12 Correct 3 ms 6612 KB correct
13 Correct 4 ms 6616 KB correct
14 Correct 4 ms 6784 KB correct
15 Correct 4 ms 6744 KB correct
16 Correct 5 ms 6684 KB correct
17 Correct 4 ms 6744 KB correct
18 Correct 3 ms 6744 KB correct
19 Correct 4 ms 6744 KB correct
20 Correct 3 ms 6752 KB correct
21 Correct 4 ms 6752 KB correct
22 Correct 5 ms 6672 KB correct
23 Correct 4 ms 6752 KB correct
24 Correct 5 ms 6640 KB correct
25 Correct 3 ms 6624 KB correct
26 Correct 4 ms 6748 KB correct
27 Correct 3 ms 6740 KB correct
28 Correct 3 ms 6612 KB correct
29 Correct 3 ms 6612 KB correct
30 Correct 4 ms 6740 KB correct
31 Correct 4 ms 6740 KB correct
32 Correct 4 ms 6704 KB correct
33 Correct 3 ms 6740 KB correct
34 Correct 31 ms 8484 KB correct
35 Correct 26 ms 8432 KB correct
36 Correct 25 ms 8088 KB correct
37 Correct 11 ms 7124 KB correct
38 Correct 28 ms 8636 KB correct
39 Correct 27 ms 8492 KB correct
40 Correct 24 ms 8212 KB correct
41 Correct 32 ms 8572 KB correct
42 Correct 27 ms 8648 KB correct
43 Correct 20 ms 7840 KB correct
44 Correct 18 ms 7508 KB correct
45 Correct 18 ms 7704 KB correct
46 Correct 16 ms 7524 KB correct
47 Correct 13 ms 6968 KB correct
48 Correct 7 ms 6680 KB correct
49 Correct 10 ms 7080 KB correct
50 Correct 16 ms 7124 KB correct
51 Correct 20 ms 7672 KB correct
52 Correct 19 ms 7588 KB correct
53 Correct 16 ms 7548 KB correct
54 Correct 19 ms 7948 KB correct
55 Correct 23 ms 7836 KB correct
56 Correct 19 ms 7636 KB correct
57 Correct 20 ms 7676 KB correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6620 KB correct
2 Correct 4 ms 6612 KB correct
3 Correct 72 ms 11796 KB correct
4 Correct 108 ms 14744 KB correct
5 Correct 111 ms 15164 KB correct
6 Correct 131 ms 14728 KB correct
7 Correct 109 ms 14804 KB correct
8 Correct 112 ms 14588 KB correct
9 Correct 115 ms 14728 KB correct
10 Correct 118 ms 14676 KB correct
11 Correct 113 ms 15052 KB correct
12 Correct 127 ms 14504 KB correct
13 Correct 5 ms 6596 KB correct
14 Correct 124 ms 14808 KB correct
15 Correct 110 ms 14748 KB correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6616 KB correct
2 Correct 4 ms 6612 KB correct
3 Correct 3 ms 6696 KB correct
4 Correct 3 ms 6644 KB correct
5 Correct 3 ms 6616 KB correct
6 Correct 3 ms 6612 KB correct
7 Correct 3 ms 6616 KB correct
8 Correct 3 ms 6616 KB correct
9 Correct 3 ms 6616 KB correct
10 Correct 4 ms 6684 KB correct
11 Correct 3 ms 6620 KB correct
12 Correct 3 ms 6612 KB correct
13 Correct 4 ms 6616 KB correct
14 Correct 4 ms 6784 KB correct
15 Correct 4 ms 6744 KB correct
16 Correct 5 ms 6684 KB correct
17 Correct 4 ms 6744 KB correct
18 Correct 3 ms 6744 KB correct
19 Correct 4 ms 6744 KB correct
20 Correct 3 ms 6752 KB correct
21 Correct 4 ms 6752 KB correct
22 Correct 5 ms 6672 KB correct
23 Correct 4 ms 6752 KB correct
24 Correct 5 ms 6640 KB correct
25 Correct 3 ms 6624 KB correct
26 Correct 4 ms 6748 KB correct
27 Correct 3 ms 6740 KB correct
28 Correct 3 ms 6612 KB correct
29 Correct 3 ms 6612 KB correct
30 Correct 4 ms 6740 KB correct
31 Correct 4 ms 6740 KB correct
32 Correct 4 ms 6704 KB correct
33 Correct 3 ms 6740 KB correct
34 Correct 31 ms 8484 KB correct
35 Correct 26 ms 8432 KB correct
36 Correct 25 ms 8088 KB correct
37 Correct 11 ms 7124 KB correct
38 Correct 28 ms 8636 KB correct
39 Correct 27 ms 8492 KB correct
40 Correct 24 ms 8212 KB correct
41 Correct 32 ms 8572 KB correct
42 Correct 27 ms 8648 KB correct
43 Correct 20 ms 7840 KB correct
44 Correct 18 ms 7508 KB correct
45 Correct 18 ms 7704 KB correct
46 Correct 16 ms 7524 KB correct
47 Correct 13 ms 6968 KB correct
48 Correct 7 ms 6680 KB correct
49 Correct 10 ms 7080 KB correct
50 Correct 16 ms 7124 KB correct
51 Correct 20 ms 7672 KB correct
52 Correct 19 ms 7588 KB correct
53 Correct 16 ms 7548 KB correct
54 Correct 19 ms 7948 KB correct
55 Correct 23 ms 7836 KB correct
56 Correct 19 ms 7636 KB correct
57 Correct 20 ms 7676 KB correct
58 Correct 4 ms 6620 KB correct
59 Correct 4 ms 6612 KB correct
60 Correct 72 ms 11796 KB correct
61 Correct 108 ms 14744 KB correct
62 Correct 111 ms 15164 KB correct
63 Correct 131 ms 14728 KB correct
64 Correct 109 ms 14804 KB correct
65 Correct 112 ms 14588 KB correct
66 Correct 115 ms 14728 KB correct
67 Correct 118 ms 14676 KB correct
68 Correct 113 ms 15052 KB correct
69 Correct 127 ms 14504 KB correct
70 Correct 5 ms 6596 KB correct
71 Correct 124 ms 14808 KB correct
72 Correct 110 ms 14748 KB correct
73 Correct 4 ms 6612 KB correct
74 Correct 107 ms 14832 KB correct
75 Correct 111 ms 14532 KB correct
76 Correct 60 ms 9896 KB correct
77 Correct 109 ms 14908 KB correct
78 Correct 109 ms 14984 KB correct
79 Correct 111 ms 14960 KB correct
80 Correct 123 ms 14896 KB correct
81 Correct 104 ms 13980 KB correct
82 Correct 105 ms 14648 KB correct
83 Correct 99 ms 11940 KB correct
84 Correct 83 ms 12304 KB correct
85 Correct 71 ms 11872 KB correct
86 Correct 67 ms 9992 KB correct
87 Correct 54 ms 9356 KB correct
88 Correct 46 ms 8544 KB correct
89 Correct 46 ms 8400 KB correct
90 Correct 43 ms 8240 KB correct
91 Correct 23 ms 6932 KB correct
92 Correct 15 ms 6816 KB correct
93 Correct 82 ms 11732 KB correct
94 Correct 63 ms 10136 KB correct
95 Correct 68 ms 10684 KB correct
96 Correct 45 ms 8660 KB correct
97 Correct 64 ms 8708 KB correct
98 Correct 52 ms 9372 KB correct
99 Correct 46 ms 8812 KB correct
100 Correct 31 ms 7636 KB correct
101 Correct 19 ms 8276 KB correct
102 Correct 86 ms 11792 KB correct
103 Correct 82 ms 10752 KB correct
104 Correct 77 ms 11456 KB correct
105 Correct 76 ms 10752 KB correct