hdu5097(上海邀请赛I) 拓扑序(双队列)+(输入略复杂)
最小重启==
打代码还是不仔细!
1 #include<stdio.h> 2 #include<string.h> 3 #include<string> 4 #include<map> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 map<string,int>mp; 9 queue<int>q1,q2; 10 int next[2000005],value[2000005],head[2000005],now; 11 char ss[11000],s1[11000],s2[1100]; 12 int in[1100],vis[1100],need[1100]; 13 void addedge(int u,int v) 14 { 15 next[++now]=head[u]; 16 head[u]=now; 17 value[now]=v; 18 } 19 int main() 20 { 21 int T,t,i,j,nn,mm,len,ans,point,flag,x; 22 scanf("%d%*c%*c",&T); 23 for (t=1;t<=T;t++) 24 { 25 point=0; now=0; mp.clear(); 26 memset(in,0,sizeof(in)); 27 memset(head,-1,sizeof(head)); 28 memset(vis,0,sizeof(vis)); 29 while (1) 30 { 31 if (!gets(ss)) break; 32 if (!ss[0]) break; 33 len=strlen(ss); 34 for (i=0;ss[i]!=':';i++) 35 if (ss[i]!='*') s1[i]=ss[i]; 36 else s1[i]='\0'; 37 s1[i]='\0'; 38 if (!mp.count(s1)) nn=mp[s1]=++point; 39 else nn=mp[s1]; 40 if (ss[i-1]=='*') need[nn]=1; 41 else need[nn]=0; 42 for (i++;i<len;i++) 43 { 44 j=i+1; 45 while (ss[i+1]!='\0'&&ss[i+1]!=' ') 46 { 47 i++; 48 s2[i-j]=ss[i]; 49 } 50 s2[i-j+1]='\0'; 51 if (!mp.count(s2)) mm=mp[s2]=++point; 52 else mm=mp[s2]; 53 addedge(mm,nn); 54 in[nn]++; 55 } 56 } 57 while (!q1.empty()) q1.pop(); 58 while (!q2.empty()) q2.pop(); 59 ans=0; 60 for (i=1;i<=point;i++) 61 if (in[i]==0){ 62 if (need[i]) q2.push(i); 63 else q1.push(i); 64 vis[i]=1; 65 } 66 while (1) 67 { 68 while (!q1.empty()) 69 { 70 x=q1.front(); q1.pop(); 71 for (i=head[x];i!=-1;i=next[i]) 72 { 73 in[value[i]]--; 74 if (in[value[i]]==0&&vis[value[i]]==0){ 75 if (need[value[i]]) q2.push(value[i]); 76 else q1.push(value[i]); 77 vis[value[i]]=1; 78 } 79 } 80 } 81 if (q2.empty()) break; 82 ans++; 83 while (!q2.empty()){ 84 x=q2.front(); q2.pop(); 85 q1.push(x); 86 } 87 } 88 printf("Case %d: %d\n",t,ans); 89 } 90 return 0; 91 } 92 /* 93 2 94 95 glibc: 96 gcc*: glibc 97 98 uefi*: 99 gcc*: 100 raid_util*: uefi 101 gpu_driver*: uefi 102 opencl_sdk: gpu_driver gcc 103 104 */
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5098