#include <bits/stdc++.h>
using namespace std;
const int N = 305;
vector <int> G[N];
int odl[N];
int n, m, k;
void zer(){
for(int i = 0; i < N; i++){
odl[i] = -2;
}
}
void DFS(int v, int o){
int ma = -1;
for(int i = 0; i < G[v].size(); i++){
if(G[v][i] != o){
if(odl[G[v][i]] == -1) continue;
if(odl[G[v][i]] == -2)DFS(G[v][i], v);
ma = max(odl[G[v][i]], ma);
}
}
odl[v] = ma + 1;
}
int glob = 1e9;
void start(){
for(int i = 1; i < n; i++){
if(odl[i] == -2){
DFS(i, i);
}
}
int curr = 0;
for(int i = 1; i < n; i++){
curr = max(curr, odl[i]);
}
glob = min(glob, curr);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m >> k;
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
G[a].push_back(b);
}
for(int i = 1; i <= n; i++){
zer();
odl[i] = -1;
if(k == 1){
start();
//cout << "USUWAM " << i << " " << glob << "\n";
} else
for(int j = i + 1; j <= n; j++){
zer();
odl[i] = -1;
odl[j] = -1;
if(k == 2){
start();
} else
for(int l = j + 1; l <= n; l++){
zer();
odl[i] = -1;
odl[j] = -1;
odl[l] = -1;
if(k == 3){
start();
} else
for(int x = l + 1; x <= n; x++){
zer();
odl[i] = -1;
odl[j] = -1;
odl[l] = -1;
odl[x] = -1;
start();
}
}
}
}
if(glob == 0)cout << "0\n";
else cout << glob + 1<< "\n";
}
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 | #include <bits/stdc++.h> using namespace std; const int N = 305; vector <int> G[N]; int odl[N]; int n, m, k; void zer(){ for(int i = 0; i < N; i++){ odl[i] = -2; } } void DFS(int v, int o){ int ma = -1; for(int i = 0; i < G[v].size(); i++){ if(G[v][i] != o){ if(odl[G[v][i]] == -1) continue; if(odl[G[v][i]] == -2)DFS(G[v][i], v); ma = max(odl[G[v][i]], ma); } } odl[v] = ma + 1; } int glob = 1e9; void start(){ for(int i = 1; i < n; i++){ if(odl[i] == -2){ DFS(i, i); } } int curr = 0; for(int i = 1; i < n; i++){ curr = max(curr, odl[i]); } glob = min(glob, curr); } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> m >> k; for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; G[a].push_back(b); } for(int i = 1; i <= n; i++){ zer(); odl[i] = -1; if(k == 1){ start(); //cout << "USUWAM " << i << " " << glob << "\n"; } else for(int j = i + 1; j <= n; j++){ zer(); odl[i] = -1; odl[j] = -1; if(k == 2){ start(); } else for(int l = j + 1; l <= n; l++){ zer(); odl[i] = -1; odl[j] = -1; odl[l] = -1; if(k == 3){ start(); } else for(int x = l + 1; x <= n; x++){ zer(); odl[i] = -1; odl[j] = -1; odl[l] = -1; odl[x] = -1; start(); } } } } if(glob == 0)cout << "0\n"; else cout << glob + 1<< "\n"; } |
English