#include <bits/stdc++.h> #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define PII pair<int,int> #define mp make_pair #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define debug(x) {cout <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cout <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} #define jeb() fflush(stdout); using namespace std; const int max_n = 3e5 + 5; int n, m, k; VI g[305]; VI og[305]; int active[305]; int dp[305]; int prv[305]; int ans; int get_ans() { int lans = 0; FOR(j,0,n) { if (active[j] == 0) { dp[j] = 0; } else { dp[j] = 1; FORE(u, og[j]) { dp[j] = max(dp[j], dp[*u] + 1); } lans = max(lans, dp[j]); if (lans >= ans) return ans; } } if (lans >= ans) return ans; ans = lans; return lans; } VI get_longest_path() { FOR(i,0,n) { prv[i] = -1; } int lans = 0; int dla = -1; FOR(j,0,n) { if (active[j] == 0) { dp[j] = 0; } else { dp[j] = 1; FORE(u, og[j]) { if (active[*u] && dp[*u]+1 > dp[j]) { dp[j] = dp[*u] + 1; prv[j] = *u; } } if (dp[j] > lans) { lans = dp[j]; dla = j; } } } VI ja; while (dla!=-1) { ja.pb(dla); dla = prv[dla]; } return ja; } void smutal() { FOR(i,0,n) active[i] = 1; ans = n; VI my_path1 = get_longest_path(); ans = my_path1.size(); if (k >= 1) { FOR(i1,0,my_path1.size()) { int v1 = my_path1[i1]; active[v1] = 0; VI my_path2 = get_longest_path(); ans = min(ans, (int) my_path2.size()); if (k >= 2) { FOR(i2,0,my_path2.size()) { int v2 = my_path2[i2]; active[v2] = 0; VI my_path3 = get_longest_path(); ans = min(ans, (int) my_path3.size()); if (k >= 3) { FOR(i3,0,my_path3.size()) { int v3 = my_path3[i3]; active[v3] = 0; VI my_path4 = get_longest_path(); ans = min(ans, (int) my_path4.size()); if (k >= 4) { FOR(i4,0,my_path4.size()) { int v4 = my_path4[i4]; active[v4] = 0; ans = min(ans, get_ans()); active[v4] = 1; } } active[v3] = 1; } } active[v2] = 1; } } active[v1] = 1; } } printf("%d\n", ans); } void brutal() { ans = n; FOR(i,0,n) active[i] = 1; if (k == 0) ans = min(ans, get_ans()); if (k >= 1) { FOR(i1,0,n) { active[i1] = 0; if (k == 1) ans = min(ans, get_ans()); else { FOR(i2,i1+1,n) { active[i2] = 0; if (k == 2) ans = min(ans, get_ans()); else { FOR(i3,i2+1,n) { active[i3] = 0; if (k == 3) ans = min(ans, get_ans()); else { FOR(i4,i3+1,n) { active[i4] = 0; ans = min(ans, get_ans()); active[i4] = 1; } } active[i3] = 1; } } active[i2] = 1; } } active[i1] = 1; } } printf("%d\n", ans); } int main() { read3(n, m, k); FOR(i,0,m) { make2(a, b); a--; b--; g[a].pb(b); og[b].pb(a); } if (n <= 65) { brutal(); return 0; } else { smutal(); 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 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | #include <bits/stdc++.h> #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define PII pair<int,int> #define mp make_pair #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define debug(x) {cout <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cout <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} #define jeb() fflush(stdout); using namespace std; const int max_n = 3e5 + 5; int n, m, k; VI g[305]; VI og[305]; int active[305]; int dp[305]; int prv[305]; int ans; int get_ans() { int lans = 0; FOR(j,0,n) { if (active[j] == 0) { dp[j] = 0; } else { dp[j] = 1; FORE(u, og[j]) { dp[j] = max(dp[j], dp[*u] + 1); } lans = max(lans, dp[j]); if (lans >= ans) return ans; } } if (lans >= ans) return ans; ans = lans; return lans; } VI get_longest_path() { FOR(i,0,n) { prv[i] = -1; } int lans = 0; int dla = -1; FOR(j,0,n) { if (active[j] == 0) { dp[j] = 0; } else { dp[j] = 1; FORE(u, og[j]) { if (active[*u] && dp[*u]+1 > dp[j]) { dp[j] = dp[*u] + 1; prv[j] = *u; } } if (dp[j] > lans) { lans = dp[j]; dla = j; } } } VI ja; while (dla!=-1) { ja.pb(dla); dla = prv[dla]; } return ja; } void smutal() { FOR(i,0,n) active[i] = 1; ans = n; VI my_path1 = get_longest_path(); ans = my_path1.size(); if (k >= 1) { FOR(i1,0,my_path1.size()) { int v1 = my_path1[i1]; active[v1] = 0; VI my_path2 = get_longest_path(); ans = min(ans, (int) my_path2.size()); if (k >= 2) { FOR(i2,0,my_path2.size()) { int v2 = my_path2[i2]; active[v2] = 0; VI my_path3 = get_longest_path(); ans = min(ans, (int) my_path3.size()); if (k >= 3) { FOR(i3,0,my_path3.size()) { int v3 = my_path3[i3]; active[v3] = 0; VI my_path4 = get_longest_path(); ans = min(ans, (int) my_path4.size()); if (k >= 4) { FOR(i4,0,my_path4.size()) { int v4 = my_path4[i4]; active[v4] = 0; ans = min(ans, get_ans()); active[v4] = 1; } } active[v3] = 1; } } active[v2] = 1; } } active[v1] = 1; } } printf("%d\n", ans); } void brutal() { ans = n; FOR(i,0,n) active[i] = 1; if (k == 0) ans = min(ans, get_ans()); if (k >= 1) { FOR(i1,0,n) { active[i1] = 0; if (k == 1) ans = min(ans, get_ans()); else { FOR(i2,i1+1,n) { active[i2] = 0; if (k == 2) ans = min(ans, get_ans()); else { FOR(i3,i2+1,n) { active[i3] = 0; if (k == 3) ans = min(ans, get_ans()); else { FOR(i4,i3+1,n) { active[i4] = 0; ans = min(ans, get_ans()); active[i4] = 1; } } active[i3] = 1; } } active[i2] = 1; } } active[i1] = 1; } } printf("%d\n", ans); } int main() { read3(n, m, k); FOR(i,0,m) { make2(a, b); a--; b--; g[a].pb(b); og[b].pb(a); } if (n <= 65) { brutal(); return 0; } else { smutal(); return 0; } } |