C. League of Leesins

#include <iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;


const int maxn=100010;
vector<int>g[maxn];
int con[maxn];
int vis[maxn];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n-2;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        g[x].push_back(y),g[x].push_back(z);
        g[y].push_back(x),g[y].push_back(z);
        g[z].push_back(y),g[z].push_back(x);
        con[x]++,con[y]++,con[z]++;
    }
    int x,y;
    for(int i=1;i<=n;i++)
    {
        if(con[i]==1)
        {
            x=i;
            break;
        }
    }
    if(con[g[x][0]]==2) y=g[x][0];
    else y=g[x][1];
    cout<<x<<" "<<y<<" ";
    vis[x]=1,vis[y]=1;
    int k=2;
    while(k<n)
    {
        int ans;
        for(auto u:g[x])
        {
            if(!vis[u]) ans=u;
        }
        cout<<ans<<" ";
        vis[ans]=1;
        x=y;
        y=ans;
        k++;
    }
    cout<<endl;
    return 0;
}

 

猜你喜欢

转载自www.cnblogs.com/AAAzhuo/p/11930629.html