Submission #840765

# Submission time Handle Problem Language Result Execution time Memory
840765 2023-08-31T16:50:23 Z happypotato Longest Trip (IOI23_longesttrip) C++17
100 / 100
18 ms 716 KB
#include "longesttrip.h"

#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
// #define int long long // remove when necessary
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define ll long long
const int mxN = 256;
vector<int> adj[mxN];
vector<int> grp[mxN];
int conn[mxN][mxN];
int callcnt;
bool con(int x, int y) {
	// cerr << "CALLx " << x << ' ' << y << endl;
	if (x == y) return (conn[x][x] = 1);
	if (conn[x][y] != -1) return conn[x][y];
	callcnt++;
	return (conn[x][y] = conn[y][x] = are_connected({x}, {y}));
}
bool Con(vector<int> x, vector<int> y) {
	if (x.size() == 1 && y.size() == 1) {
		return con(x[0], y[0]);
	}
	// cerr << "CALL\n";
	// for (int &cur : x) cerr << cur << ' ';
	// cerr << endl;
	// for (int &cur : y) cerr << cur << ' ';
	// cerr << endl;
	callcnt++;
	return are_connected(x, y);
}
int n;
vector<int> nxt[mxN];
bool vis[mxN];
void resetvis() {
	for (int i = 0; i < n; i++) vis[i] = false;
}
void reset() {
	callcnt = 0;
	for (int i = 0; i < n; i++) {
		adj[i].clear();
		grp[i].clear();
		for (int j = 0; j < n; j++) {
			conn[i][j] = -1;
		}
		nxt[i].clear();
		vis[i] = false;
	}
}
vector<int> GetPath(int st) {
	resetvis();
	vector<int> res;
	while (true) {
		res.pb(st);
		vis[st] = true;
		bool found = false;
		for (int &v : nxt[st]) {
			if (!vis[v]) {
				st = v;
				found = true;
				break;
			}
		}
		if (!found) break;
	}
	return res;
}
vector<int> MakePath(vector<int> &v) {
	mt19937 rng(time(NULL));
	shuffle(v.begin(), v.end(), rng);
	if (v.size() == 1) return {v[0]};
	if (v.size() == 2) {
		if (con(v[0], v[1])) return {v[0], v[1]};
		else return {v[0]};
	}
	if (v.size() == 3) {
		if (con(v[0], v[1]) && con(v[0], v[2])) return {v[1], v[0], v[2]};
		if (con(v[0], v[1]) && con(v[1], v[2])) return {v[0], v[1], v[2]};
		if (con(v[0], v[2]) && con(v[1], v[2])) return {v[0], v[2], v[1]};
		if (con(v[0], v[1])) return {v[0], v[1]};
		if (con(v[0], v[2])) return {v[0], v[2]};
		if (con(v[1], v[2])) return {v[1], v[2]};
		return {v[0]};
	}
	for (int i = 0; i < n; i++) {
		nxt[i].clear();
	}
	pair<pii, pii> cur = {{v[0], v[0]}, {v[1], v[1]}};
	for (int i = 2; i < (int)(v.size()); i++) {
		// if (rng() % 2) swap(cur.ff, cur.ss);
		if (con(cur.ff.ff, v[i])) {
			nxt[cur.ff.ff].pb(v[i]);
			nxt[v[i]].pb(cur.ff.ff);
			cur.ff.ff = v[i];
		} else if (con(cur.ff.ff, cur.ss.ff)) {
			nxt[cur.ff.ff].pb(cur.ss.ff);
			nxt[cur.ss.ff].pb(cur.ff.ff);
			cur.ff.ff = cur.ss.ss;
			cur.ss = {v[i], v[i]};
		} else {
			conn[cur.ss.ff][v[i]] = conn[v[i]][cur.ss.ff] = 1;
			nxt[cur.ss.ff].pb(v[i]);
			nxt[v[i]].pb(cur.ss.ff);
			cur.ss.ff = v[i];
		}
	}
	vector<int> res[2];
	res[0] = GetPath(cur.ff.ff);
	res[1] = GetPath(cur.ss.ff);
	if (!Con(res[0], res[1])) {
		return (res[0].size() >= res[1].size() ? res[0] : res[1]);
	}
	int st = -1;
	vector<int> temp[2];
	temp[0].pb(cur.ff.ff); if (cur.ff.ff != cur.ff.ss) temp[0].pb(cur.ff.ss);
	temp[1].pb(cur.ss.ff); if (cur.ss.ff != cur.ss.ss) temp[1].pb(cur.ss.ss);
	if (Con(temp[0], temp[1])) {
		if (con(cur.ff.ff, cur.ss.ff)) {
			nxt[cur.ff.ff].pb(cur.ss.ff);
			nxt[cur.ss.ff].pb(cur.ff.ff);
			st = cur.ff.ss;
		} else if (con(cur.ff.ff, cur.ss.ss)) {
			nxt[cur.ff.ff].pb(cur.ss.ss);
			nxt[cur.ss.ss].pb(cur.ff.ff);
			st = cur.ff.ss;
		} else if (con(cur.ff.ss, cur.ss.ff)) {
			nxt[cur.ff.ss].pb(cur.ss.ff);
			nxt[cur.ss.ff].pb(cur.ff.ss);
			st = cur.ff.ff;
		} else if (con(cur.ff.ss, cur.ss.ss)) {
			nxt[cur.ff.ss].pb(cur.ss.ss);
			nxt[cur.ss.ss].pb(cur.ff.ss);
			st = cur.ff.ff;
		}
	} else {
		vector<int> take[2];
		take[0] = res[0];
		take[1] = res[1];

		// cerr << "res0: ";
		// for (int &x : res[0]) cerr << x << ' ';
		// cerr << endl;
		// cerr << "res1: ";
		// for (int &x : res[1]) cerr << x << ' ';
		// cerr << endl;

		vector<int> split[2];
		while (take[0].size() >= 2) {
			split[0].clear();
			split[1].clear();
			int cut = (int)(take[0].size()) / 2;
			for (int i = 0; i < cut; i++) split[0].pb(take[0][i]);
			for (int i = cut; i < (int)(take[0].size()); i++) split[1].pb(take[0][i]);
			if (Con(split[0], take[1])) take[0] = split[0];
			else take[0] = split[1];
		}
		while (take[1].size() >= 2) {
			split[0].clear();
			split[1].clear();
			int cut = (int)(take[1].size()) / 2;
			for (int i = 0; i < cut; i++) split[0].pb(take[1][i]);
			for (int i = cut; i < (int)(take[1].size()); i++) split[1].pb(take[1][i]);
			if (Con(take[0], split[0])) take[1] = split[0];
			else take[1] = split[1];
		}

		bool found = false;
		for (int i = 0; i < (int)(res[0].size()); i++) {
			if (res[0][i] != take[0][0]) continue;
			for (int j = 0; j < (int)(res[1].size()); j++) {
				if (res[1][j] != take[1][0]) continue;
				if (true) {
					// cerr << "FOUND " << res[0][i] << ' ' << res[1][j] << endl;
					nxt[res[0][i]].pb(res[1][j]);
					nxt[res[1][j]].pb(res[0][i]);
 
					if (res[0].size() >= 3) {
						nxt[cur.ff.ff].pb(cur.ff.ss);
						nxt[cur.ff.ss].pb(cur.ff.ff);
						// cerr << "RES0 ";
						// for (int &x : res[0]) cerr << x << ' ';
						// cerr << endl;
						st = (i == 0 ? res[0].back() : res[0][i - 1]);
						// cerr << "ST " << st << endl;
						vector<int>::iterator it;
						for (it = nxt[st].begin(); it != nxt[st].end(); ++it) {
							if (*it == res[0][i]) {
								nxt[st].erase(it);
								break;
							}
						}
						for (it = nxt[res[0][i]].begin(); it != nxt[res[0][i]].end(); ++it) {
							if (*it == st) {
								nxt[res[0][i]].erase(it);
								break;
							}
						}
					} else {
						st = (res[0].size() == 2 ? res[0][i ^ 1] : res[0][i]);
					}
 
					if (res[1].size() >= 3) {
						nxt[cur.ss.ff].pb(cur.ss.ss);
						nxt[cur.ss.ss].pb(cur.ss.ff);
						int tar = (j == 0 ? res[1].back() : res[1][j + 1]);
						vector<int>::iterator it;
						for (it = nxt[tar].begin(); it != nxt[tar].end(); ++it) {
							if (*it == res[1][j]) {
								nxt[tar].erase(it);
								break;
							}
						}
						for (it = nxt[res[1][j]].begin(); it != nxt[res[1][j]].end(); ++it) {
							if (*it == tar) {
								nxt[res[1][j]].erase(it);
								break;
							}
						}
					}
 
					found = true;
					break;
				}
			}
			if (found) break;
		}

		// cerr << "END\n";
	}

	// cerr << "find path for: ";
	// for (int &x : v) cerr << x << ' ';
	// cerr << endl;
	
	vector<int> fin = GetPath(st);

	// cerr << "st: " << st << endl;
	// cerr << "result: ";
	// for (int &x : res) cerr << x << ' ';
	// cerr << endl;

	return fin;
}
vector<int> longest_trip(int N, int D)
{
	n = N;
	reset();

	vector<int> res(n);
	for (int i = 0; i < n; i++) res[i] = i;
	return MakePath(res);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 2 ms 584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 9 ms 336 KB Output is correct
3 Correct 7 ms 380 KB Output is correct
4 Correct 9 ms 336 KB Output is correct
5 Correct 9 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 460 KB Output is correct
2 Correct 9 ms 208 KB Output is correct
3 Correct 9 ms 336 KB Output is correct
4 Correct 7 ms 464 KB Output is correct
5 Correct 5 ms 464 KB Output is correct
6 Correct 9 ms 208 KB Output is correct
7 Correct 9 ms 336 KB Output is correct
8 Correct 10 ms 460 KB Output is correct
9 Correct 9 ms 336 KB Output is correct
10 Correct 7 ms 464 KB Output is correct
11 Correct 10 ms 592 KB Output is correct
12 Correct 6 ms 592 KB Output is correct
13 Correct 7 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 208 KB Output is correct
2 Correct 12 ms 336 KB Output is correct
3 Correct 9 ms 336 KB Output is correct
4 Correct 7 ms 336 KB Output is correct
5 Correct 7 ms 464 KB Output is correct
6 Correct 10 ms 208 KB Output is correct
7 Correct 8 ms 336 KB Output is correct
8 Correct 10 ms 336 KB Output is correct
9 Correct 8 ms 336 KB Output is correct
10 Correct 9 ms 464 KB Output is correct
11 Correct 9 ms 464 KB Output is correct
12 Correct 7 ms 592 KB Output is correct
13 Correct 9 ms 592 KB Output is correct
14 Correct 10 ms 208 KB Output is correct
15 Correct 11 ms 208 KB Output is correct
16 Correct 13 ms 336 KB Output is correct
17 Correct 11 ms 336 KB Output is correct
18 Correct 10 ms 336 KB Output is correct
19 Correct 11 ms 336 KB Output is correct
20 Correct 8 ms 336 KB Output is correct
21 Correct 7 ms 592 KB Output is correct
22 Correct 11 ms 592 KB Output is correct
23 Correct 12 ms 612 KB Output is correct
24 Correct 6 ms 464 KB Output is correct
25 Correct 10 ms 208 KB Output is correct
26 Correct 11 ms 208 KB Output is correct
27 Correct 10 ms 336 KB Output is correct
28 Correct 7 ms 336 KB Output is correct
29 Correct 10 ms 336 KB Output is correct
30 Correct 7 ms 336 KB Output is correct
31 Correct 9 ms 336 KB Output is correct
32 Correct 11 ms 336 KB Output is correct
33 Correct 10 ms 336 KB Output is correct
34 Correct 8 ms 336 KB Output is correct
35 Correct 8 ms 336 KB Output is correct
36 Correct 7 ms 592 KB Output is correct
37 Correct 9 ms 592 KB Output is correct
38 Correct 11 ms 592 KB Output is correct
39 Correct 8 ms 592 KB Output is correct
40 Correct 11 ms 592 KB Output is correct
41 Correct 10 ms 592 KB Output is correct
42 Correct 11 ms 592 KB Output is correct
43 Correct 12 ms 716 KB Output is correct
44 Correct 8 ms 464 KB Output is correct
45 Correct 16 ms 336 KB Output is correct
46 Correct 11 ms 208 KB Output is correct
47 Correct 6 ms 336 KB Output is correct
48 Correct 7 ms 336 KB Output is correct
49 Correct 9 ms 336 KB Output is correct
50 Correct 11 ms 460 KB Output is correct
51 Correct 12 ms 336 KB Output is correct
52 Correct 13 ms 336 KB Output is correct
53 Correct 8 ms 336 KB Output is correct
54 Correct 11 ms 336 KB Output is correct
55 Correct 11 ms 336 KB Output is correct
56 Correct 6 ms 592 KB Output is correct
57 Correct 10 ms 584 KB Output is correct
58 Correct 12 ms 592 KB Output is correct
59 Correct 12 ms 592 KB Output is correct
60 Correct 6 ms 636 KB Output is correct
61 Correct 14 ms 596 KB Output is correct
62 Correct 9 ms 592 KB Output is correct
63 Correct 9 ms 592 KB Output is correct
64 Correct 12 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 11 ms 336 KB Output is correct
3 Correct 9 ms 336 KB Output is correct
4 Correct 7 ms 512 KB Output is correct
5 Correct 8 ms 592 KB Output is correct
6 Correct 10 ms 208 KB Output is correct
7 Correct 7 ms 336 KB Output is correct
8 Correct 6 ms 336 KB Output is correct
9 Correct 6 ms 336 KB Output is correct
10 Correct 8 ms 580 KB Output is correct
11 Correct 9 ms 592 KB Output is correct
12 Correct 7 ms 592 KB Output is correct
13 Correct 7 ms 592 KB Output is correct
14 Correct 11 ms 336 KB Output is correct
15 Correct 11 ms 208 KB Output is correct
16 Correct 11 ms 336 KB Output is correct
17 Correct 11 ms 336 KB Output is correct
18 Correct 6 ms 336 KB Output is correct
19 Correct 8 ms 336 KB Output is correct
20 Correct 11 ms 336 KB Output is correct
21 Correct 12 ms 208 KB Output is correct
22 Correct 18 ms 208 KB Output is correct
23 Correct 8 ms 336 KB Output is correct
24 Correct 8 ms 336 KB Output is correct
25 Correct 9 ms 336 KB Output is correct
26 Correct 8 ms 336 KB Output is correct
27 Correct 7 ms 336 KB Output is correct
28 Correct 8 ms 396 KB Output is correct
29 Correct 9 ms 336 KB Output is correct
30 Correct 7 ms 336 KB Output is correct
31 Correct 10 ms 336 KB Output is correct
32 Correct 11 ms 208 KB Output is correct
33 Correct 13 ms 208 KB Output is correct
34 Correct 14 ms 336 KB Output is correct
35 Correct 13 ms 336 KB Output is correct
36 Correct 14 ms 336 KB Output is correct
37 Correct 10 ms 388 KB Output is correct
38 Correct 14 ms 396 KB Output is correct
39 Correct 13 ms 336 KB Output is correct
40 Correct 8 ms 336 KB Output is correct
41 Correct 7 ms 432 KB Output is correct
42 Correct 12 ms 336 KB Output is correct
43 Correct 11 ms 592 KB Output is correct
44 Correct 11 ms 464 KB Output is correct
45 Correct 8 ms 592 KB Output is correct
46 Correct 6 ms 600 KB Output is correct
47 Correct 7 ms 592 KB Output is correct
48 Correct 10 ms 592 KB Output is correct
49 Correct 11 ms 592 KB Output is correct
50 Correct 16 ms 584 KB Output is correct
51 Correct 8 ms 592 KB Output is correct
52 Correct 10 ms 464 KB Output is correct
53 Correct 11 ms 592 KB Output is correct
54 Correct 8 ms 584 KB Output is correct
55 Correct 14 ms 640 KB Output is correct
56 Correct 10 ms 592 KB Output is correct
57 Correct 10 ms 592 KB Output is correct
58 Correct 14 ms 584 KB Output is correct
59 Correct 7 ms 464 KB Output is correct
60 Correct 13 ms 584 KB Output is correct
61 Correct 9 ms 592 KB Output is correct
62 Correct 8 ms 464 KB Output is correct
63 Correct 10 ms 592 KB Output is correct
64 Correct 9 ms 600 KB Output is correct
65 Correct 13 ms 592 KB Output is correct
66 Correct 7 ms 596 KB Output is correct
67 Correct 9 ms 592 KB Output is correct
68 Correct 14 ms 588 KB Output is correct
69 Correct 13 ms 592 KB Output is correct
70 Correct 10 ms 592 KB Output is correct
71 Correct 7 ms 592 KB Output is correct
72 Correct 12 ms 640 KB Output is correct
73 Correct 10 ms 592 KB Output is correct
74 Correct 13 ms 588 KB Output is correct
75 Correct 12 ms 592 KB Output is correct
76 Correct 11 ms 592 KB Output is correct
77 Correct 10 ms 592 KB Output is correct
78 Correct 13 ms 596 KB Output is correct
79 Correct 9 ms 592 KB Output is correct
80 Correct 13 ms 588 KB Output is correct
81 Correct 10 ms 592 KB Output is correct
82 Correct 15 ms 584 KB Output is correct