답안 #782461

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
782461 2023-07-14T02:19:16 Z GusterGoose27 Simurgh (IOI17_simurgh) C++17
51 / 100
114 ms 4056 KB
#include "simurgh.h"

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;

const int MAXN = 501;
vector<pii> edges[MAXN];
set<int> ans;
int n, m;
int uf[MAXN];
int sz[MAXN];
int vis[MAXN];

int find(int i) {
	return uf[i] == i ? i : uf[i] = find(uf[i]);
}

void dfs(int s, int col) {
	vis[s] = col;
	for (pii p: edges[s]) {
		if (vis[p.first]) continue;
		dfs(p.first, col);
	}
}

vector<int> queries;
vector<int> u, v;
vector<int> undo;

void merge(int a, int b) {
	a = find(a);
	b = find(b);
	assert(a != b);
	if (sz[a] < sz[b]) swap(a, b);
	uf[b] = a;
	sz[a] += sz[b];
	undo.push_back(b);
}

void rollback() {
	int v = undo.back(); undo.pop_back();
	sz[uf[v]] -= sz[v];
	uf[v] = v;
}

void make_mst(int cur, int col) {
	int num = 0;
	for (int i = 0; i < m; i++) {
		int a = u[i], b = v[i];
		if (find(a) == find(b) || vis[b] == col && a == cur || vis[a] == col && b == cur) continue;
		merge(a, b);
		num++;
		queries.push_back(i);
	}
	for (int i = 0; i < num; i++) rollback();
}

void get(vector<pii> &group, int col, int cur) {
	queries.clear();
	make_mst(cur, col);
	vector<int> vals;
	int mx = 0;
	bool one_big = 0;
	for (pii p: group) {
		if (p.first < cur) {
			if (ans.find(p.second) != ans.end()) {
				if (one_big) {
					vals.push_back(-1);
					continue;
				}
				one_big = 1;
			}
			else {
				vals.push_back(-1);
				continue;
			}
		}
		queries.push_back(p.second);
		vals.push_back(count_common_roads(queries));
		mx = max(mx, vals.back());
		queries.pop_back();
	}
	for (int i = 0; i < group.size(); i++) {
		if (vals[i] == mx) ans.insert(group[i].second);
	}
	// if (group.size() == 1) {
	// 	ans.insert(group[0].second);
	// 	return 1;
	// }
	// vector<pii> left, right;
	// for (int i = 0; i < group.size()/2; i += 2) {
	// 	merge(group[i].first, group[i+1].first);
	// 	left.push_back(group[i]);
	// 	right.push_back(group[i+1]);
	// }
	// if (group.size() & 1) {
	// 	left.push_back(group.back());
	// 	right.push_back(group.back());
	// }
	// queries.clear();
	// make_mst(cur, col); // ignore stuff from current to subtree
	// for (pii p: left) queries.push_back(p.second);
	// int va = count_common_roads(queries);
	// for (pii p: left) queries.pop_back();
	// for (pii p: right) queries.push_back(p.second);
	// int vb = count_common_roads(queries);
	// if (va < vb) {
	// 	swap(va, vb);
	// 	swap(left, right);
	// }
	// assert(va >= vb);
	// int num = get(left, col, cur);
	// if (!(group.size() & 1)) {
	// 	assert(va-num <= vb);
	// 	if (va-num < vb) return num+get(right, col, cur);
	// 	return num;
	// }
	// else {
	// 	bool ex = (ans.find(group.back().second) != ans.end());
	// 	num -= ex;
	// 	merge(group[0].first, group.back().first);
	// 	right.pop_back();
	// 	assert(va-num <= vb);
	// 	if (va-num < vb) return num+ex+get(right, col, cur);
	// 	return num+ex;
	// }
	// assert(false);
}

void solve(int i) {
	fill(vis, vis+n, 0);
	int t = 0;
	vis[i] = 1;
	for (pii e: edges[i]) {
		if (vis[e.first]) continue;
		dfs(e.first, ++t);
	}
	vector<vector<pii>> groups(t);
	for (pii e: edges[i]) {
		groups[vis[e.first]-1].push_back(e);
	}
	for (int j = 0; j < groups.size(); j++) {
		iota(uf, uf+n, 0);
		fill(sz, sz+n, 1);
		get(groups[j], j+1, i);
	}
}

vector<int> find_roads(int N, vector<int> U, vector<int> V) {
	n = N;
	m = U.size();
	for (int i = 0; i < m; i++) {
		u.push_back(U[i]);
		v.push_back(V[i]);
	}
	for (int i = 0; i < m; i++) {
		edges[u[i]].push_back(pii(v[i], i));
		edges[v[i]].push_back(pii(u[i], i));
	}
	for (int i = 0; i < n; i++) solve(i);
	vector<int> res;
	for (int v: ans) res.push_back(v);
	return res;
}

Compilation message

simurgh.cpp: In function 'void make_mst(int, int)':
simurgh.cpp:52:43: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   52 |   if (find(a) == find(b) || vis[b] == col && a == cur || vis[a] == col && b == cur) continue;
      |                             ~~~~~~~~~~~~~~^~~~~~~~~~~
simurgh.cpp:52:72: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   52 |   if (find(a) == find(b) || vis[b] == col && a == cur || vis[a] == col && b == cur) continue;
      |                                                          ~~~~~~~~~~~~~~^~~~~~~~~~~
simurgh.cpp: In function 'void get(std::vector<std::pair<int, int> >&, int, int)':
simurgh.cpp:85:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |  for (int i = 0; i < group.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~
simurgh.cpp: In function 'void solve(int)':
simurgh.cpp:144:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |  for (int j = 0; j < groups.size(); j++) {
      |                  ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 2 ms 340 KB correct
15 Correct 2 ms 340 KB correct
16 Correct 2 ms 340 KB correct
17 Correct 1 ms 340 KB correct
18 Correct 1 ms 340 KB correct
19 Correct 1 ms 340 KB correct
20 Correct 2 ms 340 KB correct
21 Correct 1 ms 340 KB correct
22 Correct 1 ms 340 KB correct
23 Correct 1 ms 340 KB correct
24 Correct 1 ms 340 KB correct
25 Correct 1 ms 212 KB correct
26 Correct 2 ms 340 KB correct
27 Correct 2 ms 340 KB correct
28 Correct 1 ms 340 KB correct
29 Correct 1 ms 212 KB correct
30 Correct 1 ms 340 KB correct
31 Correct 1 ms 340 KB correct
32 Correct 1 ms 340 KB correct
33 Correct 1 ms 340 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 2 ms 340 KB correct
15 Correct 2 ms 340 KB correct
16 Correct 2 ms 340 KB correct
17 Correct 1 ms 340 KB correct
18 Correct 1 ms 340 KB correct
19 Correct 1 ms 340 KB correct
20 Correct 2 ms 340 KB correct
21 Correct 1 ms 340 KB correct
22 Correct 1 ms 340 KB correct
23 Correct 1 ms 340 KB correct
24 Correct 1 ms 340 KB correct
25 Correct 1 ms 212 KB correct
26 Correct 2 ms 340 KB correct
27 Correct 2 ms 340 KB correct
28 Correct 1 ms 340 KB correct
29 Correct 1 ms 212 KB correct
30 Correct 1 ms 340 KB correct
31 Correct 1 ms 340 KB correct
32 Correct 1 ms 340 KB correct
33 Correct 1 ms 340 KB correct
34 Correct 106 ms 1492 KB correct
35 Correct 105 ms 1752 KB correct
36 Correct 72 ms 1524 KB correct
37 Correct 8 ms 328 KB correct
38 Correct 111 ms 1740 KB correct
39 Correct 97 ms 1656 KB correct
40 Correct 90 ms 1492 KB correct
41 Correct 107 ms 1772 KB correct
42 Correct 114 ms 1768 KB correct
43 Correct 58 ms 1236 KB correct
44 Correct 48 ms 980 KB correct
45 Correct 54 ms 980 KB correct
46 Correct 42 ms 904 KB correct
47 Correct 25 ms 596 KB correct
48 Correct 4 ms 324 KB correct
49 Correct 9 ms 340 KB correct
50 Correct 24 ms 588 KB correct
51 Correct 53 ms 1040 KB correct
52 Correct 48 ms 984 KB correct
53 Correct 43 ms 904 KB correct
54 Correct 56 ms 1232 KB correct
55 Correct 53 ms 980 KB correct
56 Correct 62 ms 1048 KB correct
57 Correct 52 ms 1056 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Incorrect 74 ms 4056 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 2 ms 340 KB correct
15 Correct 2 ms 340 KB correct
16 Correct 2 ms 340 KB correct
17 Correct 1 ms 340 KB correct
18 Correct 1 ms 340 KB correct
19 Correct 1 ms 340 KB correct
20 Correct 2 ms 340 KB correct
21 Correct 1 ms 340 KB correct
22 Correct 1 ms 340 KB correct
23 Correct 1 ms 340 KB correct
24 Correct 1 ms 340 KB correct
25 Correct 1 ms 212 KB correct
26 Correct 2 ms 340 KB correct
27 Correct 2 ms 340 KB correct
28 Correct 1 ms 340 KB correct
29 Correct 1 ms 212 KB correct
30 Correct 1 ms 340 KB correct
31 Correct 1 ms 340 KB correct
32 Correct 1 ms 340 KB correct
33 Correct 1 ms 340 KB correct
34 Correct 106 ms 1492 KB correct
35 Correct 105 ms 1752 KB correct
36 Correct 72 ms 1524 KB correct
37 Correct 8 ms 328 KB correct
38 Correct 111 ms 1740 KB correct
39 Correct 97 ms 1656 KB correct
40 Correct 90 ms 1492 KB correct
41 Correct 107 ms 1772 KB correct
42 Correct 114 ms 1768 KB correct
43 Correct 58 ms 1236 KB correct
44 Correct 48 ms 980 KB correct
45 Correct 54 ms 980 KB correct
46 Correct 42 ms 904 KB correct
47 Correct 25 ms 596 KB correct
48 Correct 4 ms 324 KB correct
49 Correct 9 ms 340 KB correct
50 Correct 24 ms 588 KB correct
51 Correct 53 ms 1040 KB correct
52 Correct 48 ms 984 KB correct
53 Correct 43 ms 904 KB correct
54 Correct 56 ms 1232 KB correct
55 Correct 53 ms 980 KB correct
56 Correct 62 ms 1048 KB correct
57 Correct 52 ms 1056 KB correct
58 Correct 0 ms 212 KB correct
59 Correct 0 ms 212 KB correct
60 Incorrect 74 ms 4056 KB WA in grader: NO
61 Halted 0 ms 0 KB -