#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; vector < int > G[301], P[301]; bool used[301]; void dfs( vector < int > &st, vector < vector < int > > &V, int x ) { int tmp = st[x]-1; if ( st[x] == 0 ) return; V[st[x]].push_back( x ); st[x] = 0; for ( int i = 0; i < P[x].size(); ++i ) { if ( st[P[x][i]] == tmp ) dfs( st, V, P[x][i] ); } } int fun( int n, int m, int k ) { int result = 0, tmp, maks = 0; vector < int > st( n+1, 1 ); vector < vector < int > > V; for ( int i = 1; i <= n; ++i ) { if ( used[i] ) { st[i] = 0; continue; } for ( int j = 0; j < P[i].size(); ++j ) { tmp = P[i][j]; st[i] = max( st[i], st[tmp]+1 ); } maks = max( maks, st[i]); } if ( maks == n && m == n-1 ) { return ( n - k ) / ( k+1 ) + (bool)(( n - k ) % ( k+1 )); } if ( k == 0 ) return maks; result = maks; V.resize( maks+1 ); for ( int i = 1; i <= n; ++i ) { if ( st[i] == maks ) { dfs( st, V, i ); } } for ( int i = 1; i <= maks; ++i ) { if ( V[i].size() <= k ) { for ( int j = 0; j < V[i].size(); ++j ) used[V[i][j]] = true; result = min( result, fun( n, m, k - V[i].size() ) ); for ( int j = 0; j < V[i].size(); ++j ) used[V[i][j]] = false; } } return result; } int main() { int n, m, k, a, b; scanf( "%d%d%d", &n, &m, &k ); for ( int i = 0; i < m; ++i ) { scanf( "%d%d", &a, &b ); G[a].push_back( b ); P[b].push_back( a ); } if ( n <= k ) { printf( "0\n" ); return 0; } printf( "%d\n", fun( n, m, k ) ); }
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 | #include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; vector < int > G[301], P[301]; bool used[301]; void dfs( vector < int > &st, vector < vector < int > > &V, int x ) { int tmp = st[x]-1; if ( st[x] == 0 ) return; V[st[x]].push_back( x ); st[x] = 0; for ( int i = 0; i < P[x].size(); ++i ) { if ( st[P[x][i]] == tmp ) dfs( st, V, P[x][i] ); } } int fun( int n, int m, int k ) { int result = 0, tmp, maks = 0; vector < int > st( n+1, 1 ); vector < vector < int > > V; for ( int i = 1; i <= n; ++i ) { if ( used[i] ) { st[i] = 0; continue; } for ( int j = 0; j < P[i].size(); ++j ) { tmp = P[i][j]; st[i] = max( st[i], st[tmp]+1 ); } maks = max( maks, st[i]); } if ( maks == n && m == n-1 ) { return ( n - k ) / ( k+1 ) + (bool)(( n - k ) % ( k+1 )); } if ( k == 0 ) return maks; result = maks; V.resize( maks+1 ); for ( int i = 1; i <= n; ++i ) { if ( st[i] == maks ) { dfs( st, V, i ); } } for ( int i = 1; i <= maks; ++i ) { if ( V[i].size() <= k ) { for ( int j = 0; j < V[i].size(); ++j ) used[V[i][j]] = true; result = min( result, fun( n, m, k - V[i].size() ) ); for ( int j = 0; j < V[i].size(); ++j ) used[V[i][j]] = false; } } return result; } int main() { int n, m, k, a, b; scanf( "%d%d%d", &n, &m, &k ); for ( int i = 0; i < m; ++i ) { scanf( "%d%d", &a, &b ); G[a].push_back( b ); P[b].push_back( a ); } if ( n <= k ) { printf( "0\n" ); return 0; } printf( "%d\n", fun( n, m, k ) ); } |