Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 457 Accepted Submission(s): 129
1 4 5 2 1 1 2 3 3 2 4 2 4 4 1 4 2 3
2
Statistic | Submit | Discuss | Back
我错误的代码啊,至今没发现哪里错了。。。。 用FLOYED写得,谁跟我说一下 #include<cstdio> #include<cstring> const int N=510; const int M=300001; const int inf=0x7fffffff; int head[N]; int map[310][310]; struct Edge { int v,next,w; } edge[M]; int cnt,n,s,t,m,f; void addedge(int u,int v,int w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++; } int sap() { int pre[N],cur[N],dis[N],gap[N]; int flow=0,aug=inf,u; bool flag; for(int i=0; i<n; i++) { cur[i]=head[i]; gap[i]=dis[i]=0; } gap[s]=n; u=pre[s]=s; while(dis[s]<n) { flag=0; for(int &j=cur[u]; j!=-1; j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[u]==dis[v]+1) { flag=1; if(edge[j].w<aug) aug=edge[j].w; pre[v]=u; u=v; if(u==t) { flow+=aug; while(u!=s) { u=pre[u]; edge[cur[u]].w-=aug; edge[cur[u]^1].w+=aug; } aug=inf; } break; } } if(flag) continue; int mindis=n; for(int j=head[u]; j!=-1; j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[v]<mindis) { mindis=dis[v]; cur[u]=j; } } if((--gap[dis[u]])==0) break; gap[dis[u]=mindis+1]++; u=pre[u]; } return flow; } void floyed() { for(int k=1; k<=n; k++) for(int i=1; i<=n; i++) for(int j=n+1; j<=n+n; j++) if(map[i][k]&&map[k][j]) map[i][j]=true; } bool iSAP(int lim) { s=0; t=n*2+1; cnt=0; memset(head,-1,sizeof(head)); for(int i=1; i<=n; i++) addedge(s,i,lim),addedge(i+n,t,lim); for(int i=1;i<=n;i++) for(int j=n+1;j<=n+n;j++) if(map[i][j]) addedge(i,j,1); int nn=n; n=n*2+2; int t=sap(); n=nn; if(t==n*lim) return true; return false; } void solve() { int head=0,end=n,ans=0; while(head<=end) { int mid=(head+end)>>1; if(iSAP(mid)) { ans=mid; head=mid+1; } else end=mid-1; } printf("%d/n",ans); } int main() { int T; scanf("%d",&T); while(T--) { memset(map,false,sizeof(map)); scanf("%d%d%d",&n,&m,&f); for(int i=1; i<=m; i++) { int x,y; scanf("%d%d",&x,&y); map[x][y+n]=true; } for(int i=1; i<=f; i++) { int x,y; scanf("%d%d",&x,&y); map[x][y]=map[y][x]=true; } floyed(); solve(); } return 0; }
跟着网上的人用并查集能过,还有这题目很恶心,TLE,前面。。。。 #include<cstdio> #include<cstring> const int N=510; const int M=300001; const int inf=0x7fffffff; int head[N]; int map[310][310]; int p[310]; struct Edge { int v,next,w; } edge[M]; struct E { int x,y; }e[300000]; int cnt,n,s,t,m,f; void addedge(int u,int v,int w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++; } int find(int x) { if(p[x]!=x) p[x]=find(p[x]); return p[x]; } void Union(int a,int b) { a=find(a); b=find(b); p[b]=a; } int sap() { int pre[N],cur[N],dis[N],gap[N]; int flow=0,aug=inf,u; bool flag; for(int i=0; i<n; i++) { cur[i]=head[i]; gap[i]=dis[i]=0; } gap[s]=n; u=pre[s]=s; while(dis[s]<n) { flag=0; for(int &j=cur[u]; j!=-1; j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[u]==dis[v]+1) { flag=1; if(edge[j].w<aug) aug=edge[j].w; pre[v]=u; u=v; if(u==t) { flow+=aug; while(u!=s) { u=pre[u]; edge[cur[u]].w-=aug; edge[cur[u]^1].w+=aug; } aug=inf; } break; } } if(flag) continue; int mindis=n; for(int j=head[u]; j!=-1; j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[v]<mindis) { mindis=dis[v]; cur[u]=j; } } if((--gap[dis[u]])==0) break; gap[dis[u]=mindis+1]++; u=pre[u]; } return flow; } bool iSAP(int lim) { s=0; t=n*2+1; cnt=0; memset(head,-1,sizeof(head)); memset(map,0,sizeof(map)); for(int i=1; i<=n; i++) addedge(s,i,lim),addedge(i+n,t,lim); /*for(int i=1;i<=n;i++) for(int j=n+1;j<=n+n;j++) if(map[i][j]) { //int a=find(i); //int b=find(j); // if(a==b) addedge(i,j,1); } */ for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(p[j]==p[e[i].x]&&!map[j][e[i].y+n]) { map[j][e[i].y+n]=1; addedge(j,e[i].y+n,1); } } } int nn=n; n=n*2+2; int t=sap(); n=nn; if(t==n*lim) return true; return false; } void solve() { for(int i=1;i<=n;i++) find(i); int head=0,end=n,ans=0; while(head<=end) { int mid=(head+end)>>1; if(iSAP(mid)) { ans=mid; head=mid+1; } else end=mid-1; } printf("%d/n",ans); } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&f); for(int i=1;i<=2*n;i++) p[i]=i; for(int i=1; i<=m; i++) scanf("%d%d",&e[i].x,&e[i].y); for(int i=1; i<=f; i++) { int x,y; scanf("%d%d",&x,&y); Union(x,y); } solve(); } return 0; }