#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 444; int n; int res; bool bad[N]; vector<int> g[N]; int dp[N]; void go(int k) { //printf("go %d [", k); //FOR(i,n) if (bad[i]) printf("%d ", i); //printf("]\n"); int cur = -1, who = -1; for (int i=n-1; i>=0; i--) { if (bad[i]) continue; dp[i]=1; FOR(j,SZ(g[i])) if (!bad[g[i][j]]) dp[i] = max(dp[i], dp[g[i][j]]+1); if (dp[i] > cur) { cur = dp[i]; who = i; } } if (who == -1) { res = 0; return; } if (k == 0) { res = min(res, cur); return; } //printf("%d -- %d\n", k, who); vi path(1, who); while (dp[who]>1) { //printf("%d\n", who); FOR(j,SZ(g[who])) if (!bad[g[who][j]] && dp[g[who][j]]+1 == dp[who]) { who = g[who][j]; break; } path.pb(who); } FOR(i,SZ(path)) { bad[path[i]] = 1; go(k-1); bad[path[i]] = 0; } } int main() { int m,k; scanf("%d%d%d", &n, &m, &k); FOR(i,m) { int a,b; scanf("%d%d", &a, &b); a--; b--; g[a].pb(b); } res = n; go(k); printf("%d\n", res); return 0; }
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 77 78 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 444; int n; int res; bool bad[N]; vector<int> g[N]; int dp[N]; void go(int k) { //printf("go %d [", k); //FOR(i,n) if (bad[i]) printf("%d ", i); //printf("]\n"); int cur = -1, who = -1; for (int i=n-1; i>=0; i--) { if (bad[i]) continue; dp[i]=1; FOR(j,SZ(g[i])) if (!bad[g[i][j]]) dp[i] = max(dp[i], dp[g[i][j]]+1); if (dp[i] > cur) { cur = dp[i]; who = i; } } if (who == -1) { res = 0; return; } if (k == 0) { res = min(res, cur); return; } //printf("%d -- %d\n", k, who); vi path(1, who); while (dp[who]>1) { //printf("%d\n", who); FOR(j,SZ(g[who])) if (!bad[g[who][j]] && dp[g[who][j]]+1 == dp[who]) { who = g[who][j]; break; } path.pb(who); } FOR(i,SZ(path)) { bad[path[i]] = 1; go(k-1); bad[path[i]] = 0; } } int main() { int m,k; scanf("%d%d%d", &n, &m, &k); FOR(i,m) { int a,b; scanf("%d%d", &a, &b); a--; b--; g[a].pb(b); } res = n; go(k); printf("%d\n", res); return 0; } |