1.Luogu P1155 双栈排序
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define inf 0x7fffffff 5 #define MN 1005 6 #define ME 1000005 7 using namespace std; 8 inline int in(){ 9 int x=0;bool f=0; char c; 10 for (;(c=getchar())<‘0‘||c>‘9‘;f=c==‘-‘); 11 for (x=c-‘0‘;(c=getchar())>=‘0‘&&c<=‘9‘;x=(x<<3)+(x<<1)+c-‘0‘); 12 return f?-x:x; 13 } 14 struct edge{ 15 int to,next; 16 }e[ME]; 17 int head[MN],col[MN],a[MN],f[MN],st1[MN],st2[MN]; 18 int n,cur,c1,c2,cnt=0; 19 inline void ins(int x,int y){ 20 e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt; 21 } 22 inline bool dfs(int u,int c){ 23 col[u]=c; 24 for (int i=head[u];i;i=e[i].next){ 25 int v=e[i].to; 26 if (col[v]==c) return 0; 27 if (col[v]==-1) dfs(v,c^1); 28 }return 1; 29 } 30 int main() 31 { 32 n=in();f[n+1]=inf;memset(col,-1,sizeof(col)); 33 for (int i=1;i<=n;++i) a[i]=in(); 34 for (int i=n;i;--i) f[i]=min(a[i],f[i+1]); 35 for (int i=1;i<=n;++i) 36 for (int j=i+1;j<=n;++j){ 37 if (a[i]<a[j]&&a[i]>f[j+1]) ins(i,j),ins(j,i); 38 }for (int i=1;i<=n;++i) if(col[i]==-1) 39 if (!dfs(i,0)) {printf("0");return 0;}cur=1; 40 for (int i=1;i<=n;++i){ 41 if (!col[i]) st1[++c1]=a[i],printf("a "); 42 else st2[++c2]=a[i],printf("c "); 43 while ((c1&&st1[c1]==cur)||(c2&&st2[c2]==cur)){ 44 while (c1&&st1[c1]==cur) printf("b "),--c1,++cur; 45 while (c2&&st2[c2]==cur) printf("d "),--c2,++cur; 46 } 47 }return 0; 48 }
2.Luogu P1099 树网的核
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define inf 0x3f3f3f3f 5 #define ME 605 6 #define MN 303 7 using namespace std; 8 inline int in(){ 9 int x=0;bool f=0; char c; 10 for (;(c=getchar())<‘0‘||c>‘9‘;f=c==‘-‘); 11 for (x=c-‘0‘;(c=getchar())>=‘0‘&&c<=‘9‘;x=(x<<3)+(x<<1)+c-‘0‘); 12 return f?-x:x; 13 } 14 struct edge{ 15 int to,next,val; 16 }e[ME]; 17 int head[MN],dis[MN][MN],st[MN]; 18 int n,m,s,t,dmx=0,md=inf,res=inf,mx=0,dx,dy,cnt=0,v,ct=0; 19 bool vis[MN],mk=0; 20 inline void ins(int x,int y,int v){ 21 e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt;e[cnt].val=v; 22 dis[x][y]=v; 23 } 24 inline void dfs(int u){ 25 if (u==dy) {mk=1;return;} 26 for (int i=head[u];i;i=e[i].next){ 27 int v=e[i].to; 28 if (!vis[v]){ 29 vis[v]=1;st[++ct]=v;dfs(v); 30 if (mk) return;--ct; 31 } 32 } 33 } 34 35 int main() 36 { 37 n=in();m=in(); 38 memset(dis,0x3f,sizeof(dis)); 39 for (int i=1;i<=n;++i) dis[i][i]=0; 40 for (int i=1;i<n;++i){ 41 s=in();t=in();v=in(); 42 ins(s,t,v);ins(t,s,v); 43 } 44 for (int k=1;k<=n;++k) 45 for (int i=1;i<=n;++i) 46 for (int j=1;j<=n;++j) 47 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 48 for (int i=1;i<=n;++i) 49 for (int j=i+1;j<=n;++j){ 50 if (dmx<dis[i][j]) dmx=dis[i][j],dx=i,dy=j; 51 }vis[dx]=1,st[++ct]=dx;dfs(dx);memset(vis,0,sizeof(vis)); 52 for(int i=1;i<=ct;++i) 53 for(int j=i;j<=ct;++j){ 54 int sum=dis[st[i]][st[j]],mx=0;if (sum>m) break; 55 for (int k=1;k<=n;++k){ 56 md=inf;for (int l=i;l<=j;++l) 57 md=min(md,dis[k][st[l]]);mx=max(mx,md); 58 }res=min(res,mx?mx:inf); 59 }printf("%d",res);return 0; 60 }
<not_completed>
时间: 2024-10-31 08:48:56