Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
代码如下,只要出现移动并且不成环,那空闲块每次都要重置。如果像我这样遍历每个cluster,那么每次移动时注意数组的清零// // main.cpp // 1033ex // // Created by zhangchenyi on 1/14/23. // #include <iostream> int fa,H[10001],freecl,circle; void fun(int from,int to){ if(!to) freecl=fa; else if(from==to) {} else if(to==fa){ circle=1; printf("%d %d\n",from, freecl);//1->f 2->1 3->2 4->3 5->4 f->5 H[from]=0; } else{ fun(to,H[to]); printf("%d %d\n",from,to); H[from]=0; } } int main(int argc, const char * argv[]) { int N,K,cnt=1; memset(H,0,sizeof(H)); scanf("%d%d",&N,&K); while(K--){ int Si; scanf("%d",&Si); while(Si--){ int cluster; scanf("%d",&cluster); H[cluster]=cnt++; } } for(int i=1;i<=N;i++) if(!H[i]) freecl=i; int flag=0; for(int i=1;i<=N;i++) if(H[i]&&H[i]!=i) flag=1; if(!flag) printf("No optimization needed"); for(int i=1;i<=N;i++){ if(!H[i]) continue; circle=0;fa=i; fun(i,H[i]); if(circle) printf("%d %d\n",freecl,fa); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator