Submission #277576

# Submission time Handle Problem Language Result Execution time Memory
277576 2020-08-21T06:02:02 Z 임성재(#5118) Travelling Salesperson (CCO20_day2problem1) C++17
25 / 25
755 ms 36344 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int inf = 1e9;
const ll INF = 1e18;

int n;
int g[2010][2010];
vector<int> ans;

void f(int r) {
	int s = r == 1 ? 2 : 1;

	list<int> l;
	
	l.push_back(s);
	auto it = l.begin();

	for(int i=1; i<=n; i++) {
		if(i == r || i == s) continue;

		int m = *it;

		if(g[m][i] == 1) {
			l.insert(next(it), i);

			it++;
			if(next(it) != l.end() && g[*next(it)][*it] == 1) it++;
		}
		else {
			l.insert(it, i);
			it--;
			if(it != l.begin() && g[*prev(it)][*it] == 2) it--;
 		}
	}

	ans.clear();
	for(auto i : l) ans.eb(i);
	
	if(g[ans.front()][ans.back()] == 2) {
		reverse(ans.begin()+1, ans.end());
		reverse(all(ans));
	}

	if(g[ans.back()][r] == 1) reverse(ans.begin(), ans.end()-1);

	reverse(all(ans));

	cout << ans.size() + 1 << "\n";
	cout << r << " ";
	for(auto i : ans) {
		cout << i << " ";
	}
	cout << "\n";
}

int main() {
	fast;

	cin >> n;

	for(int i=2; i<=n; i++) {
		string s;
		cin >> s;

		for(int j=1; j<i; j++) {
			g[i][j] = g[j][i] = (s[j-1] == 'R') + 1;
		}
	}

	for(int i=1; i<=n; i++) {
		f(i);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 512 ms 35452 KB Output is correct
17 Correct 517 ms 36344 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 533 ms 35936 KB Output is correct
20 Correct 665 ms 35960 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 501 ms 35960 KB Output is correct
23 Correct 591 ms 35720 KB Output is correct
24 Correct 2 ms 768 KB Output is correct
25 Correct 509 ms 35704 KB Output is correct
26 Correct 659 ms 35840 KB Output is correct
27 Correct 35 ms 4348 KB Output is correct
28 Correct 561 ms 35936 KB Output is correct
29 Correct 609 ms 35792 KB Output is correct
30 Correct 593 ms 35964 KB Output is correct
31 Correct 555 ms 35816 KB Output is correct
32 Correct 584 ms 35704 KB Output is correct
33 Correct 572 ms 35884 KB Output is correct
34 Correct 509 ms 35832 KB Output is correct
35 Correct 531 ms 36064 KB Output is correct
36 Correct 524 ms 36024 KB Output is correct
37 Correct 518 ms 35720 KB Output is correct
38 Correct 503 ms 35960 KB Output is correct
39 Correct 513 ms 35704 KB Output is correct
40 Correct 501 ms 36088 KB Output is correct
41 Correct 509 ms 35948 KB Output is correct
42 Correct 524 ms 35736 KB Output is correct
43 Correct 508 ms 35960 KB Output is correct
44 Correct 517 ms 35704 KB Output is correct
45 Correct 511 ms 35960 KB Output is correct
46 Correct 550 ms 35840 KB Output is correct
47 Correct 559 ms 35964 KB Output is correct
48 Correct 536 ms 35888 KB Output is correct
49 Correct 515 ms 35960 KB Output is correct
50 Correct 498 ms 35832 KB Output is correct
51 Correct 598 ms 35704 KB Output is correct
52 Correct 755 ms 36216 KB Output is correct
53 Correct 568 ms 35960 KB Output is correct
54 Correct 526 ms 35916 KB Output is correct
55 Correct 706 ms 36088 KB Output is correct
56 Correct 676 ms 36084 KB Output is correct
57 Correct 628 ms 35960 KB Output is correct
58 Correct 618 ms 36088 KB Output is correct
59 Correct 663 ms 36052 KB Output is correct
60 Correct 650 ms 35964 KB Output is correct
61 Correct 657 ms 36044 KB Output is correct
62 Correct 670 ms 36228 KB Output is correct
63 Correct 714 ms 35936 KB Output is correct
64 Correct 635 ms 35960 KB Output is correct
65 Correct 648 ms 35964 KB Output is correct
66 Correct 622 ms 36088 KB Output is correct
67 Correct 614 ms 35960 KB Output is correct
68 Correct 653 ms 36032 KB Output is correct
69 Correct 688 ms 35984 KB Output is correct
70 Correct 673 ms 36024 KB Output is correct
71 Correct 689 ms 36020 KB Output is correct
72 Correct 634 ms 35960 KB Output is correct
73 Correct 665 ms 35952 KB Output is correct
74 Correct 612 ms 36136 KB Output is correct
75 Correct 628 ms 36000 KB Output is correct
76 Correct 647 ms 35960 KB Output is correct
77 Correct 683 ms 36072 KB Output is correct
78 Correct 626 ms 36088 KB Output is correct
79 Correct 616 ms 36088 KB Output is correct
80 Correct 617 ms 35960 KB Output is correct
81 Correct 656 ms 36088 KB Output is correct
82 Correct 642 ms 36088 KB Output is correct
83 Correct 614 ms 35960 KB Output is correct
84 Correct 622 ms 35960 KB Output is correct
85 Correct 632 ms 36052 KB Output is correct
86 Correct 681 ms 36100 KB Output is correct
87 Correct 45 ms 4472 KB Output is correct
88 Correct 37 ms 4344 KB Output is correct
89 Correct 676 ms 36056 KB Output is correct
90 Correct 697 ms 36088 KB Output is correct
91 Correct 37 ms 4344 KB Output is correct
92 Correct 36 ms 4472 KB Output is correct
93 Correct 676 ms 35952 KB Output is correct
94 Correct 677 ms 35956 KB Output is correct
95 Correct 36 ms 4560 KB Output is correct
96 Correct 36 ms 4472 KB Output is correct
97 Correct 626 ms 35980 KB Output is correct
98 Correct 694 ms 35960 KB Output is correct