#include<bits/stdc++.h>
using namespace std;
const int maxn = 500 + 5;
const int inf = 1e6;
int n,k;
int way[maxn][maxn];
vector<int> yaw[maxn];
int dp[maxn][maxn][2];
int dp2[maxn][maxn][2];
int mx[maxn];
vector<pair<pair<int,int>,int>> a[maxn];
int add(int x, int y) {
return ((x+y)%n + n)%n;
}
int f(int x, int y, int t) {
return dp[x][y][t];
}
int g(int x, int y, int t) {
return dp2[x][y][t];
}
void init() {
for(int u=0;u<n;u++) {
for(int v=0;v<n;v++) {
a[add(v,-u)].push_back({{u,v},0});
a[add(u,-v)].push_back({{u,v},1});
}
}
for(int sz=0;sz<n;sz++) {
for(auto tt : a[sz]) {
int u = tt.first.first, v = tt.first.second, t = tt.second;
dp2[u][v][t] = -inf;
if(way[u][v]) dp2[u][v][t] = 1;
int wow = (t==0 ? 1:-1);
for(int x=add(u,wow);x!=v;x=add(x,wow)) {
if(!way[u][x]) continue;
dp[u][v][t] = max(dp[u][v][t], max(f(x,u,!t), f(x,v,t)) + 1);
dp2[u][v][t] = max(dp2[u][v][t], g(x,v,t) + 1);
}
}
}
}
int main() {
scanf("%d%d",&n,&k);
for(int u=0;u<n;u++) {
int v;
while(scanf("%d",&v) && v) {
--v;
way[u][v] = 1;
yaw[v].push_back(u);
}
}
init();
pair<int,int> res = {-1,-1};
for(int u=0;u<n;u++) res = max(res, {f(u,u,0), u});
if(k) {
for(int u=0;u<n;u++) {
//case 1
for(int v=0;v<n;v++) mx[v] = 0;
for(int v=add(u,1);v!=u;v=add(v,1)) {
if(way[u][v]) {
for(int x=add(v,1);x!=u;x=add(x,1)) {
// printf("case 1 : u = %d v = %d x = %d => 1 + %d + %d\n",u,v,x,mx[x],g(v,x,0));
res = max(res, {1 + mx[x] + g(v,x,0), u});
}
}
for(auto x : yaw[v]) mx[x] = max(mx[x], f(v,u,1) + 1);
}
//case 2
for(int v=0;v<n;v++) mx[v] = 0;
for(int v=add(u,-1);v!=u;v=add(v,-1)) {
if(way[u][v]) {
for(int x=add(v,-1);x!=u;x=add(x,-1)) {
// printf("case 2 : u = %d v = %d x = %d => 1 + %d + %d\n",u,v,x,mx[x],g(v,x,1));
res = max(res, {1 + mx[x] + g(v,x,1), u});
}
}
for(auto x : yaw[v]) mx[x] = max(mx[x], f(v,u,0) + 1);
}
}
for(int v=0;v<n;v++) {
//case 3
for(int u=0;u<n;u++) mx[u] = 0;
for(int u=add(v,-1);u!=v;u=add(u,-1)) {
if(way[u][v]) {
for(int x=add(v,1);x!=u;x=add(x,1)) {
// printf("case 2 : u = %d v = %d x = %d => 1 + %d + %d\n",u,v,x,mx[x],g(v,x,0));
res = max(res, {1 + mx[x] + g(v,x,0), u});
}
}
for(auto x : yaw[u]) mx[x] = max(mx[x], f(u,v,0) + 1);
}
//case 4
for(int u=0;u<n;u++) mx[u] = 0;
for(int u=add(v,1);u!=v;u=add(u,1)) {
if(way[u][v]) {
for(int x=add(v,-1);x!=u;x=add(x,-1)) {
// printf("case 2 : u = %d v = %d x = %d => 1 + %d + %d\n",u,v,x,mx[x],g(v,x,1));
res = max(res, {1 + mx[x] + g(v,x,1), u});
}
}
for(auto x : yaw[u]) mx[x] = max(mx[x], f(u,v,1) + 1);
}
}
}
printf("%d\n%d",res.first,res.second+1);
}
Compilation message
race.cpp: In function 'int main()':
race.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&k);
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
380 KB |
Output isn't correct |
2 |
Correct |
3 ms |
616 KB |
Output is correct |
3 |
Correct |
3 ms |
820 KB |
Output is correct |
4 |
Correct |
6 ms |
1012 KB |
Output is correct |
5 |
Incorrect |
8 ms |
1016 KB |
Output isn't correct |
6 |
Correct |
14 ms |
1176 KB |
Output is correct |
7 |
Incorrect |
15 ms |
1352 KB |
Output isn't correct |
8 |
Correct |
22 ms |
1660 KB |
Output is correct |
9 |
Incorrect |
107 ms |
1704 KB |
Output isn't correct |
10 |
Incorrect |
32 ms |
1888 KB |
Output isn't correct |
11 |
Incorrect |
35 ms |
1960 KB |
Output isn't correct |
12 |
Correct |
296 ms |
3924 KB |
Output is correct |
13 |
Correct |
829 ms |
7056 KB |
Output is correct |
14 |
Incorrect |
1722 ms |
9444 KB |
Output isn't correct |
15 |
Execution timed out |
3071 ms |
11784 KB |
Time limit exceeded |
16 |
Execution timed out |
3050 ms |
11876 KB |
Time limit exceeded |
17 |
Execution timed out |
3048 ms |
11876 KB |
Time limit exceeded |
18 |
Execution timed out |
3039 ms |
11876 KB |
Time limit exceeded |
19 |
Execution timed out |
3039 ms |
11884 KB |
Time limit exceeded |
20 |
Execution timed out |
3034 ms |
11884 KB |
Time limit exceeded |