#include <bits/stdc++.h> #include <random> #include <ext/pb_ds/assoc_container.hpp> //using namespace __gnu_pbds; using namespace std; #define ff first #define dd second #define mp make_pair #define lld long long #define pb emplace_back #define sz size() #define For(i,s,a) for(int i=(int)s;i<(int)a;++i) #define pii pair<int,int> #define pll pair<lld,lld> #define rpt(S,it) for(auto it=S.begin();it!=S.end();++it) #define mod (lld)(1e9+7) #define scanf(...) scanf(__VA_ARGS__)?:0 #define P first #define S second #ifdef __WIN32__ #define gcx getchar #elif __linux__ #define gcx getchar_unlocked #endif #define piii pair<pii,pii> //template<typename T> #define T int inline void scan(T *i) { register T t=0; register char z='a'; register bool neg=0; while(z<'0' || z>'9'){if(z=='-')neg^=1; z=gcx();} while(z>='0' && z<='9') { t=(t<<3ll)+(t<<1ll)+z-'0'; z=gcx(); } if(neg)t=~(t-1); *i=t; } vector<int>c[301],rc[301]; int ts[301],_end; int dp[311]; void dfs(int x) { dp[x]=1; For(i,0,c[x].sz)if(!dp[c[x][i]]) dfs(c[x][i]); ts[--_end]=x; } lld get(lld a) { int tmp=0; For(i,0,a) tmp=max(tmp,dp[ts[i]]);//cout<<dp[ts[i]]<<" ";puts(""); return tmp; } void det(lld allowed, lld alt, lld a) { For(x,0,a){dp[ts[x]]=1; For(i,0,rc[ts[x]].sz) dp[ts[x]]=max(dp[ts[x]],dp[rc[ts[x]][i]]+1); if(dp[ts[x]]>=allowed && alt) dp[ts[x]]=-1,--alt;} } void ran(lld allowed, lld alt, lld a) { For(x,0,a){dp[ts[x]]=1; For(i,0,rc[ts[x]].sz) dp[ts[x]]=max(dp[ts[x]],dp[rc[ts[x]][i]]+1); lld cha=rand()%100+1; if((dp[ts[x]]-allowed)>-2) cha+=20; //if(ts[x]==1 || ts[x]==5)cha=100;else cha=0; if(cha>100 && alt)//cout<<ts[x]<<" "<<allowed<<" "<<alt<<endl, dp[ts[x]]=-1,--alt; } } lld rec(lld tries, lld all, lld alt, lld a) { lld ret=1000000000; memset(dp,0,sizeof(dp[0])*(a+2)); det(all,alt,a); ret=min(ret,get(a)); // puts(""); // cout<<all<<" "<<alt<<" "<<a<<endl; // tries=0; // tries=10; while(tries--) { memset(dp,0,sizeof(dp[0])*(a+2)); ran(all,alt,a); ret=min(ret,get(a)); } return ret; } bitset<301>used; void sdet(int a) { memset(dp,0,sizeof(dp[0])*(a+2)); For(x,0,a) if(used[ts[x]])dp[ts[x]]=-1; else { dp[ts[x]]=1; For(i,0,rc[ts[x]].sz) dp[ts[x]]=max(dp[ts[x]],dp[rc[ts[x]][i]]+1); } } void brut(lld a, lld b, lld d) { lld wyn=1000000; if(d==1) { For(i,1,a+1){ used=0; used[i]=1; sdet(a); wyn=min(wyn,get(a)); // cout<<get(a)<<endl; } } else if(d==2) { For(i,1,a) For(j,i+1,a+1) { used=0; used[i]=used[j]=1; sdet(a); wyn=min(wyn,get(a)); } } else if(d==3) { For(i,1,a-1) For(j,i+1,a) For(k,j+1,a+1) { used=0; used[i]=used[j]=used[k]=1; sdet(a); wyn=min(wyn,get(a)); } } else if(d==4) { For(i,1,a-2) For(j,i+1,a-1) For(k,j+1,a) For(l,k+1,a+1) { used=0; used[i]=used[j]=used[k]=used[l]=1; sdet(a); wyn=min(wyn,get(a)); } } printf("%lld",wyn); } int main() { srand(69692137); int a,b,d,g,h; scan(&a),scan(&b),scan(&d); For(i,0,b)scan(&g),scan(&h),c[g].pb(h),rc[h].pb(g); _end=a; For(i,1,a+1) if(!dp[i])dfs(i); if((d==4 && a<60) || (d==3 && a<140) || (d<3)) { brut(a,b,d); return 0; } lld minx=a; lld tries=20000000ll/((a)*a); For(i,1,a+1) minx=min(minx,rec(tries,i,d,a)); printf("%lld",minx); } /* 5 6 2 1 2 1 3 1 4 2 5 3 5 4 5 */
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 196 197 198 199 200 201 | #include <bits/stdc++.h> #include <random> #include <ext/pb_ds/assoc_container.hpp> //using namespace __gnu_pbds; using namespace std; #define ff first #define dd second #define mp make_pair #define lld long long #define pb emplace_back #define sz size() #define For(i,s,a) for(int i=(int)s;i<(int)a;++i) #define pii pair<int,int> #define pll pair<lld,lld> #define rpt(S,it) for(auto it=S.begin();it!=S.end();++it) #define mod (lld)(1e9+7) #define scanf(...) scanf(__VA_ARGS__)?:0 #define P first #define S second #ifdef __WIN32__ #define gcx getchar #elif __linux__ #define gcx getchar_unlocked #endif #define piii pair<pii,pii> //template<typename T> #define T int inline void scan(T *i) { register T t=0; register char z='a'; register bool neg=0; while(z<'0' || z>'9'){if(z=='-')neg^=1; z=gcx();} while(z>='0' && z<='9') { t=(t<<3ll)+(t<<1ll)+z-'0'; z=gcx(); } if(neg)t=~(t-1); *i=t; } vector<int>c[301],rc[301]; int ts[301],_end; int dp[311]; void dfs(int x) { dp[x]=1; For(i,0,c[x].sz)if(!dp[c[x][i]]) dfs(c[x][i]); ts[--_end]=x; } lld get(lld a) { int tmp=0; For(i,0,a) tmp=max(tmp,dp[ts[i]]);//cout<<dp[ts[i]]<<" ";puts(""); return tmp; } void det(lld allowed, lld alt, lld a) { For(x,0,a){dp[ts[x]]=1; For(i,0,rc[ts[x]].sz) dp[ts[x]]=max(dp[ts[x]],dp[rc[ts[x]][i]]+1); if(dp[ts[x]]>=allowed && alt) dp[ts[x]]=-1,--alt;} } void ran(lld allowed, lld alt, lld a) { For(x,0,a){dp[ts[x]]=1; For(i,0,rc[ts[x]].sz) dp[ts[x]]=max(dp[ts[x]],dp[rc[ts[x]][i]]+1); lld cha=rand()%100+1; if((dp[ts[x]]-allowed)>-2) cha+=20; //if(ts[x]==1 || ts[x]==5)cha=100;else cha=0; if(cha>100 && alt)//cout<<ts[x]<<" "<<allowed<<" "<<alt<<endl, dp[ts[x]]=-1,--alt; } } lld rec(lld tries, lld all, lld alt, lld a) { lld ret=1000000000; memset(dp,0,sizeof(dp[0])*(a+2)); det(all,alt,a); ret=min(ret,get(a)); // puts(""); // cout<<all<<" "<<alt<<" "<<a<<endl; // tries=0; // tries=10; while(tries--) { memset(dp,0,sizeof(dp[0])*(a+2)); ran(all,alt,a); ret=min(ret,get(a)); } return ret; } bitset<301>used; void sdet(int a) { memset(dp,0,sizeof(dp[0])*(a+2)); For(x,0,a) if(used[ts[x]])dp[ts[x]]=-1; else { dp[ts[x]]=1; For(i,0,rc[ts[x]].sz) dp[ts[x]]=max(dp[ts[x]],dp[rc[ts[x]][i]]+1); } } void brut(lld a, lld b, lld d) { lld wyn=1000000; if(d==1) { For(i,1,a+1){ used=0; used[i]=1; sdet(a); wyn=min(wyn,get(a)); // cout<<get(a)<<endl; } } else if(d==2) { For(i,1,a) For(j,i+1,a+1) { used=0; used[i]=used[j]=1; sdet(a); wyn=min(wyn,get(a)); } } else if(d==3) { For(i,1,a-1) For(j,i+1,a) For(k,j+1,a+1) { used=0; used[i]=used[j]=used[k]=1; sdet(a); wyn=min(wyn,get(a)); } } else if(d==4) { For(i,1,a-2) For(j,i+1,a-1) For(k,j+1,a) For(l,k+1,a+1) { used=0; used[i]=used[j]=used[k]=used[l]=1; sdet(a); wyn=min(wyn,get(a)); } } printf("%lld",wyn); } int main() { srand(69692137); int a,b,d,g,h; scan(&a),scan(&b),scan(&d); For(i,0,b)scan(&g),scan(&h),c[g].pb(h),rc[h].pb(g); _end=a; For(i,1,a+1) if(!dp[i])dfs(i); if((d==4 && a<60) || (d==3 && a<140) || (d<3)) { brut(a,b,d); return 0; } lld minx=a; lld tries=20000000ll/((a)*a); For(i,1,a+1) minx=min(minx,rec(tries,i,d,a)); printf("%lld",minx); } /* 5 6 2 1 2 1 3 1 4 2 5 3 5 4 5 */ |