#include <cstdio> #include <cstdlib> #include <iostream> #include <fstream> #include <sstream> #include <set> #include <map> #include <vector> #include <list> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <queue> #include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic #include <cassert> #include <iomanip> //do setprecision #include <ctime> #include <complex> using namespace std; #define FOR(i,b,e) for(int i=(b);i<(e);++i) #define FORQ(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ALL(u) (u).begin(),(u).end() #define ST first #define ND second #define PB push_back #define MP make_pair #define LL long long #define ULL unsigned LL #define LD long double typedef pair<int, int> PII; const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; const int MR = 310; bool done0, done1[MR], done2[MR][MR], done3[MR][MR][MR], ban[MR]; vector<int> g[MR]; PII dp[MR]; vector<int> getChain(int n) { FORD(i, n, 0) { if (ban[i]) continue; dp[i] = MP(1, -1); for (int j : g[i]) if (!ban[j]) dp[i] = max(dp[i], MP(1 + dp[j].first, j)); } int best = -1; REP(i, n) if (!ban[i] && (best == -1 || dp[i].first > dp[best].first)) best = i; vector<int> res; while (best != -1) { res.push_back(best); best = dp[best].second; } return res; } int res; void go(int lvl, int k, int n, vector<int>& banned) { if (!lvl) { if (done0) return; done0 = 1; } else if (lvl == 1) { if (done1[banned.front()]) return; done1[banned.front()] = 1; } else if (lvl == 2) { auto tmp = banned; sort(tmp.begin(), tmp.end()); if (done2[tmp.front()][tmp.back()]) return; done2[tmp.front()][tmp.back()] = 1; } else if (lvl == 3) { auto tmp = banned; sort(tmp.begin(), tmp.end()); if (done3[tmp.front()][tmp[1]][tmp.back()]) return; done3[tmp.front()][tmp[1]][tmp.back()] = 1; } auto chain = getChain(n); res = min(res, (int)chain.size()); if (lvl < k) for (int c : chain) { ban[c] = 1; banned.push_back(c); go(lvl + 1, k, n, banned); banned.pop_back(); ban[c] = 0; } } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); REP(i, m) { int x, y; scanf("%d%d", &x, &y); x--; y--; g[x].push_back(y); } res = n; vector<int> banned; go(0, k, n, banned); 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #include <cstdio> #include <cstdlib> #include <iostream> #include <fstream> #include <sstream> #include <set> #include <map> #include <vector> #include <list> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <queue> #include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic #include <cassert> #include <iomanip> //do setprecision #include <ctime> #include <complex> using namespace std; #define FOR(i,b,e) for(int i=(b);i<(e);++i) #define FORQ(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ALL(u) (u).begin(),(u).end() #define ST first #define ND second #define PB push_back #define MP make_pair #define LL long long #define ULL unsigned LL #define LD long double typedef pair<int, int> PII; const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; const int MR = 310; bool done0, done1[MR], done2[MR][MR], done3[MR][MR][MR], ban[MR]; vector<int> g[MR]; PII dp[MR]; vector<int> getChain(int n) { FORD(i, n, 0) { if (ban[i]) continue; dp[i] = MP(1, -1); for (int j : g[i]) if (!ban[j]) dp[i] = max(dp[i], MP(1 + dp[j].first, j)); } int best = -1; REP(i, n) if (!ban[i] && (best == -1 || dp[i].first > dp[best].first)) best = i; vector<int> res; while (best != -1) { res.push_back(best); best = dp[best].second; } return res; } int res; void go(int lvl, int k, int n, vector<int>& banned) { if (!lvl) { if (done0) return; done0 = 1; } else if (lvl == 1) { if (done1[banned.front()]) return; done1[banned.front()] = 1; } else if (lvl == 2) { auto tmp = banned; sort(tmp.begin(), tmp.end()); if (done2[tmp.front()][tmp.back()]) return; done2[tmp.front()][tmp.back()] = 1; } else if (lvl == 3) { auto tmp = banned; sort(tmp.begin(), tmp.end()); if (done3[tmp.front()][tmp[1]][tmp.back()]) return; done3[tmp.front()][tmp[1]][tmp.back()] = 1; } auto chain = getChain(n); res = min(res, (int)chain.size()); if (lvl < k) for (int c : chain) { ban[c] = 1; banned.push_back(c); go(lvl + 1, k, n, banned); banned.pop_back(); ban[c] = 0; } } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); REP(i, m) { int x, y; scanf("%d%d", &x, &y); x--; y--; g[x].push_back(y); } res = n; vector<int> banned; go(0, k, n, banned); printf("%d\n", res); return 0; } |