PAT (Advanced Level) 1097. Deduplication on a Linked List (25)

简单题。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

const int maxn=100000+10;
int root,n;
struct Node
{
    int id,val,nx;
}tmp[maxn],s[maxn];

int in[maxn];
int flag[maxn];
int idx[maxn];
int tot=0;
vector<int>g[maxn];

int main()
{
    memset(in,0,sizeof in);
    memset(flag,0,sizeof flag);
    scanf("%d%d",&root,&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&tmp[i].id,&tmp[i].val,&tmp[i].nx);
        in[tmp[i].id]=i;
    }

    int p=root,sz=0;
    while(1)
    {
        if(p==-1) break;
        s[sz++]=tmp[in[p]];
        p=tmp[in[p]].nx;
    }

    for(int i=0;i<sz;i++)
    {
        if(flag[abs(s[i].val)]==0) g[0].push_back(i);
        else g[1].push_back(i);
        flag[abs(s[i].val)]++;
    }

    for(int i=0;i<=1;i++)
    {
        for(int j=0;j<g[i].size();j++)
        {
            if(j==g[i].size()-1)
                printf("%05d %d %d\n",s[g[i][j]].id,s[g[i][j]].val,-1);
            else
                printf("%05d %d %05d\n",s[g[i][j]].id,s[g[i][j]].val,s[g[i][j+1]].id);
        }
    }
    return 0;
}

 

posted @ 2016-07-04 22:44  Fighting_Heart  阅读(136)  评论(0编辑  收藏  举报