League of Lessins

Bob预测了League of Leesins世界锦标赛的排名,并创建了一个基于三连元素的数组q。他的朋友Alice声称能从重新排列的q中恢复原始排名。给定重新排列的q,输出符合的任意排列。
摘要由CSDN通过智能技术生成

Bob is an avid fan of the video game “League of Leesins”, and today he celebrates as the League of Leesins World Championship comes to an end!

The tournament consisted of n (n≥5) teams around the world. Before the tournament starts, Bob has made a prediction of the rankings of each team, from 1-st to n-th. After the final, he compared the prediction with the actual result and found out that the i-th team according to his prediction ended up at the pi-th position (1≤pi≤n, all pi are unique). In other words, p is a permutation of 1,2,…,n.

As Bob’s favorite League player is the famous “3ga”, he decided to write down every 3 consecutive elements of the permutation p. Formally, Bob created an array q of n−2 triples, where qi=(pi,pi+1,pi+2) for each 1≤i≤n−2. Bob was very proud of his array, so he showed it to his friend Alice.

After learning of Bob’s array, Alice declared that she could retrieve the permutation p even if Bob rearranges the elements of q and the elements within each triple. Of course, Bob did not believe in such magic, so he did just the same as above to see Alice’s respond.

For example, if n=5 and p=[1,4,2,3,5], then the original array q will be [(1,4,2),(4,2,3),(2,3,5)]. Bob can then rearrange the numbers within each triple and the positions of the triples to get [(4,3,2),(2,3,5),(4,1,2)]. Note that [(1,4,2),(4,2,2),(3,3,5)] is not a valid rearrangement of q, as Bob is not allowed to swap numbers belong to different triples.

As Alice’s friend, you know for sure that Alice was just trying to show off, so you decided to save her some face by giving her any permutation p that is consistent with the array q she was given.

Input
The first line contains a single integer n (5≤n≤105) — the size of permutation p.

The i-th of the next n−2 lines contains 3 integers qi,1, qi,2, qi,3 (1≤qi,j≤n) — the elements of the i-th triple of the rearranged (shuffled) array qi, in random order. Remember, that the numbers within each triple can be rearranged and also the positions of the triples can be rearranged.

It is guaranteed that there is at least one permutation p that is consistent with the input.

Output
Print n distinct integers p1,p2,…,pn (1≤pi≤n) such that p is consistent with array q.

If there are multiple answers, print any.

Example
Input
5
4 3 2
2 3 5
4 1 2
Output
1 4 2 3 5
这题即给出n-2个排列的每3个连续排列打乱后的3个数字,求出这个排列即可。
根据题意:可知开头的数字和结尾的数字必然在输入矩阵中只会出现一次,而与之同组的数字只会出现2次或3次。其他元素均出现3次,那么可轻易得到第一组数。接着把第一组数中除开开头的元素,剩下的两个数字是第二组中的两个。比如将{1,2,3,4,5}排列得到三组{1,2,3},{2,3,4},{3,4,5}。其中2,3会出现在第一组和第二组,第一组最先能求出来,那么在另一组数中找到下一个数,以此类推。

#include <bits/stdc++.h>
using namespace std;
int num[100010][5];//记录每组数据
vector<int>vec[100010];//记录数字i出现过的组数
int ans[100010];//记录结果
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n-2;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        vec[a].push_back(i);vec[b].push_back(i);vec[c].push_back(i);
        num[i][1]=a;num[i][2]=b;num[i][3]=c;
    }
    int a=0,b=0,c=0;
    int last=0;//记录b和c出现过的组数
    for(int i=1;i<=n;i++){//找开头的3个数(b和c的位置不重要)
        if(vec[i].size()==1){
            int temp=vec[i][0];
            last=temp;	
            a=i;
            for(int j=1;j<=3;j++){
                if(vec[num[temp][j]].size()==2)b=num[temp][j];
                else if(vec[num[temp][j]].size()==3)c=num[temp][j];
            }
        }
        if(a)break;
    }
    int cnt=0;
    ans[++cnt]=a;ans[++cnt]=b;ans[++cnt]=c;
    while(cnt!=n){
        int temp=0;//记录组数
        for(int i=1;i<=vec[b].size();i++){//找b和c相同的组数
            for(int j=1;j<=vec[c].size();j++){
                if(vec[b][i-1]==vec[c][j-1]&&vec[b][i-1]!=last){
                    temp=vec[b][i-1]; 
                    last=temp;
                    a=b;
                    b=c;
                    break;
                }
            }
            if(temp)break;
        }
        for(int i=1;i<=3;i++){
            if(num[temp][i]!=a&&num[temp][i]!=b){
                c=num[temp][i];
                break;
            }
        }
        ans[++cnt]=c;
    }
    for(int i=1;i<=n;i++){
        printf("%d ",ans[i]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值