#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
int dp[500][500][2], dist[500][500][2], best_range[500][500][2];
bool adj[500][500];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, t;
cin >> n >> t;
FOR(i, 0, n) FOR(j, 0, n) FOR(k, 0, 2) dist[i][j][k] = -n;
FOR(i, 0, n) {
int v;
cin >> v;
while (v) {
adj[i][v - 1] = true;
dist[i][v - 1][0] = dist[v - 1][i][1] = 1;
cin >> v;
}
}
FOR(k, 1, n + 1) FOR(i, 0, n) {
int j = (i + k) % n;
FOR(v, 0, n) {
if ((v > i && v < i + k) || (v > i - n && v < i + k - n)) {
if (adj[i][v]) dp[i][j][0] = max(dp[i][j][0], max(dp[v][j][0], dp[i][v][1]) + 1);
if (adj[j][v]) dp[i][j][1] = max(dp[i][j][1], max(dp[v][j][0], dp[i][v][1]) + 1);
dist[i][j][0] = max(dist[i][j][0], dist[i][v][0] + dist[v][j][0]);
dist[i][j][1] = max(dist[i][j][1], dist[i][v][1] + dist[v][j][1]);
}
}
best_range[i][j][0] = max(best_range[(i + 1) % n][j][0], dp[i][j][0]);
best_range[i][j][1] = max(best_range[i][(j - 1 + n) % n][1], dp[i][j][1]);
}
pair<int, int> ans = {0, -1};
FOR(i, 0, n) ans = max(ans, {max(dp[i][i][0], dp[i][i][1]), i});
if (t) {
FOR(k, 1, n + 1) FOR(i, 0, n) {
int j = (i + k) % n;
FOR(v, i + k + 1, i + n) if (adj[v % n][i]) {
FOR(x, v + 1, i + n) if (adj[j][x % n]) {
ans = max(ans, {dist[i][j][0] + max(best_range[v % n][x % n][1], best_range[x % n][i][0]) + 2, v % n});
}
break;
}
for (int v = i + n - 1; v > i + k; v--) if (adj[v % n][j]) {
for (int x = v - 1; x > i + k; x--) if (adj[i][x % n]) {
ans = max(ans, {dist[i][j][1] + max(best_range[x % n][v % n][0], best_range[j][x % n][1]) + 2, v % n});
}
break;
}
}
}
cout << ans.first << '\n' << ans.second + 1 << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
6 ms |
896 KB |
Output is correct |
5 |
Correct |
6 ms |
896 KB |
Output is correct |
6 |
Correct |
8 ms |
1024 KB |
Output is correct |
7 |
Correct |
8 ms |
1280 KB |
Output is correct |
8 |
Correct |
11 ms |
1280 KB |
Output is correct |
9 |
Correct |
13 ms |
1408 KB |
Output is correct |
10 |
Correct |
10 ms |
1664 KB |
Output is correct |
11 |
Correct |
14 ms |
1536 KB |
Output is correct |
12 |
Correct |
102 ms |
2876 KB |
Output is correct |
13 |
Correct |
286 ms |
4216 KB |
Output is correct |
14 |
Correct |
251 ms |
5368 KB |
Output is correct |
15 |
Correct |
1378 ms |
6604 KB |
Output is correct |
16 |
Correct |
1461 ms |
6648 KB |
Output is correct |
17 |
Correct |
1391 ms |
6528 KB |
Output is correct |
18 |
Correct |
447 ms |
6648 KB |
Output is correct |
19 |
Correct |
1552 ms |
6780 KB |
Output is correct |
20 |
Correct |
1549 ms |
6776 KB |
Output is correct |