#include <cstdio> #include <cassert> #include <set> #include <algorithm> #include <cstring> #include <vector> using std::vector; const int M = 313; vector<int> v[M], cc, a, b; vector<int> zo; int ret; int calc(int n) { a = b = vector<int>(n+1); for (int i = n; i >= 0; i--) { for (int j = 0; j < v[i].size(); j++) if (zo[v[i][j]] >= 0) { int x = a[v[i][j]]+1; if (a[i] < x) { a[i] = x; b[i] = v[i][j]; } } } cc.clear(); int i = 0; while(a[i]) { i = b[i]; cc.push_back(i); } return a[0]; } void go(int n, int k) { //printf("%d %d\n", n, k); if (!k) { ret = std::min(ret, calc(n)); return; } calc(n); vector<int> my = cc; for(int i = 0; i < my.size(); i++) { if(!zo[my[i]]) { zo[my[i]] = -1; go(n, k-1); zo[my[i]] = 0; } zo[my[i]]++; } for (int i = 0; i < my.size(); i++) { zo[my[i]]--; } } int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); for (int i = 1; i<=n; i++) v[0].push_back(i); vector<std::pair<int, int> > tt; for (int i = 1; i <= n; i++) for (int j = i+1; j <= n; j++) tt.push_back(std::make_pair(i, j)); //srand(time(0)); for (int i = 0; i < m; i++) { int a,b; /*int u = rand() % tt.size(); a = tt[u].first; b = tt[u].second; tt[u] = tt.back(); tt.pop_back(); */ scanf("%d %d",&a,&b); v[a].push_back(b); } zo = vector<int>(n+1); ret = calc(n); go(n, k); printf("%d\n", ret); }
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 | #include <cstdio> #include <cassert> #include <set> #include <algorithm> #include <cstring> #include <vector> using std::vector; const int M = 313; vector<int> v[M], cc, a, b; vector<int> zo; int ret; int calc(int n) { a = b = vector<int>(n+1); for (int i = n; i >= 0; i--) { for (int j = 0; j < v[i].size(); j++) if (zo[v[i][j]] >= 0) { int x = a[v[i][j]]+1; if (a[i] < x) { a[i] = x; b[i] = v[i][j]; } } } cc.clear(); int i = 0; while(a[i]) { i = b[i]; cc.push_back(i); } return a[0]; } void go(int n, int k) { //printf("%d %d\n", n, k); if (!k) { ret = std::min(ret, calc(n)); return; } calc(n); vector<int> my = cc; for(int i = 0; i < my.size(); i++) { if(!zo[my[i]]) { zo[my[i]] = -1; go(n, k-1); zo[my[i]] = 0; } zo[my[i]]++; } for (int i = 0; i < my.size(); i++) { zo[my[i]]--; } } int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); for (int i = 1; i<=n; i++) v[0].push_back(i); vector<std::pair<int, int> > tt; for (int i = 1; i <= n; i++) for (int j = i+1; j <= n; j++) tt.push_back(std::make_pair(i, j)); //srand(time(0)); for (int i = 0; i < m; i++) { int a,b; /*int u = rand() % tt.size(); a = tt[u].first; b = tt[u].second; tt[u] = tt.back(); tt.pop_back(); */ scanf("%d %d",&a,&b); v[a].push_back(b); } zo = vector<int>(n+1); ret = calc(n); go(n, k); printf("%d\n", ret); } |