#include <bits/stdc++.h> // #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define pb push_back #define mp make_pair #define st first #define nd second int N, M, K; vector<int> adj[500]; int DP[500]; bool removed[500]; int p[500]; int go(int K) { REP(i,N) { DP[i] = !removed[i]; p[i] = -1; } int max_chain = -1; int max_chain_end = -1; REP(u,N) if (!removed[u]) { for (auto v: adj[u]) if (DP[v] < DP[u] + 1) { DP[v] = DP[u] + 1; p[v] = u; } if (DP[u] > max_chain) { max_chain = DP[u]; max_chain_end = u; } } int result = max_chain; if (!K) return result; vector<int> candidates; while (max_chain_end != -1) { candidates.pb(max_chain_end); max_chain_end = p[max_chain_end]; } for (auto v: candidates) { removed[v] = true; result = min(result, go(K-1)); removed[v] = false; } return result; } int main(int argc, char* argv[]) { scanf("%d%d%d", &N, &M, &K); REP(i,M) { int u, v; scanf("%d%d", &u, &v); --u, --v; adj[u].pb(v); } if (K >= N) { printf("0\n"); return 0; } int result = go(K); printf("%d\n", result); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <bits/stdc++.h> // #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define pb push_back #define mp make_pair #define st first #define nd second int N, M, K; vector<int> adj[500]; int DP[500]; bool removed[500]; int p[500]; int go(int K) { REP(i,N) { DP[i] = !removed[i]; p[i] = -1; } int max_chain = -1; int max_chain_end = -1; REP(u,N) if (!removed[u]) { for (auto v: adj[u]) if (DP[v] < DP[u] + 1) { DP[v] = DP[u] + 1; p[v] = u; } if (DP[u] > max_chain) { max_chain = DP[u]; max_chain_end = u; } } int result = max_chain; if (!K) return result; vector<int> candidates; while (max_chain_end != -1) { candidates.pb(max_chain_end); max_chain_end = p[max_chain_end]; } for (auto v: candidates) { removed[v] = true; result = min(result, go(K-1)); removed[v] = false; } return result; } int main(int argc, char* argv[]) { scanf("%d%d%d", &N, &M, &K); REP(i,M) { int u, v; scanf("%d%d", &u, &v); --u, --v; adj[u].pb(v); } if (K >= N) { printf("0\n"); return 0; } int result = go(K); printf("%d\n", result); } |