/*
* Author: Nonoze
* Created: Friday 10/05/2024
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
#define int long long
#ifdef _IN_LOCAL
#include <bits/DebugTemplate.h>
#else
#define dbg(...)
#endif
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template<typename T> void read(T& x, bool write=0) { if (write) cout << x << ' '; else cin >> x; }
template<typename T1, typename T2> void read(pair<T1, T2>& p, bool write=0) { read(p.first, write), read(p.second, write); if (write) cout << endl;}
template<typename T> void read(vector<T>& v, bool write=0) { for (auto& x : v) read(x, write); if (write) cout << endl; }
template<typename T1, typename T2> void read(T1& x, T2& y, bool write=0) { read(x, write), read(y, write); if (write) cout << endl; }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z, bool write=0) { read(x, write), read(y, write), read(z, write); if (write) cout << endl; }
template<typename T> void print(T x) { read(x, 1); cout << endl; }
template<typename T1, typename T2> void print(T1 x, T2 y) { read(x, y, 1); }
template<typename T1, typename T2, typename T3> void print(T1 x, T2 y, T3 z) { read(x, y, z, 1); }
#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) {cout << x << endl; return;}
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=25;
void solve();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
// cin >> tt;
while(tt--) solve();
return 0;
}
struct subtask1 {
int n, k;
vector<vector<int>> adj;
vector<vector<vector<int>>> memo;
bool valable(int node, int left, int right) {
if (node>left && node<right) return 1;
if (right<left && (node>left || node<right)) return 1;
return 0;
}
int dp(int l, int r, bool left) {
if (memo[l][r][left]!=-1) return memo[l][r][left];
int u=(left?l:r);
int ans=0;
for (auto v: adj[u]) {
if (valable(v, l, r)) {
cmax(ans, dp(v, r, 1));
cmax(ans, dp(l, v, 0));
}
}
return memo[l][r][left]=ans+1;
}
subtask1(int _n, int _k, vector<vector<int>> _adj) {
n=_n, k=_k, adj=_adj;
memo.resize(n, vector<vector<int>>(n, vector<int>(2, -1)));
int ans=0, empl=0;
for (int i=0; i<n; i++) {
int act=0;
for (auto v: adj[i]) {
cmax(act, dp(i, v, 0));
cmax(act, dp(v, i, 1));
}
if (act>ans) {
ans=act;
empl=i;
}
}
print(ans);
print(empl+1);
}
};
struct subtask2 {
int n, k;
vector<vector<int>> adj;
vector<vector<vector<vector<vector<vector<int>>>>>> memo;
bool valable(int node, int left, int right) {
if (node>left && node<right) return 1;
if (right<left && (node>left || node<right)) return 1;
return 0;
}
int dp(int l, int r, bool left, int beg, bool leftbeg, bool can) {
if (memo[l][r][left][beg][leftbeg][can]!=-1) return memo[l][r][left][beg][leftbeg][can];
int lbeg=-1, rbeg=-1;
if (can) {
lbeg=beg, rbeg=beg;
if (leftbeg) rbeg=r;
else lbeg=l;
swap(lbeg, rbeg);
}
int u=(left?l:r);
int ans=0;
for (auto v: adj[u]) {
if (valable(v, l, r)) {
if (!leftbeg) cmax(ans, dp(v, r, 1, 0, 0, 0));
else cmax(ans, dp(v, r, 1, beg, leftbeg, can));
if (leftbeg) cmax(ans, dp(l, v, 0, 0, 0, 0));
else cmax(ans, dp(l, v, 0, beg, leftbeg, can));
}
if (can && valable(v, lbeg, rbeg)) {
cmax(ans, dp(v, rbeg, 1, 0, 0, 0));
cmax(ans, dp(lbeg, v, 0, 0, 0, 0));
}
}
return memo[l][r][left][beg][leftbeg][can]=ans+1;
}
subtask2(int _n, int _k, vector<vector<int>> _adj) {
n=_n, k=_k, adj=_adj;
memo.resize(n, vector<vector<vector<vector<vector<int>>>>>(n, vector<vector<vector<vector<int>>>>(2, vector<vector<vector<int>>>(n, vector<vector<int>>(2, vector<int>(2, -1))))));
int ans=0, empl=0;
for (int i=0; i<n; i++) {
int act=0;
for (auto v: adj[i]) {
cmax(act, dp(i, v, 0, v, 0, 1));
cmax(act, dp(v, i, 1, v, 1, 1));
}
if (act>ans) {
ans=act;
empl=i;
}
}
print(ans);
print(empl+1);
}
};
int n, k, m, q;
vector<vector<int>> adj;
void solve() {
read(n, k);
adj.resize(n);
for (int i=0; i<n; i++) {
int x; read(x);
while (x!=0) {
x--;
adj[i].pb(x);
read(x);
}
}
if (k==0) subtask1(n, k, adj);
else subtask2(n, k, adj);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
2908 KB |
Output is correct |
3 |
Correct |
8 ms |
8796 KB |
Output is correct |
4 |
Correct |
21 ms |
20056 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Runtime error |
51 ms |
65536 KB |
Execution killed with signal 9 |
7 |
Correct |
3 ms |
720 KB |
Output is correct |
8 |
Runtime error |
52 ms |
65536 KB |
Execution killed with signal 9 |
9 |
Correct |
4 ms |
1016 KB |
Output is correct |
10 |
Correct |
8 ms |
1116 KB |
Output is correct |
11 |
Correct |
5 ms |
1112 KB |
Output is correct |
12 |
Runtime error |
53 ms |
65536 KB |
Execution killed with signal 9 |
13 |
Runtime error |
51 ms |
65536 KB |
Execution killed with signal 9 |
14 |
Correct |
62 ms |
9560 KB |
Output is correct |
15 |
Runtime error |
53 ms |
65536 KB |
Execution killed with signal 9 |
16 |
Runtime error |
68 ms |
65536 KB |
Execution killed with signal 9 |
17 |
Runtime error |
50 ms |
65536 KB |
Execution killed with signal 9 |
18 |
Correct |
90 ms |
14540 KB |
Output is correct |
19 |
Runtime error |
51 ms |
65536 KB |
Execution killed with signal 9 |
20 |
Runtime error |
52 ms |
65536 KB |
Execution killed with signal 9 |