답안 #1076052

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1076052 2024-08-26T10:47:41 Z NeroZein Simurgh (IOI17_simurgh) C++17
51 / 100
306 ms 2216 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> find_roads(int n, vector<int> eu, vector<int> ev) {
	
	vector<int> p(n), sz(n);
	
	function<int(int)> get = [&](int v) {
		return p[v] = (p[v] == v ? v : get(p[v]));
	};
	
	auto unite = [&](int u, int v) {
		u = get(u); v = get(v);
		if (u == v) {
			return false;
		}
		if (sz[u] > sz[v]) {
			swap(u, v);
		}
		p[u] = v;
		sz[v] += sz[u];
		return true;
	};
	
	auto init_dsu = [&]() {
		iota(p.begin(), p.end(), 0);
		fill(sz.begin(), sz.end(), 1);
	};
	
	int m = (int) eu.size();
	init_dsu();
	vector<int> in_tree, out_tree;
	for (int i = 0; i < m; ++i) {
		if (unite(eu[i], ev[i])) {
			in_tree.push_back(i);
		} else {
			out_tree.push_back(i);
		}
	}
	
	int idd;
	vector<vector<int>> g(n); 
	vector<int> in(n), out(n);
	function<void(int, int)> dfs = [&](int v, int prnt) {
		in[v] = idd++;
		for (int u : g[v]) {
			if (u == prnt) {
				continue;
			}
			dfs(u, v); 
		}
		out[v] = idd - 1;
	};
	auto in_subtree = [&](int prnt, int v) {
		return in[prnt] <= in[v] && out[prnt] >= out[v];
	};
	
	auto traverse = [&]() {
		for (int i = 0; i < n; ++i) {
			g[i].clear();
		}
		for (int i = 0; i < n - 1; ++i) {
			int label = in_tree[i];
			g[eu[label]].push_back(ev[label]);
			g[ev[label]].push_back(eu[label]);
		}
		idd = 0;
		dfs(0, 0); 
	};
	
	int x = count_common_roads(in_tree);
	vector<int> state(m, -1);
	for (int i : out_tree) {
		vector<int> unknown;
		state[i] = 0;
		traverse();
		assert((int) in_tree.size() == n - 1); 
		for (int j = 0; j < n - 1; ++j) {
			int label = in_tree[j];
			vector<int> tmp = in_tree;
			tmp.erase(tmp.begin() + j);
			tmp.push_back(i);
			int ru = eu[label], rv = ev[label];
			if (in[ru] < in[rv]) {//u is a parent for v
				swap(ru, rv); 
			}
			int cnt = 0; 
			for (int k : {eu[i], ev[i]}) {
				cnt += in_subtree(ru, k);
			}
			if (cnt != 1 || state[label] == 1) {
				continue; 
			}
			int y = count_common_roads(tmp);
			if (state[label] == -1) {
				if (y > x) {
					state[label] = 0;
					state[i] = 1;
					in_tree = tmp;
					x = y;
					break;
				} else if (y == x) {
					unknown.push_back(label);
				} else {//y < x
					state[label] = 1;
					state[i] = 0;//doesn't matter
					break; 
				}
			} else {
				assert(state[label] == 0);
				if (y > x) {
					x = y; 
					state[i] = 1;
					in_tree = tmp;
				} else {
					assert(y == x);
					state[i] = 0;//doesn't matter
				}
				break;
			}
		}
		for (int j : unknown) {
			state[j] = state[i];
		}
	}
	return in_tree;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 344 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 344 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 3 ms 344 KB correct
15 Correct 3 ms 348 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 2 ms 464 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 2 ms 344 KB correct
21 Correct 2 ms 348 KB correct
22 Correct 2 ms 460 KB correct
23 Correct 2 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 344 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 2 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 2 ms 348 KB correct
33 Correct 2 ms 348 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 344 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 3 ms 344 KB correct
15 Correct 3 ms 348 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 2 ms 464 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 2 ms 344 KB correct
21 Correct 2 ms 348 KB correct
22 Correct 2 ms 460 KB correct
23 Correct 2 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 344 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 2 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 2 ms 348 KB correct
33 Correct 2 ms 348 KB correct
34 Correct 253 ms 1116 KB correct
35 Correct 232 ms 1112 KB correct
36 Correct 157 ms 860 KB correct
37 Correct 11 ms 344 KB correct
38 Correct 252 ms 1116 KB correct
39 Correct 238 ms 1116 KB correct
40 Correct 166 ms 856 KB correct
41 Correct 284 ms 1208 KB correct
42 Correct 306 ms 1204 KB correct
43 Correct 200 ms 860 KB correct
44 Correct 158 ms 600 KB correct
45 Correct 188 ms 860 KB correct
46 Correct 147 ms 604 KB correct
47 Correct 60 ms 604 KB correct
48 Correct 3 ms 348 KB correct
49 Correct 14 ms 508 KB correct
50 Correct 58 ms 604 KB correct
51 Correct 186 ms 856 KB correct
52 Correct 157 ms 600 KB correct
53 Correct 138 ms 604 KB correct
54 Correct 198 ms 856 KB correct
55 Correct 201 ms 924 KB correct
56 Correct 196 ms 924 KB correct
57 Correct 199 ms 856 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Incorrect 169 ms 2216 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 344 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 3 ms 344 KB correct
15 Correct 3 ms 348 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 2 ms 464 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 2 ms 344 KB correct
21 Correct 2 ms 348 KB correct
22 Correct 2 ms 460 KB correct
23 Correct 2 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 344 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 2 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 2 ms 348 KB correct
33 Correct 2 ms 348 KB correct
34 Correct 253 ms 1116 KB correct
35 Correct 232 ms 1112 KB correct
36 Correct 157 ms 860 KB correct
37 Correct 11 ms 344 KB correct
38 Correct 252 ms 1116 KB correct
39 Correct 238 ms 1116 KB correct
40 Correct 166 ms 856 KB correct
41 Correct 284 ms 1208 KB correct
42 Correct 306 ms 1204 KB correct
43 Correct 200 ms 860 KB correct
44 Correct 158 ms 600 KB correct
45 Correct 188 ms 860 KB correct
46 Correct 147 ms 604 KB correct
47 Correct 60 ms 604 KB correct
48 Correct 3 ms 348 KB correct
49 Correct 14 ms 508 KB correct
50 Correct 58 ms 604 KB correct
51 Correct 186 ms 856 KB correct
52 Correct 157 ms 600 KB correct
53 Correct 138 ms 604 KB correct
54 Correct 198 ms 856 KB correct
55 Correct 201 ms 924 KB correct
56 Correct 196 ms 924 KB correct
57 Correct 199 ms 856 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 348 KB correct
60 Incorrect 169 ms 2216 KB WA in grader: NO
61 Halted 0 ms 0 KB -