#include <iostream>
#include <vector>
using namespace std;
const int N = 301;
const int M = 401;
int n, m, k;
bool used[N];
int longest[N];
int q[N];
int best;
vector<int> G[N];
vector<int> updateLongest() {
int cur = 0;
for (int i = n; i > 0; --i) {
if (used[i]) {
longest[i] = -2;
continue;
}
longest[i] = 0;
q[i] = -1;
for (int j = 0; j < G[i].size(); ++j) {
if (!used[G[i][j]] && longest[G[i][j]] + 1 > longest[i]) {
longest[i] = longest[G[i][j]] + 1;
q[i] = G[i][j];
if (longest[i] > longest[cur]) cur = i;
}
}
}
vector<int> nodes;
while (cur != -1) {
nodes.push_back(cur);
cur = q[cur];
}
return nodes;
}
void qq(int left) {
vector<int> nodes = updateLongest();
if (left == 0) {
if (best > nodes.size()) best = nodes.size();
return;
}
for (int i = 0; i < nodes.size(); ++i) {
used[nodes[i]] = true;
qq(left - 1);
used[nodes[i]] = false;
}
}
int main() {
int a, b;
best = 400;
cin>>n>>m>>k;
if (k >= n) {
cout<<0<<endl;
return 0;
}
for (int i = 0; i < m; ++i) {
cin>>a>>b;
G[a].push_back(b);
}
qq(k);
cout<<best<<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 | #include <iostream> #include <vector> using namespace std; const int N = 301; const int M = 401; int n, m, k; bool used[N]; int longest[N]; int q[N]; int best; vector<int> G[N]; vector<int> updateLongest() { int cur = 0; for (int i = n; i > 0; --i) { if (used[i]) { longest[i] = -2; continue; } longest[i] = 0; q[i] = -1; for (int j = 0; j < G[i].size(); ++j) { if (!used[G[i][j]] && longest[G[i][j]] + 1 > longest[i]) { longest[i] = longest[G[i][j]] + 1; q[i] = G[i][j]; if (longest[i] > longest[cur]) cur = i; } } } vector<int> nodes; while (cur != -1) { nodes.push_back(cur); cur = q[cur]; } return nodes; } void qq(int left) { vector<int> nodes = updateLongest(); if (left == 0) { if (best > nodes.size()) best = nodes.size(); return; } for (int i = 0; i < nodes.size(); ++i) { used[nodes[i]] = true; qq(left - 1); used[nodes[i]] = false; } } int main() { int a, b; best = 400; cin>>n>>m>>k; if (k >= n) { cout<<0<<endl; return 0; } for (int i = 0; i < m; ++i) { cin>>a>>b; G[a].push_back(b); } qq(k); cout<<best<<endl; return 0; } |
English