Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

代码如下,只要出现移动并且不成环,那空闲块每次都要重置。如果像我这样遍历每个cluster,那么每次移动时注意数组的清零

Posted by zhangcy269 at 2023-01-14 12:43:49 on Problem 1033
//
//  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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator