Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2072 Accepted Submission(s): 647
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<algorithm> 6 7 using namespace std; 8 9 const int maxm = 10005 ; //最大边数 10 const int maxn = 105 ; //最大点数 11 12 struct aaa 13 { 14 int s,f,next ; 15 }; 16 17 aaa c[maxm]; 18 int sta[maxn],fa[maxn],zh[maxn]; 19 int d[maxn][maxn],e[maxn]; 20 bool b[maxn]; 21 int n,m,now,tot; 22 bool goal; 23 24 void ins(int s ,int f) //创建邻接表 25 { 26 now++; 27 c[now].s=s; 28 c[now].f=f; 29 c[now].next=sta[s]; 30 sta[s]=now; 31 } 32 33 void bfs() 34 { 35 int i,cl,op,k,t; 36 cl=0;op=1; 37 for(i=1;i<=n ;i++) fa[i]=0; 38 zh[1]=1;fa[1]=-1; 39 while(cl<op) 40 { 41 cl++; 42 k=zh[cl]; 43 for( t=sta[k] ; t ; t=c[t].next ) 44 if(b[c[t].f]&&fa[c[t].f]==0) 45 { 46 op++; 47 zh[op]=c[t].f; 48 fa[c[t].f]=c[t].s; 49 if(c[t].f==n) break; 50 } 51 if(fa[n]) break ; 52 } 53 } 54 55 void dfs(int deep) 56 { 57 int i,cl,op,l,k; 58 if(goal) return ; 59 bfs(); 60 if(fa[n]==0) 61 { 62 goal=true ; 63 return ; 64 } 65 l=0; 66 for(k=n ;k>l ; k=fa[k]) 67 { 68 l++; 69 d[deep][l]=k; 70 } 71 if(l>m) 72 { 73 goal=true; 74 return ; 75 } 76 if(deep>tot) return ; 77 78 for(i=2;i<=l;i++) 79 { 80 b[d[deep][i]]=false ; 81 if(e[d[deep][i]]==0) dfs(deep+1); 82 b[d[deep][i]]=true; 83 e[d[deep][i]]++; 84 } 85 for(i=2 ; i<=l ; i++ ) 86 e[d[deep][i]]--; 87 } 88 89 int make() 90 { 91 int i,j; 92 goal= false; 93 for(i=0 ;i<=n ;i++) 94 { 95 tot=i; 96 for(j=1;j<=n ;j++) b[j]=true; 97 memset(e,0,sizeof(e)); 98 dfs(1); 99 if(goal) return i; 100 } 101 return n; 102 } 103 104 int main() 105 { 106 int i,s,f,g; 107 while(1) 108 { 109 scanf("%d%d%d",&n,&g,&m); 110 if(n==0) break; 111 memset(sta,0,sizeof(sta)); 112 now=0; 113 for(i=1; i<=g ;i++) 114 { 115 scanf("%d%d",&s,&f); 116 ins(s,f); 117 } 118 g=make(); 119 printf("%d\n",g); 120 } 121 return 0; 122 }