#include <bits/stdc++.h>
using namespace std;
#define REP(i, x) for( int i = 0; i < x; i++ )
#define FOR(i, x) for( int i = 1; i <=x; i++ )
#define FORALL(it, x) for(__typeof(x.begin()) it = x.begin(); it != x.end(); it++)
#define PB push_back
typedef vector<int> VI;
typedef vector<VI> VVI;
const int MAX_K = 5;
const int MAX_N = 307;
int wynik = MAX_N;
int longest_path[MAX_K][MAX_N];
int father[MAX_K][MAX_N];
bool blocked[MAX_N];
VVI graph(MAX_N);
int n, k;
void wczytaj(){
int m;
cin >> n >> m >> k;
REP(i, m){
int x, y;
cin >> x >> y;
graph[y].PB(x);
}
}
void rozw(int k){
int ktory = 0;
FOR(i, n) if( !blocked[i] ){
longest_path[k][i] = 1;
father[k][i] = 0;
FORALL(it, graph[i]) if( !blocked[*it] )
if( longest_path[k][*it] + 1 > longest_path[k][i] ){
longest_path[k][i] = longest_path[k][*it] + 1;
father[k][i] = *it;
if( longest_path[k][i] > longest_path[k][ktory] )
ktory = i;
}
if( longest_path[k][i] > longest_path[k][ktory] )
ktory = i;
}
if( k == 0 ) wynik = min(wynik, longest_path[k][ktory]);
else {
int dl = longest_path[k][ktory];
int pod = 0;
while(ktory){
if( pod < wynik ){
blocked[ktory] = 1;
rozw(k-1);
blocked[ktory] = 0;
}
ktory = father[k][ktory];
pod++;
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
wczytaj();
rozw(k);
cout << wynik << endl;
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 | #include <bits/stdc++.h> using namespace std; #define REP(i, x) for( int i = 0; i < x; i++ ) #define FOR(i, x) for( int i = 1; i <=x; i++ ) #define FORALL(it, x) for(__typeof(x.begin()) it = x.begin(); it != x.end(); it++) #define PB push_back typedef vector<int> VI; typedef vector<VI> VVI; const int MAX_K = 5; const int MAX_N = 307; int wynik = MAX_N; int longest_path[MAX_K][MAX_N]; int father[MAX_K][MAX_N]; bool blocked[MAX_N]; VVI graph(MAX_N); int n, k; void wczytaj(){ int m; cin >> n >> m >> k; REP(i, m){ int x, y; cin >> x >> y; graph[y].PB(x); } } void rozw(int k){ int ktory = 0; FOR(i, n) if( !blocked[i] ){ longest_path[k][i] = 1; father[k][i] = 0; FORALL(it, graph[i]) if( !blocked[*it] ) if( longest_path[k][*it] + 1 > longest_path[k][i] ){ longest_path[k][i] = longest_path[k][*it] + 1; father[k][i] = *it; if( longest_path[k][i] > longest_path[k][ktory] ) ktory = i; } if( longest_path[k][i] > longest_path[k][ktory] ) ktory = i; } if( k == 0 ) wynik = min(wynik, longest_path[k][ktory]); else { int dl = longest_path[k][ktory]; int pod = 0; while(ktory){ if( pod < wynik ){ blocked[ktory] = 1; rozw(k-1); blocked[ktory] = 0; } ktory = father[k][ktory]; pod++; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); wczytaj(); rozw(k); cout << wynik << endl; return 0; } |
English