C - League of Leesins

代码:

// Created by CAD on 2019/11/21.
#include <bits/stdc++.h>
using namespace std;

const int maxn=1e5+5;
int vis[maxn],bj[maxn];
vector<int> v[maxn];
int ans[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;cin>>n;
    for(int i=1;i<=n-2;++i)
    {
        int a[4];
        for(int j=1; j<=3; ++j)
            cin>>a[j],bj[a[j]]++;
        for(int j=1; j<=3; ++j)
            for(int k=1;k<=3;++k)
                if(j!=k)
                    v[a[j]].push_back(a[k]);
    }
    for(int i=1;i<=n;++i)
        if(bj[i]==1) ans[1]=i;
    ans[2]=v[ans[1]][0],ans[3]=v[ans[1]][1];
    if(bj[ans[2]]==3) swap(ans[2],ans[3]);
    cout<<ans[1]<<" "<<ans[2];
    vis[ans[1]]=vis[ans[2]]=1;
    for(int i=1;i<=n-2;++i)
    {
        for(auto k:v[ans[1]]) if(!vis[k]) ans[3]=k;
        cout<<" "<<ans[3];
        vis[ans[3]]=1;
        ans[1]=ans[2],ans[2]=ans[3];
    }
    cout<<endl;
    return 0;
}
12-27 22:03