#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
using namespace std;
typedef long long ll;
ll n, m, k, a, b; // k < 5
const int maxn = 330, inf = 1e9;
vector < int > t[maxn][2];
vector < int > inny[6];
vector < int > pom;
int dl[maxn][2];
int wyn = inf, res;
queue < int > q;
void miny(int &a, int b){a = min(a, b);}
bitset < maxn > bit;
void oblicz(int ktory){
for(bool o: {0, 1}){
for(int i=0; i<=n+1; i++){
dl[i][o] = 0;
}
res = 0;
}
for(int i: pom) bit[i] = 1;
for(int ter=0; ter<=n+1; ter++){
if(bit[ter]) continue;
res = max(res, dl[ter][1]);
for(int i: t[ter][1]){
dl[i][1] = max(dl[i][1], dl[ter][1]+1);
}
}
for(int ter=n+1; ter>=0; ter--){
if(bit[ter]) continue;
res = max(res, dl[ter][0]);
for(int i: t[ter][0]){
dl[i][0] = max(dl[i][0], dl[ter][0]+1);
}
}
int res = dl[0][0], ter = 0;
miny(wyn, res);
while(ter != n+1){
for(int i: t[ter][1]){
if(bit[i] == 0 && dl[ter][1] + 1 + dl[i][0] == res){
ter = i;
if(i != n+1) inny[ktory].push_back(i);
break;
}
}
}
for(int i: pom) bit[i] = 0;
}
void backtrack(int poz){
for(int i: inny[poz]){
pom.push_back(i);
oblicz(poz+1);
if(poz < k) backtrack(poz+1);
else{
inny[poz+1].clear();
}
pom.pop_back();
}
inny[poz].clear();
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
if(k == n){
cout << "0";
return 0;
}
for(int i=1; i<=n; i++){
t[0][1].push_back(i);
t[i][1].push_back(n+1);
t[i][0].push_back(0);
t[n+1][0].push_back(i);
}
for(int i=0; i<m; i++){
cin >> a >> b;
t[a][1].push_back(b);
t[b][0].push_back(a);
}
oblicz(1);
backtrack(1);
cout << wyn-1;
}
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 | #include <bits/stdc++.h> #pragma GCC optimize ("O3") using namespace std; typedef long long ll; ll n, m, k, a, b; // k < 5 const int maxn = 330, inf = 1e9; vector < int > t[maxn][2]; vector < int > inny[6]; vector < int > pom; int dl[maxn][2]; int wyn = inf, res; queue < int > q; void miny(int &a, int b){a = min(a, b);} bitset < maxn > bit; void oblicz(int ktory){ for(bool o: {0, 1}){ for(int i=0; i<=n+1; i++){ dl[i][o] = 0; } res = 0; } for(int i: pom) bit[i] = 1; for(int ter=0; ter<=n+1; ter++){ if(bit[ter]) continue; res = max(res, dl[ter][1]); for(int i: t[ter][1]){ dl[i][1] = max(dl[i][1], dl[ter][1]+1); } } for(int ter=n+1; ter>=0; ter--){ if(bit[ter]) continue; res = max(res, dl[ter][0]); for(int i: t[ter][0]){ dl[i][0] = max(dl[i][0], dl[ter][0]+1); } } int res = dl[0][0], ter = 0; miny(wyn, res); while(ter != n+1){ for(int i: t[ter][1]){ if(bit[i] == 0 && dl[ter][1] + 1 + dl[i][0] == res){ ter = i; if(i != n+1) inny[ktory].push_back(i); break; } } } for(int i: pom) bit[i] = 0; } void backtrack(int poz){ for(int i: inny[poz]){ pom.push_back(i); oblicz(poz+1); if(poz < k) backtrack(poz+1); else{ inny[poz+1].clear(); } pom.pop_back(); } inny[poz].clear(); } main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; if(k == n){ cout << "0"; return 0; } for(int i=1; i<=n; i++){ t[0][1].push_back(i); t[i][1].push_back(n+1); t[i][0].push_back(0); t[n+1][0].push_back(i); } for(int i=0; i<m; i++){ cin >> a >> b; t[a][1].push_back(b); t[b][0].push_back(a); } oblicz(1); backtrack(1); cout << wyn-1; } |
English