답안 #841796

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
841796 2023-09-02T06:14:48 Z emuyumi 가장 긴 여행 (IOI23_longesttrip) C++17
70 / 100
56 ms 1496 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]);
				}
			}
			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:155:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |  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 14 ms 1232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 4 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 5 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 5 ms 444 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 448 KB Output is correct
11 Correct 5 ms 448 KB Output is correct
12 Correct 5 ms 600 KB Output is correct
13 Correct 4 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 16 ms 344 KB Output is correct
3 Correct 27 ms 344 KB Output is correct
4 Correct 36 ms 856 KB Output is correct
5 Correct 52 ms 1232 KB Output is correct
6 Correct 12 ms 344 KB Output is correct
7 Correct 21 ms 596 KB Output is correct
8 Correct 32 ms 344 KB Output is correct
9 Correct 37 ms 600 KB Output is correct
10 Correct 51 ms 752 KB Output is correct
11 Correct 53 ms 1000 KB Output is correct
12 Correct 56 ms 1236 KB Output is correct
13 Correct 47 ms 976 KB Output is correct
14 Correct 10 ms 344 KB Output is correct
15 Correct 11 ms 344 KB Output is correct
16 Correct 24 ms 344 KB Output is correct
17 Correct 31 ms 344 KB Output is correct
18 Correct 27 ms 344 KB Output is correct
19 Correct 37 ms 600 KB Output is correct
20 Correct 35 ms 600 KB Output is correct
21 Correct 54 ms 980 KB Output is correct
22 Correct 55 ms 1220 KB Output is correct
23 Correct 50 ms 708 KB Output is correct
24 Correct 55 ms 488 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 11 ms 344 KB Output is correct
27 Correct 16 ms 344 KB Output is correct
28 Correct 14 ms 344 KB Output is correct
29 Correct 12 ms 344 KB Output is correct
30 Correct 24 ms 436 KB Output is correct
31 Correct 23 ms 688 KB Output is correct
32 Correct 17 ms 692 KB Output is correct
33 Correct 29 ms 444 KB Output is correct
34 Correct 32 ms 1016 KB Output is correct
35 Correct 26 ms 344 KB Output is correct
36 Correct 55 ms 1232 KB Output is correct
37 Correct 44 ms 992 KB Output is correct
38 Correct 43 ms 1000 KB Output is correct
39 Correct 35 ms 600 KB Output is correct
40 Correct 30 ms 980 KB Output is correct
41 Correct 26 ms 1228 KB Output is correct
42 Correct 26 ms 1252 KB Output is correct
43 Correct 30 ms 1488 KB Output is correct
44 Correct 28 ms 976 KB Output is correct
45 Correct 10 ms 344 KB Output is correct
46 Correct 11 ms 344 KB Output is correct
47 Correct 17 ms 344 KB Output is correct
48 Correct 13 ms 344 KB Output is correct
49 Correct 17 ms 344 KB Output is correct
50 Correct 25 ms 440 KB Output is correct
51 Correct 15 ms 436 KB Output is correct
52 Correct 18 ms 436 KB Output is correct
53 Correct 30 ms 600 KB Output is correct
54 Correct 30 ms 692 KB Output is correct
55 Correct 25 ms 344 KB Output is correct
56 Correct 50 ms 1008 KB Output is correct
57 Correct 35 ms 1232 KB Output is correct
58 Correct 23 ms 756 KB Output is correct
59 Correct 35 ms 1008 KB Output is correct
60 Correct 31 ms 1236 KB Output is correct
61 Correct 25 ms 976 KB Output is correct
62 Correct 31 ms 1476 KB Output is correct
63 Correct 28 ms 980 KB Output is correct
64 Correct 34 ms 720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 16 ms 344 KB Output is correct
3 Correct 31 ms 344 KB Output is correct
4 Partially correct 39 ms 704 KB Output is partially correct
5 Partially correct 50 ms 972 KB Output is partially correct
6 Correct 12 ms 344 KB Output is correct
7 Correct 16 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 49 ms 996 KB Output is partially correct
11 Partially correct 51 ms 716 KB Output is partially correct
12 Partially correct 43 ms 740 KB Output is partially correct
13 Partially correct 43 ms 740 KB Output is partially correct
14 Correct 8 ms 344 KB Output is correct
15 Correct 11 ms 344 KB Output is correct
16 Correct 22 ms 344 KB Output is correct
17 Correct 24 ms 344 KB Output is correct
18 Correct 29 ms 344 KB Output is correct
19 Partially correct 33 ms 600 KB Output is partially correct
20 Partially correct 37 ms 860 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 15 ms 344 KB Output is correct
24 Correct 12 ms 344 KB Output is correct
25 Correct 11 ms 344 KB Output is correct
26 Correct 26 ms 696 KB Output is correct
27 Correct 20 ms 436 KB Output is correct
28 Correct 17 ms 436 KB Output is correct
29 Partially correct 25 ms 344 KB Output is partially correct
30 Partially correct 28 ms 852 KB Output is partially correct
31 Partially correct 25 ms 344 KB Output is partially correct
32 Correct 10 ms 344 KB Output is correct
33 Correct 13 ms 344 KB Output is correct
34 Correct 15 ms 344 KB Output is correct
35 Correct 14 ms 344 KB Output is correct
36 Correct 17 ms 344 KB Output is correct
37 Correct 26 ms 436 KB Output is correct
38 Correct 19 ms 440 KB Output is correct
39 Correct 18 ms 692 KB Output is correct
40 Partially correct 29 ms 344 KB Output is partially correct
41 Partially correct 32 ms 600 KB Output is partially correct
42 Partially correct 30 ms 600 KB Output is partially correct
43 Partially correct 53 ms 720 KB Output is partially correct
44 Partially correct 56 ms 880 KB Output is partially correct
45 Partially correct 53 ms 740 KB Output is partially correct
46 Partially correct 50 ms 1260 KB Output is partially correct
47 Partially correct 48 ms 896 KB Output is partially correct
48 Partially correct 39 ms 708 KB Output is partially correct
49 Partially correct 42 ms 976 KB Output is partially correct
50 Partially correct 35 ms 1248 KB Output is partially correct
51 Partially correct 36 ms 964 KB Output is partially correct
52 Partially correct 27 ms 980 KB Output is partially correct
53 Partially correct 36 ms 988 KB Output is partially correct
54 Partially correct 29 ms 1468 KB Output is partially correct
55 Partially correct 34 ms 1496 KB Output is partially correct
56 Partially correct 52 ms 756 KB Output is partially correct
57 Partially correct 36 ms 980 KB Output is partially correct
58 Partially correct 28 ms 1120 KB Output is partially correct
59 Partially correct 28 ms 640 KB Output is partially correct
60 Partially correct 31 ms 976 KB Output is partially correct
61 Partially correct 27 ms 976 KB Output is partially correct
62 Partially correct 46 ms 992 KB Output is partially correct
63 Partially correct 36 ms 1400 KB Output is partially correct
64 Partially correct 23 ms 992 KB Output is partially correct
65 Partially correct 35 ms 996 KB Output is partially correct
66 Partially correct 30 ms 992 KB Output is partially correct
67 Partially correct 30 ms 720 KB Output is partially correct
68 Partially correct 28 ms 1244 KB Output is partially correct
69 Partially correct 28 ms 484 KB Output is partially correct
70 Partially correct 29 ms 1020 KB Output is partially correct
71 Partially correct 48 ms 444 KB Output is partially correct
72 Partially correct 36 ms 736 KB Output is partially correct
73 Partially correct 30 ms 856 KB Output is partially correct
74 Partially correct 30 ms 996 KB Output is partially correct
75 Partially correct 30 ms 1232 KB Output is partially correct
76 Partially correct 25 ms 992 KB Output is partially correct
77 Partially correct 38 ms 1248 KB Output is partially correct
78 Partially correct 38 ms 1216 KB Output is partially correct
79 Partially correct 37 ms 996 KB Output is partially correct
80 Partially correct 31 ms 1216 KB Output is partially correct
81 Partially correct 39 ms 1240 KB Output is partially correct
82 Partially correct 32 ms 996 KB Output is partially correct