POJ 1033 模拟

来源:互联网 发布:linux搭建git服务器 编辑:程序博客网 时间:2024/05/18 00:23
//11158197c00h00g1033Accepted672K454MSG++1947B2013-01-04 22:40:37//几十天没看,回过头来,终于想通了//我没有处理环的情况//假设有这样一组数据,1->2,2->3,3->4,4->1,排列应该为 4 1 2 3(输入),我们的做法应该是将1234放入栈中,然后出栈将//4放到从后找出来的一块空的区域,剩下的123组成了链,只需要将3->4,2->3,1->2就可以了 #include<cstdio>#include<cstdlib>#include<stack>#include<string.h>using namespace std;stack<int> st;//p[i]表示放在i位置上的数据应该放在哪个位置 int p[10005];int N,K,n;int i,j; int main(){    while(scanf("%d%d",&N,&K)!=EOF){        int num=1,pos;        memset(p,-1,sizeof(p));        while(K--){            scanf("%d",&n);            for(i=0;i<n;i++){                scanf("%d",&pos);                p[pos]=num++;            }        }                int start;        bool flag=false;        for(i=1;i<=N;i++){            if(p[i]==i||p[i]==-1)                continue;            flag=true;            start=j=i;            while(p[j]!=start&&p[j]!=-1){                st.push(j);                j=p[j];            }            if(p[j]==-1){                int begin;                int to=j;                while(!st.empty()){                    begin=st.top();st.pop();                    printf("%d %d\n",begin,to);                    p[to]=to;                    to=begin;                         }                p[begin]=-1;            }else if(p[j]==start){                st.push(j);                int begin,to;                for(int k=N;k>=1;k--){                    if(p[k]==-1){                        to=k;                        break;                    }                }                int count=1;                while(!st.empty()){                    begin=st.top();st.pop();                    printf("%d %d\n",begin,to);                    if(count==1){                        p[to]=start;                    }else{                        p[to]=to;                    }                    to=begin;                    count++;                }                p[begin]=-1;            }        }        if(flag==false)            printf("No optimization needed\n");    }                return 0;}

原创粉丝点击