#include <iostream> #include <stack> using namespace std; void update(short int mapa[300][300], short int maksy[300], short int what, int n, int newval, short int maks[1]) { short int i, j; if(newval < maksy[what]) return; maksy[what] = newval; if(maks[0]<newval) maks[0] = newval; for(i = 0; i < n; ++i){ if(mapa[what][i] > 1){ mapa[what][i] = newval + 1; update(mapa, maksy, what, n, newval + 1, maks); } } } void prepare(short int mapa[300][300], short int maksy[300], int n, int a1 = -1, int a2 = -1, int a3 = -1, int a4 = -1){ int i,j; for(i=0;i<n;++i) maksy[i]=0; for(i=0; i<n; ++i){ for(j=i; j<n;++j){ if((j==i) && (j != a1) && (j != a2) && (j != a3) && (j != a4)){ mapa[i][j] = 1; maksy[j] = 1; } else { mapa[i][j] = 0; } } } } int main() { int n, m, k, i, j, x, y, a1, a2, a3, a4; short int tabl [300][300]; short int maksy[300]; short int kraw[400][2]; short int maks[1]; short int mini=300; long result = 0; bool samo = false; unsigned long long l, tmp, lastl = 0; cin >> n; // 1-300 cin >> m; // 0-400 cin >> k; // 0-min(4,m) maks[0] = 0; for(i=0; i<300; ++i){ for(j=0; j<300; j++){ tabl[i][j] = 0; } if(i<n){ tabl[i][i] = 1; maksy[i] = 1; } } // skrajne przypadki if(k >= n){ cout << "0"; return 0; } if(k == (n-1)){ cout << "1"; return 0; } for(i=0; i < m; ++i){ cin >> x; cin >> y; kraw[i][0] = x-1; kraw[i][1] = y-1; tabl[x-1][y-1] = maksy[x-1] + 1; update(tabl, maksy, y-1, n, maksy[x-1] + 1, maks); } mini = maks[0]; if(k==0){ cout << mini; return 0; } for(a1=0; a1<n; ++a1){ if(k>1){ for(a2=a1+1; a2<n;++a2){ if(k>2){ for(a3=a2+1; a3<n; ++a3){ if(k>3){ for(a4=a3+1; a4<n; ++a4){ prepare(tabl, maksy, n, a1, a2, a3, a4); maks[0] = 1; for(i=0; i < m; ++i){ if((kraw[i][0]!=a1) && (kraw[i][1]!=a1) && (kraw[i][0]!=a2) && (kraw[i][1]!=a2) && (kraw[i][0]!=a3) && (kraw[i][1]!=a3) && (kraw[i][0]!=a4) && (kraw[i][1]!=a4) ){ tabl[kraw[i][0]][kraw[i][1]] = maksy[kraw[i][0]] + 1; update(tabl, maksy, kraw[i][1], n, maksy[kraw[i][0]]+1, maks); } } if(mini>maks[0]){ mini=maks[0]; } } } else { prepare(tabl, maksy, n, a1, a2, a3, -1); maks[0] = 1; for(i=0; i < m; ++i){ if((kraw[i][0]!=a1) && (kraw[i][1]!=a1) && (kraw[i][0]!=a2) && (kraw[i][1]!=a2) && (kraw[i][0]!=a3) && (kraw[i][1]!=a3) ){ tabl[kraw[i][0]][kraw[i][1]] = maksy[kraw[i][0]] + 1; update(tabl, maksy, kraw[i][1], n, maksy[kraw[i][0]]+1, maks); } } if(mini>maks[0]){ mini=maks[0]; } } } } else { prepare(tabl, maksy, n, a1, a2, -1, -1); maks[0] = 1; for(i=0; i < m; ++i){ if((kraw[i][0]!=a1) && (kraw[i][1]!=a1) && (kraw[i][0]!=a2) && (kraw[i][1]!=a2) ){ tabl[kraw[i][0]][kraw[i][1]] = maksy[kraw[i][0]] + 1; update(tabl, maksy, kraw[i][1], n, maksy[kraw[i][0]]+1, maks); } } if(mini>maks[0]){ mini=maks[0]; } } } } else { prepare(tabl, maksy, n, a1, -1, -1, -1); maks[0] = 1; for(i=0; i < m; ++i){ if((kraw[i][0]!=a1) && (kraw[i][1]!=a1)){ tabl[kraw[i][0]][kraw[i][1]] = maksy[kraw[i][0]] + 1; update(tabl, maksy, kraw[i][1], n, maksy[kraw[i][0]]+1, maks); } } if(mini>maks[0]){ mini=maks[0]; } } } cout << mini; 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 | #include <iostream> #include <stack> using namespace std; void update(short int mapa[300][300], short int maksy[300], short int what, int n, int newval, short int maks[1]) { short int i, j; if(newval < maksy[what]) return; maksy[what] = newval; if(maks[0]<newval) maks[0] = newval; for(i = 0; i < n; ++i){ if(mapa[what][i] > 1){ mapa[what][i] = newval + 1; update(mapa, maksy, what, n, newval + 1, maks); } } } void prepare(short int mapa[300][300], short int maksy[300], int n, int a1 = -1, int a2 = -1, int a3 = -1, int a4 = -1){ int i,j; for(i=0;i<n;++i) maksy[i]=0; for(i=0; i<n; ++i){ for(j=i; j<n;++j){ if((j==i) && (j != a1) && (j != a2) && (j != a3) && (j != a4)){ mapa[i][j] = 1; maksy[j] = 1; } else { mapa[i][j] = 0; } } } } int main() { int n, m, k, i, j, x, y, a1, a2, a3, a4; short int tabl [300][300]; short int maksy[300]; short int kraw[400][2]; short int maks[1]; short int mini=300; long result = 0; bool samo = false; unsigned long long l, tmp, lastl = 0; cin >> n; // 1-300 cin >> m; // 0-400 cin >> k; // 0-min(4,m) maks[0] = 0; for(i=0; i<300; ++i){ for(j=0; j<300; j++){ tabl[i][j] = 0; } if(i<n){ tabl[i][i] = 1; maksy[i] = 1; } } // skrajne przypadki if(k >= n){ cout << "0"; return 0; } if(k == (n-1)){ cout << "1"; return 0; } for(i=0; i < m; ++i){ cin >> x; cin >> y; kraw[i][0] = x-1; kraw[i][1] = y-1; tabl[x-1][y-1] = maksy[x-1] + 1; update(tabl, maksy, y-1, n, maksy[x-1] + 1, maks); } mini = maks[0]; if(k==0){ cout << mini; return 0; } for(a1=0; a1<n; ++a1){ if(k>1){ for(a2=a1+1; a2<n;++a2){ if(k>2){ for(a3=a2+1; a3<n; ++a3){ if(k>3){ for(a4=a3+1; a4<n; ++a4){ prepare(tabl, maksy, n, a1, a2, a3, a4); maks[0] = 1; for(i=0; i < m; ++i){ if((kraw[i][0]!=a1) && (kraw[i][1]!=a1) && (kraw[i][0]!=a2) && (kraw[i][1]!=a2) && (kraw[i][0]!=a3) && (kraw[i][1]!=a3) && (kraw[i][0]!=a4) && (kraw[i][1]!=a4) ){ tabl[kraw[i][0]][kraw[i][1]] = maksy[kraw[i][0]] + 1; update(tabl, maksy, kraw[i][1], n, maksy[kraw[i][0]]+1, maks); } } if(mini>maks[0]){ mini=maks[0]; } } } else { prepare(tabl, maksy, n, a1, a2, a3, -1); maks[0] = 1; for(i=0; i < m; ++i){ if((kraw[i][0]!=a1) && (kraw[i][1]!=a1) && (kraw[i][0]!=a2) && (kraw[i][1]!=a2) && (kraw[i][0]!=a3) && (kraw[i][1]!=a3) ){ tabl[kraw[i][0]][kraw[i][1]] = maksy[kraw[i][0]] + 1; update(tabl, maksy, kraw[i][1], n, maksy[kraw[i][0]]+1, maks); } } if(mini>maks[0]){ mini=maks[0]; } } } } else { prepare(tabl, maksy, n, a1, a2, -1, -1); maks[0] = 1; for(i=0; i < m; ++i){ if((kraw[i][0]!=a1) && (kraw[i][1]!=a1) && (kraw[i][0]!=a2) && (kraw[i][1]!=a2) ){ tabl[kraw[i][0]][kraw[i][1]] = maksy[kraw[i][0]] + 1; update(tabl, maksy, kraw[i][1], n, maksy[kraw[i][0]]+1, maks); } } if(mini>maks[0]){ mini=maks[0]; } } } } else { prepare(tabl, maksy, n, a1, -1, -1, -1); maks[0] = 1; for(i=0; i < m; ++i){ if((kraw[i][0]!=a1) && (kraw[i][1]!=a1)){ tabl[kraw[i][0]][kraw[i][1]] = maksy[kraw[i][0]] + 1; update(tabl, maksy, kraw[i][1], n, maksy[kraw[i][0]]+1, maks); } } if(mini>maks[0]){ mini=maks[0]; } } } cout << mini; return 0; } |