# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
66393 | ikura355 | Sailing Race (CEOI12_race) | C++14 | 3042 ms | 6764 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
int add(int x, int y) {
return ((x+y)%n + n)%n;
}
int f(int u, int v, int t) {
if(dp[u][v][t]==-1) {
dp[u][v][t] = 0;
int wow = (t==0 ? 1:-1);
for(int x=add(u,wow);x!=v;x=add(x,wow)) {
if(!way[u][x]) continue;
//if(u==4 && v==1) printf("%d %d %d -> %d\n",u,v,t,x);
dp[u][v][t] = max(dp[u][v][t], max(f(x,u,!t), f(x,v,t)) + 1);
}
}
return dp[u][v][t];
}
int g(int u, int v, int t) {
if(dp2[u][v][t]==-1) {
dp2[u][v][t] = -inf;
int wow = (t==0 ? 1:-1);
if(way[u][v]) dp2[u][v][t] = 1;
for(int x=add(u,wow);x!=v;x=add(x,wow)) {
if(!way[u][x]) continue;
// if(u==4 && v==2) printf("%d %d %d -> %d\n",u,v,t,x);
dp2[u][v][t] = max(dp2[u][v][t], g(x,v,t) + 1);
}
}
return dp2[u][v][t];
}
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);
}
}
memset(dp,-1,sizeof(dp));
memset(dp2,-1,sizeof(dp2));
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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |