#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; // head const int N=1010; int n,m,k,ans,u,v; int dp[N],dp2[N],pre[N],del[N],cnt[N]; VI e[N],pp; vector<PII> mod[N],eg; VI find() { int z=0; int posz=-1; for (int u=1;u<=n;u++) if (!del[u]) { dp[u]=1; pre[u]=-1; for (auto v:e[u]) if (!del[v]) { if (dp[v]+1>dp[u]) { dp[u]=dp[v]+1; pre[u]=v; } } if (dp[u]>z) { z=dp[u]; posz=u; } } VI pt; while (1) { pt.pb(posz); posz=pre[posz]; if (posz==-1) break; } reverse(all(pt)); return pt; } bitset<310> ff[310][310]; int solvefast() { VI qq=pp; while (SZ(qq)<3) qq.pb(0); sort(all(qq)); if (ff[qq[0]][qq[1]][qq[2]]) return n; ff[qq[0]][qq[1]][qq[2]]=1; int z=0; for (int u=1;u<=n;u++) if (!del[u]) { dp[u]=1; for (auto v:e[u]) if (!del[v]) { dp[u]=max(dp[u],dp[v]+1); } z=max(z,dp[u]); } for (int u=1;u<=n;u++) dp2[u]=1; for (int u=n;u>=1;u--) if (!del[u]) { for (auto v:e[u]) if (!del[v]) { dp2[v]=max(dp2[v],dp2[u]+1); } } assert(z<=60); for (int i=0;i<=60;i++) cnt[i]=0; for (int i=1;i<=n+1;i++) mod[i].clear(); for (auto p:eg) { int u=p.fi,v=p.se; if (del[u]||del[v]) continue; mod[u+1].pb(mp(dp[u]+dp2[v],1)); mod[v].pb(mp(dp[u]+dp2[v],-1)); } ll st=0,mst=1ll<<61; for (int i=1;i<=n;i++) { for (auto w:mod[i]) { cnt[w.fi]+=w.se; if (w.se==1&&cnt[w.fi]==1) st^=(1ll<<w.fi); if (w.se==-1&&cnt[w.fi]==0) st^=(1ll<<w.fi); } if (!del[i]) mst=min(mst,st); } int ans=0; for (int i=1;i<=z;i++) if (mst&(1ll<<i)) ans=i; return ans; } void dfs(int dep) { if (dep==1) { ans=min(ans,solvefast()); // ans=min(ans,SZ(find())); } else { VI w=find(); ans=min(ans,SZ(w)); for (auto u:w) { del[u]=1; pp.pb(u); dfs(dep-1); del[u]=0; pp.pop_back(); } } } int main() { scanf("%d%d%d",&n,&m,&k); rep(i,0,m) { scanf("%d%d",&u,&v); e[v].pb(u); eg.pb(mp(u,v)); } rep(i,1,n+1) eg.pb(mp(0,i)),eg.pb(mp(i,n+1)); ans=SZ(find()); if (k==0) { printf("%d\n",ans); return 0; } if (k==n) { puts("0"); return 0; } dfs(k); printf("%d\n",ans); }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; // head const int N=1010; int n,m,k,ans,u,v; int dp[N],dp2[N],pre[N],del[N],cnt[N]; VI e[N],pp; vector<PII> mod[N],eg; VI find() { int z=0; int posz=-1; for (int u=1;u<=n;u++) if (!del[u]) { dp[u]=1; pre[u]=-1; for (auto v:e[u]) if (!del[v]) { if (dp[v]+1>dp[u]) { dp[u]=dp[v]+1; pre[u]=v; } } if (dp[u]>z) { z=dp[u]; posz=u; } } VI pt; while (1) { pt.pb(posz); posz=pre[posz]; if (posz==-1) break; } reverse(all(pt)); return pt; } bitset<310> ff[310][310]; int solvefast() { VI qq=pp; while (SZ(qq)<3) qq.pb(0); sort(all(qq)); if (ff[qq[0]][qq[1]][qq[2]]) return n; ff[qq[0]][qq[1]][qq[2]]=1; int z=0; for (int u=1;u<=n;u++) if (!del[u]) { dp[u]=1; for (auto v:e[u]) if (!del[v]) { dp[u]=max(dp[u],dp[v]+1); } z=max(z,dp[u]); } for (int u=1;u<=n;u++) dp2[u]=1; for (int u=n;u>=1;u--) if (!del[u]) { for (auto v:e[u]) if (!del[v]) { dp2[v]=max(dp2[v],dp2[u]+1); } } assert(z<=60); for (int i=0;i<=60;i++) cnt[i]=0; for (int i=1;i<=n+1;i++) mod[i].clear(); for (auto p:eg) { int u=p.fi,v=p.se; if (del[u]||del[v]) continue; mod[u+1].pb(mp(dp[u]+dp2[v],1)); mod[v].pb(mp(dp[u]+dp2[v],-1)); } ll st=0,mst=1ll<<61; for (int i=1;i<=n;i++) { for (auto w:mod[i]) { cnt[w.fi]+=w.se; if (w.se==1&&cnt[w.fi]==1) st^=(1ll<<w.fi); if (w.se==-1&&cnt[w.fi]==0) st^=(1ll<<w.fi); } if (!del[i]) mst=min(mst,st); } int ans=0; for (int i=1;i<=z;i++) if (mst&(1ll<<i)) ans=i; return ans; } void dfs(int dep) { if (dep==1) { ans=min(ans,solvefast()); // ans=min(ans,SZ(find())); } else { VI w=find(); ans=min(ans,SZ(w)); for (auto u:w) { del[u]=1; pp.pb(u); dfs(dep-1); del[u]=0; pp.pop_back(); } } } int main() { scanf("%d%d%d",&n,&m,&k); rep(i,0,m) { scanf("%d%d",&u,&v); e[v].pb(u); eg.pb(mp(u,v)); } rep(i,1,n+1) eg.pb(mp(0,i)),eg.pb(mp(i,n+1)); ans=SZ(find()); if (k==0) { printf("%d\n",ans); return 0; } if (k==n) { puts("0"); return 0; } dfs(k); printf("%d\n",ans); } |