제출 #1325833

#제출 시각아이디문제언어결과실행 시간메모리
1325833tredsused70Sailing Race (CEOI12_race)C++20
10 / 100
3095 ms2616 KiB
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC optimize("trapv")
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define mpr make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int nmax = 501, mod = 1000000007, inf = 2000010000, key = 200003, lg = 20, block = 300;
const ll infll = 4000000000000000000;
const ld eps = 1e-9;

int dp_left[nmax][nmax], dp_right[nmax][nmax];
bool connected[nmax][nmax];
int n_;

int get_ans(int cur, int left, int right) {
    int ans_left = 0;
    if(dp_left[cur][left] == -1) {
        for(int i = cur; i != left; i = (i - 1 + n_) % n_) {
            if(connected[cur][i]) {
                ans_left = max(ans_left, get_ans(i, left, cur) + 1);
            }
        }
        dp_left[cur][left] = ans_left;
    }
    else {
        ans_left = dp_left[cur][left];
    }
    int ans_right = 0;
    if(dp_right[cur][right] == -1) {
        for(int i = cur; i != right; i = (i + 1) % n_) {
            if(connected[cur][i]) {
                ans_right = max(ans_right, get_ans(i, cur, right) + 1);
            }
        }
    }
    else {
        ans_right = dp_right[cur][right];
    }
    return max(ans_left, ans_right);
}

void solve()
{
    int n, k;
    cin >> n >> k;
    assert(k == 0);
    n_ = n;
    for(int i = 0; i < n; i++) {
        fill(dp_left[i], dp_left[i] + n, -1);
        fill(dp_right[i], dp_right[i] + n, -1);
        int cur = 1;
        while(1) {
            cin >> cur;
            if(cur == 0)
                break;
            cur--;
            connected[i][cur] = 1;
        }
    }
    int mx = 0, ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(connected[i][j] == 1) {
                int t = get_ans(j, i, i) + 1;
                if(t > ans) {
                    ans = t;
                    mx = i;
                }
            }
        }
    }
    cout << ans << "\n" << mx + 1 << "\n";
    return ;
}

int main()
{
    //freopen("pieaters.in","r",stdin);
    //freopen("pieaters.out","w",stdout);
    ios_base::sync_with_stdio(0);cin.tie(0);
    srand(8713);
    //init();
    int t = 1;
    //cin >> t;
    //int t_ = t;
    //t = rdi();
    while(t--) {
        //cout << "Case #" << t_ - t << ": ";
        solve();
    }
    //flush();
    return 0;
}

/*
7 1
5 0
5 0
7 0
3 0
4 0
4 3 0
2 1 0
*/
#Verdict Execution timeMemoryGrader output
Fetching results...