답안 #841788

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
841788 2023-09-02T05:39:27 Z emuyumi 가장 긴 여행 (IOI23_longesttrip) C++17
70 / 100
59 ms 1500 KB
#include "longesttrip.h"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<int> longest_trip(int N, int D) {

	if (D >= 2) {
		set<int> s;
		for (int i = 1; i < N; ++i) {
			s.insert(i);
		}
		vector<int> ans = {0};
		while (!s.empty()) {
			int v = ans.back();
			bool found = 0;
			for (int to : s) {
				bool b = are_connected({v}, {to});
				if (b) {
					s.erase(to);
					ans.push_back(to);
					found = 1;
					break;
				}
			}
			if (!found) {
				int last = *s.begin();
				ans.insert(ans.begin(), last);
				break;
			}
		}
		return ans;
	}

	// if (N == 3) {
	// 	int cnt = 0;
	// 	int x = 0;
	// 	auto go = [&](int i, int j) {
	// 		if (are_connected({i}, {j})) {
	// 			cnt++;
	// 			x ^= i;
	// 			x ^= j;
	// 		}
	// 	};
	// 	go(0, 1);
	// 	go(0, 2);
	// 	go(1, 2);
	// 	if (cnt == 0) return {0};
	// 	int other = 3^x;
	// 	set<int> s = {0, 1, 2};
	// 	s.erase(other);
	// 	int v1 = *s.begin();
	// 	int v2 = *(++s.begin());
	// 	if (cnt == 1) {
	// 		return {v1, v2};
	// 	}
	// 	return {v1, other, v2};
	// }

	// check if single component
	// if not, each component is clique, problem is then find largest component

	// otherwise, one component,
	// start with one node and keep trying to extend path by randomly querying
	// if gets stuck, that means past must be a clique, done

	int calls = 0;
	auto my_are_connected = [&](vector<int> a, vector<int> b) {
		calls ++;
		assert(calls <= 32460);
		return are_connected(a, b);
	};

	bool found = 0;
	vector<int> answer;
	using vi = vector<int>;
	auto halves = [&](vector<int> v) -> pair<vi, vi> {
		int m = v.size() / 2;
		return {{v.begin(), v.begin()+m}, {v.begin()+m, v.end()}};
	};

	vector<int> path = {0};
	vector<int> left(N-1);
	iota(left.begin(), left.end(), 1);
	while (left.size()) {
		random_shuffle(left.begin(), left.end());

		// cout << "! ";
		// for (int x : left) {
		// 	cout << x << " ";
		// }
		// cout << '\n';

		auto locate = [&](vector<int> src, vector<int> dest) -> int {
			while (src.size() != 1) {
				auto [a, b] = halves(src);
				if (my_are_connected(a, dest)) src = a;
				else src = b;
			}
			return src[0];
		};

		int v = path.back();
		int nxt = locate(left, {v});
		if (!my_are_connected({nxt}, {v})) {

			if (!my_are_connected(path, left)) {
				if (path.size() > left.size()) return path;
				else return left;
			}
			int a = locate(path, left);
			int b = locate(left, {a});

			vector<int> ans = left;
			ans.erase(find(ans.begin(), ans.end(), b));
			ans.push_back(b);
			int j = find(path.begin(), path.end(), a) - path.begin();
			if (j == 0) {
				for (int x : path) {
					ans.push_back(x);
				}
			}
			else {
				for (int i = j; i >= 0; --i) {
					ans.push_back(path[i]);
				}
				for (int i = path.size()-1; i > j; --i) {
					ans.push_back(path[i]);
				}
			}
			for (int i = 1; i < ans.size(); ++i) {
				assert(my_are_connected({ans[i]}, {ans[i-1]}));
			}
			return ans;

			// path.erase(find(path.begin(), path.end(), a));
			// left.erase(find(left.begin(), left.end(), b));
			// path.push_back(a);
			// left.insert(left.begin(), a);
			// for (int x : left) path.push_back(x);
			// return path;
			// int lst = end(path)[-2];
			// vector<int> ans = {v, lst};
			// for (int i = 0; i < N; ++i) {
			// 	if (i != v && i != lst) ans.push_back(i);
			// }
			// for (int i = 1; i < ans.size(); ++i) {
			// 	assert(my_are_connected({ans[i]}, {ans[i-1]}));
			// }
			// return ans;
		}
		else {
			path.push_back(nxt);
			left.erase(find(left.begin(), left.end(), nxt));
		}
	}
	for (int i = 1; i < path.size(); ++i) {
		assert(my_are_connected({path[i]}, {path[i-1]}));
	}
	return path;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:132:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |    for (int i = 1; i < ans.size(); ++i) {
      |                    ~~^~~~~~~~~~~~
longesttrip.cpp:158:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |  for (int i = 1; i < path.size(); ++i) {
      |                  ~~^~~~~~~~~~~~~
longesttrip.cpp:75:7: warning: unused variable 'found' [-Wunused-variable]
   75 |  bool found = 0;
      |       ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 13 ms 864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 5 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 6 ms 456 KB Output is correct
6 Correct 6 ms 348 KB Output is correct
7 Correct 6 ms 352 KB Output is correct
8 Correct 5 ms 352 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 5 ms 452 KB Output is correct
11 Correct 5 ms 600 KB Output is correct
12 Correct 5 ms 448 KB Output is correct
13 Correct 5 ms 444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 344 KB Output is correct
2 Correct 19 ms 344 KB Output is correct
3 Correct 31 ms 344 KB Output is correct
4 Correct 38 ms 704 KB Output is correct
5 Correct 49 ms 1004 KB Output is correct
6 Correct 11 ms 344 KB Output is correct
7 Correct 19 ms 344 KB Output is correct
8 Correct 29 ms 344 KB Output is correct
9 Correct 39 ms 344 KB Output is correct
10 Correct 52 ms 1212 KB Output is correct
11 Correct 50 ms 1164 KB Output is correct
12 Correct 49 ms 980 KB Output is correct
13 Correct 53 ms 1224 KB Output is correct
14 Correct 8 ms 344 KB Output is correct
15 Correct 11 ms 344 KB Output is correct
16 Correct 21 ms 344 KB Output is correct
17 Correct 27 ms 600 KB Output is correct
18 Correct 31 ms 344 KB Output is correct
19 Correct 37 ms 692 KB Output is correct
20 Correct 38 ms 700 KB Output is correct
21 Correct 49 ms 708 KB Output is correct
22 Correct 47 ms 1224 KB Output is correct
23 Correct 49 ms 1240 KB Output is correct
24 Correct 51 ms 632 KB Output is correct
25 Correct 9 ms 348 KB Output is correct
26 Correct 8 ms 352 KB Output is correct
27 Correct 17 ms 348 KB Output is correct
28 Correct 16 ms 344 KB Output is correct
29 Correct 14 ms 348 KB Output is correct
30 Correct 28 ms 436 KB Output is correct
31 Correct 20 ms 692 KB Output is correct
32 Correct 19 ms 688 KB Output is correct
33 Correct 30 ms 600 KB Output is correct
34 Correct 32 ms 600 KB Output is correct
35 Correct 26 ms 600 KB Output is correct
36 Correct 48 ms 744 KB Output is correct
37 Correct 45 ms 740 KB Output is correct
38 Correct 45 ms 964 KB Output is correct
39 Correct 39 ms 1220 KB Output is correct
40 Correct 30 ms 1012 KB Output is correct
41 Correct 30 ms 1244 KB Output is correct
42 Correct 29 ms 872 KB Output is correct
43 Correct 32 ms 956 KB Output is correct
44 Correct 30 ms 1500 KB Output is correct
45 Correct 15 ms 344 KB Output is correct
46 Correct 15 ms 344 KB Output is correct
47 Correct 18 ms 344 KB Output is correct
48 Correct 19 ms 344 KB Output is correct
49 Correct 20 ms 344 KB Output is correct
50 Correct 33 ms 432 KB Output is correct
51 Correct 24 ms 432 KB Output is correct
52 Correct 23 ms 692 KB Output is correct
53 Correct 37 ms 344 KB Output is correct
54 Correct 37 ms 600 KB Output is correct
55 Correct 35 ms 344 KB Output is correct
56 Correct 51 ms 1372 KB Output is correct
57 Correct 41 ms 976 KB Output is correct
58 Correct 33 ms 736 KB Output is correct
59 Correct 37 ms 1012 KB Output is correct
60 Correct 36 ms 740 KB Output is correct
61 Correct 36 ms 752 KB Output is correct
62 Correct 35 ms 1008 KB Output is correct
63 Correct 34 ms 1252 KB Output is correct
64 Correct 33 ms 992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 344 KB Output is correct
2 Correct 21 ms 344 KB Output is correct
3 Correct 29 ms 344 KB Output is correct
4 Partially correct 44 ms 708 KB Output is partially correct
5 Partially correct 50 ms 700 KB Output is partially correct
6 Correct 12 ms 344 KB Output is correct
7 Correct 18 ms 344 KB Output is correct
8 Correct 30 ms 344 KB Output is correct
9 Partially correct 34 ms 600 KB Output is partially correct
10 Partially correct 47 ms 976 KB Output is partially correct
11 Partially correct 54 ms 992 KB Output is partially correct
12 Partially correct 47 ms 712 KB Output is partially correct
13 Partially correct 47 ms 984 KB Output is partially correct
14 Correct 9 ms 344 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Correct 23 ms 344 KB Output is correct
17 Correct 27 ms 344 KB Output is correct
18 Correct 28 ms 344 KB Output is correct
19 Partially correct 38 ms 600 KB Output is partially correct
20 Partially correct 37 ms 856 KB Output is partially correct
21 Correct 10 ms 344 KB Output is correct
22 Correct 8 ms 344 KB Output is correct
23 Correct 12 ms 344 KB Output is correct
24 Correct 13 ms 344 KB Output is correct
25 Correct 12 ms 344 KB Output is correct
26 Correct 26 ms 432 KB Output is correct
27 Correct 20 ms 696 KB Output is correct
28 Correct 17 ms 688 KB Output is correct
29 Partially correct 33 ms 600 KB Output is partially correct
30 Partially correct 30 ms 600 KB Output is partially correct
31 Partially correct 26 ms 600 KB Output is partially correct
32 Correct 11 ms 344 KB Output is correct
33 Correct 13 ms 348 KB Output is correct
34 Correct 21 ms 344 KB Output is correct
35 Correct 20 ms 344 KB Output is correct
36 Correct 26 ms 344 KB Output is correct
37 Correct 32 ms 692 KB Output is correct
38 Partially correct 22 ms 436 KB Output is partially correct
39 Correct 25 ms 436 KB Output is correct
40 Partially correct 35 ms 600 KB Output is partially correct
41 Partially correct 37 ms 600 KB Output is partially correct
42 Partially correct 37 ms 1112 KB Output is partially correct
43 Partially correct 51 ms 736 KB Output is partially correct
44 Partially correct 59 ms 720 KB Output is partially correct
45 Partially correct 52 ms 1000 KB Output is partially correct
46 Partially correct 52 ms 960 KB Output is partially correct
47 Partially correct 44 ms 744 KB Output is partially correct
48 Partially correct 49 ms 1236 KB Output is partially correct
49 Partially correct 42 ms 1144 KB Output is partially correct
50 Partially correct 37 ms 744 KB Output is partially correct
51 Partially correct 32 ms 1220 KB Output is partially correct
52 Partially correct 29 ms 344 KB Output is partially correct
53 Partially correct 29 ms 748 KB Output is partially correct
54 Partially correct 30 ms 956 KB Output is partially correct
55 Partially correct 30 ms 700 KB Output is partially correct
56 Partially correct 46 ms 980 KB Output is partially correct
57 Partially correct 35 ms 1116 KB Output is partially correct
58 Partially correct 25 ms 1208 KB Output is partially correct
59 Partially correct 25 ms 704 KB Output is partially correct
60 Partially correct 29 ms 740 KB Output is partially correct
61 Partially correct 27 ms 1220 KB Output is partially correct
62 Partially correct 49 ms 740 KB Output is partially correct
63 Partially correct 41 ms 764 KB Output is partially correct
64 Partially correct 36 ms 964 KB Output is partially correct
65 Partially correct 41 ms 976 KB Output is partially correct
66 Partially correct 32 ms 740 KB Output is partially correct
67 Partially correct 31 ms 760 KB Output is partially correct
68 Partially correct 37 ms 984 KB Output is partially correct
69 Partially correct 31 ms 728 KB Output is partially correct
70 Partially correct 36 ms 724 KB Output is partially correct
71 Partially correct 51 ms 984 KB Output is partially correct
72 Partially correct 41 ms 704 KB Output is partially correct
73 Partially correct 39 ms 732 KB Output is partially correct
74 Partially correct 29 ms 700 KB Output is partially correct
75 Partially correct 31 ms 1152 KB Output is partially correct
76 Partially correct 32 ms 1228 KB Output is partially correct
77 Partially correct 53 ms 708 KB Output is partially correct
78 Partially correct 38 ms 992 KB Output is partially correct
79 Partially correct 40 ms 772 KB Output is partially correct
80 Partially correct 29 ms 1240 KB Output is partially correct
81 Partially correct 36 ms 720 KB Output is partially correct
82 Partially correct 32 ms 976 KB Output is partially correct