/*
dp[node 1][node 2][0/1] = max edges in the range starting at node 1/2 (based on the third dimension)
http://ceoi2012.elte.hu/download/modelsolutions/3_solrace.pdf
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, a, b) for(int i=a; i<b; i++)
#define ffi For(i, 0, N)
#define ffj For(j, 0, N)
#define ffa ffi ffj
#define s <<" "<<
#define c <<" : "<<
#define w cout
#define e endl//"\n"
#define pb push_back
#define mp make_pair
#define a first
#define b second
//#define int ll
const int MAXN=500, INF=1000000000;
///500,000,000
int N, K, dp[MAXN][MAXN][2], out, loc, dirb[MAXN][MAXN], dirs[MAXN][MAXN];
vector<int> adj[MAXN];
priority_queue<pair<int, int> > n1; /// ((-/+)relative loc, loc)
bool vis[MAXN];
bool in (int x, int y, int z) {
if (y < z) return y < x && x < z;
return x > y || x < z;
}
int solve(int n1, int n2, int at) {
if (dp[n1][n2][at] != -1) return dp[n1][n2][at];
dp[n1][n2][at] = 0;
//w<< n1 s n2 s at<<e;
/// range is n1+1 to n2-1 (n1+1)%N to (n2-1+N)%N
if ((n1+1)%N == n2) {dp[n1][n2][at] = 0; return dp[n1][n2][at];}
if (at == 0) {
/// at n1
for (int i: adj[n1]) if (in(i, n1, n2)) {
dp[n1][n2][0] = max(dp[n1][n2][0], max(solve(n1, i, 1), solve(i, n2, 0))+1);
}
return dp[n1][n2][0];
}
/// at n2
for (int i: adj[n2]) if (in(i, n1, n2)) {
dp[n1][n2][1] = max(dp[n1][n2][1], max(solve(i, n2, 0), solve(n1, i, 1))+1);
}
return dp[n1][n2][1];
}
main() {
//ifstream cin("test.in");
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> K;
ffi {int a; cin >> a; while (a != 0) {adj[i].pb(a-1); cin >> a;}}
ffa dp[i][j][0] = dp[i][j][1] = -1;
ffa {solve(i, j, 0); solve(i, j, 1);}
if (K == 0) ffi{
if (dp[i][i][0] > out) {out = dp[i][i][0]; loc = i+1;}
}
else {
ffa dirb[i][j] = dirs[i][j] = -INF;
/// set dirb
ffi {
ffj vis[j] = false;
dirb[i][i] = 0;
vis[i] = true;
n1.push(mp(0, i));
while (!n1.empty()) {
int x = n1.top().b; n1.pop();
for (int y: adj[x]) if (in(y, x, i)) {
/// moving forward
dirb[i][y] = max(dirb[i][y], dirb[i][x]+1);
if (!vis[y]) {
n1.push(mp( -((y-i+N)%N) , y));
vis[y] = true;
}
}
}
}
//ffa w<< i+1 s j+1 c dirb[i][j]<<e;
/// set dirs
ffi {
ffj vis[j] = false;
dirs[i][i] = 0;
vis[i] = true;
n1.push(mp(0, i));
while (!n1.empty()) {
int x = n1.top().b; n1.pop();
for (int y: adj[x]) if (in(y, i, x)) {
/// moving forward
dirs[i][y] = max(dirs[i][y], dirs[i][x]+1);
if (!vis[y]) {
n1.push(mp( (y-i+N)%N , y));
vis[y] = true;
}
}
}
}
//ffa w<< i+1 s j+1 c dirs[i][j]<<e;
For (a, 0, N) for (int b: adj[a]) For (cc, 0, N) for (int d: adj[cc]) {
if (a == b || a == cc || a == d || b == cc || b == d || cc == d) continue;
if (in(cc, b, a) == in(d, b, a)) continue;
if (in(cc, b, a)) {
if (2+dirb[b][cc]+max(dp[d][b][0], dp[a][d][1]) > out) {
out = 2+dirb[b][cc]+max(dp[d][b][0], dp[a][d][1]);
loc = a+1;
}
}
else {
if (2+dirs[b][cc]+max(dp[d][a][0], dp[b][d][1]) > out) {
out = max(out, 2+dirs[b][cc]+max(dp[d][a][0], dp[b][d][1]));
loc = a+1;
}
}
}
}
//ffa w<< i+1 s j+1 c solve(i, j, 0) s solve(i, j, 1)<<e;
w<< out <<e<< loc<<e;
}
Compilation message
race.cpp:52:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
816 KB |
Output is correct |
4 |
Correct |
3 ms |
952 KB |
Output is correct |
5 |
Correct |
3 ms |
952 KB |
Output is correct |
6 |
Correct |
7 ms |
952 KB |
Output is correct |
7 |
Correct |
5 ms |
952 KB |
Output is correct |
8 |
Correct |
9 ms |
1164 KB |
Output is correct |
9 |
Correct |
6 ms |
1164 KB |
Output is correct |
10 |
Correct |
14 ms |
1228 KB |
Output is correct |
11 |
Correct |
8 ms |
1228 KB |
Output is correct |
12 |
Correct |
194 ms |
2232 KB |
Output is correct |
13 |
Correct |
320 ms |
3120 KB |
Output is correct |
14 |
Correct |
71 ms |
3120 KB |
Output is correct |
15 |
Execution timed out |
3040 ms |
4980 KB |
Time limit exceeded |
16 |
Execution timed out |
3056 ms |
5148 KB |
Time limit exceeded |
17 |
Execution timed out |
3053 ms |
5148 KB |
Time limit exceeded |
18 |
Correct |
97 ms |
5148 KB |
Output is correct |
19 |
Execution timed out |
3014 ms |
5532 KB |
Time limit exceeded |
20 |
Execution timed out |
3022 ms |
5828 KB |
Time limit exceeded |