#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <string> #include <bitset> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> #include <iomanip> using namespace std; #define pb push_back #define mp make_pair typedef pair<int,int> pii; typedef long long ll; typedef double ld; typedef vector<int> vi; #define fi first #define se second #define fe first #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);} #define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);} #define es(x,e) (int e=fst[x];e;e=nxt[e]) #define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e]) #define SZ 666666 int n,m,k,ans=1e9,aa[SZ],A[SZ],B[SZ]; pii as[SZ]; vector<vector<int> > cur; vector<pii> vs[2333]; int rs[SZ],ff[SZ]; vector<int> s[2333]; int gf(int x) {return (ff[x]<0)?x:(ff[x]=gf(ff[x]));} void dfs(const vector<vector<int> >& v,int g) { if(!v.size()) { ans=0; return; } if(!g) { int ca=0; for(int i=int(v.size())-1;i>=0;--i) { aa[i]=1; for(auto g:v[i]) aa[i]=max(aa[i],aa[g]+1); ca=max(ca,aa[i]); if(ca>=ans) return; } ans=ca; return; } if(g==1) { for(int i=0;i<v.size();++i) s[i].clear(); for(int i=0;i<v.size();++i) for(auto t:v[i]) s[t].pb(i); for(int i=int(v.size())-1;i>=0;--i) { A[i]=1; for(auto g:v[i]) A[i]=max(A[i],A[g]+1); } for(int i=0;i<=int(v.size());++i) ff[i]=-1; for(int i=0;i<int(v.size());++i) { B[i]=1; for(auto g:s[i]) B[i]=max(B[i],B[g]+1); } for(int j=1;j<=int(v.size());++j) vs[j].clear(); vs[0].pb(mp(0,int(v.size())-1)); for(int i=0;i<int(v.size());++i) vs[A[i]].pb(mp(0,i-1)), vs[B[i]].pb(mp(i+1,int(v.size())-1)); for(int i=0;i<int(v.size());++i) for(auto g:v[i]) if(i+1<g) vs[A[g]+B[i]].pb(mp(i+1,g-1)); for(int i=int(v.size());i>=0;--i) for(auto t:vs[i]) for(int x=gf(t.fi);x<=t.se;) rs[x]=i,ff[x]=gf(x+1),x=ff[x]; for(int i=0;i<int(v.size());++i) ans=min(ans,rs[i]); return; } pii ca(0,0); for(int i=int(v.size())-1;i>=0;--i) { as[i]=pii(1,-1); for(auto g:v[i]) as[i]=max(as[i],pii(as[g].fi+1,g)); ca=max(ca,pii(as[i].fi,i)); } int x=ca.se; vector<int> vv; while(x>=0) vv.pb(x),x=as[x].se; for(auto t:vv) { vector<vector<int> > s(v.size()-1); for(int i=0;i<v.size();++i) { if(i==t) continue; for(auto g:v[i]) if(g>t) s[i-(i>t)].pb(g-1); else if(g<t) s[i-(i>t)].pb(g); } dfs(s,g-1); } } int main() { cin>>n>>m>>k; if(k>=n) { puts("0"); return 0; } cur.resize(n); set<pii> ts; for(int i=1,a,b;i<=m;++i) cin>>a>>b,ts.insert(pii(a,b)); for(auto s:ts) cur[s.fi-1].pb(s.se-1); dfs(cur,k); cout<<ans<<"\n"; }
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 | #include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <string> #include <bitset> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> #include <iomanip> using namespace std; #define pb push_back #define mp make_pair typedef pair<int,int> pii; typedef long long ll; typedef double ld; typedef vector<int> vi; #define fi first #define se second #define fe first #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);} #define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);} #define es(x,e) (int e=fst[x];e;e=nxt[e]) #define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e]) #define SZ 666666 int n,m,k,ans=1e9,aa[SZ],A[SZ],B[SZ]; pii as[SZ]; vector<vector<int> > cur; vector<pii> vs[2333]; int rs[SZ],ff[SZ]; vector<int> s[2333]; int gf(int x) {return (ff[x]<0)?x:(ff[x]=gf(ff[x]));} void dfs(const vector<vector<int> >& v,int g) { if(!v.size()) { ans=0; return; } if(!g) { int ca=0; for(int i=int(v.size())-1;i>=0;--i) { aa[i]=1; for(auto g:v[i]) aa[i]=max(aa[i],aa[g]+1); ca=max(ca,aa[i]); if(ca>=ans) return; } ans=ca; return; } if(g==1) { for(int i=0;i<v.size();++i) s[i].clear(); for(int i=0;i<v.size();++i) for(auto t:v[i]) s[t].pb(i); for(int i=int(v.size())-1;i>=0;--i) { A[i]=1; for(auto g:v[i]) A[i]=max(A[i],A[g]+1); } for(int i=0;i<=int(v.size());++i) ff[i]=-1; for(int i=0;i<int(v.size());++i) { B[i]=1; for(auto g:s[i]) B[i]=max(B[i],B[g]+1); } for(int j=1;j<=int(v.size());++j) vs[j].clear(); vs[0].pb(mp(0,int(v.size())-1)); for(int i=0;i<int(v.size());++i) vs[A[i]].pb(mp(0,i-1)), vs[B[i]].pb(mp(i+1,int(v.size())-1)); for(int i=0;i<int(v.size());++i) for(auto g:v[i]) if(i+1<g) vs[A[g]+B[i]].pb(mp(i+1,g-1)); for(int i=int(v.size());i>=0;--i) for(auto t:vs[i]) for(int x=gf(t.fi);x<=t.se;) rs[x]=i,ff[x]=gf(x+1),x=ff[x]; for(int i=0;i<int(v.size());++i) ans=min(ans,rs[i]); return; } pii ca(0,0); for(int i=int(v.size())-1;i>=0;--i) { as[i]=pii(1,-1); for(auto g:v[i]) as[i]=max(as[i],pii(as[g].fi+1,g)); ca=max(ca,pii(as[i].fi,i)); } int x=ca.se; vector<int> vv; while(x>=0) vv.pb(x),x=as[x].se; for(auto t:vv) { vector<vector<int> > s(v.size()-1); for(int i=0;i<v.size();++i) { if(i==t) continue; for(auto g:v[i]) if(g>t) s[i-(i>t)].pb(g-1); else if(g<t) s[i-(i>t)].pb(g); } dfs(s,g-1); } } int main() { cin>>n>>m>>k; if(k>=n) { puts("0"); return 0; } cur.resize(n); set<pii> ts; for(int i=1,a,b;i<=m;++i) cin>>a>>b,ts.insert(pii(a,b)); for(auto s:ts) cur[s.fi-1].pb(s.se-1); dfs(cur,k); cout<<ans<<"\n"; } |