题意就是给多个三元组(内部没有顺序),让你构造一个序列,使得所有的三元组都是存在的
简单的思考后就会发现一个简单的思路,开头的数一定只出现一次,进而可以找到头或者尾部的第一个三元组,然后我们知道序列最开始的元素是什么,但是后面两个我们并不知道,两个的顺序是什么,但是我们知道,两个相邻的元素,可以找到连与其相邻的两个元素,然后就很简单了,每次查找下一个元素,然后看前面两个元素中,与第三个元素是否是相邻的,用map瞎搞就行,但是要注意代码的交叉覆盖
#include#define LL long long #define pii pair #define rep(i,j,k) for(int i=j;i<=k;i++) #define per(i,j,k) for(int i=j;i>=k;i--) #define mp make_pair using namespace std; const int maxx =2e5+6; map int>p; map int>pp; int vis[maxx]; int ar[maxx][3]; vector<int>ans; int main(){ int n; scanf("%d",&n); p.clear(); int a,b,c; memset(vis,0,sizeof(vis)); rep(i,1,n-2){ scanf("%d%d%d",&ar[i][0],&ar[i][1],&ar[i][2]); a=ar[i][0]; b=ar[i][1]; c=ar[i][2]; if(p[mp(min(a,b),max(a,b))]==0){ p[mp(min(a,b),max(a,b))]=c; }else { pp[mp(min(a,b),max(a,b))]=c; } if (p[mp(min(a,c),max(a,c))]==0){ p[mp(min(a,c),max(a,c))]=b; }else { pp[mp(min(a,c),max(a,c))]=b; } if (p[mp(min(b,c),max(b,c))]==0){ p[mp(min(c,b),max(c,b))]=a; }else { pp[mp(min(c,b),max(c,b))]=a; } vis[a]++; vis[b]++; vis[c]++; } int st=0; for (int i=1;i<=n;i++){ if (vis[ar[i][0]]==1){ a=ar[i][1]; b=ar[i][2]; st=ar[i][0]; break; }else if (vis[ar[i][1]]==1){ a=ar[i][0]; b=ar[i][2]; st=ar[i][1]; break; }else if(vis[ar[i][2]]==1){ a=ar[i][0]; b=ar[i][1]; st=ar[i][2]; break; } } ans.push_back(st); if (pp[mp(min(a,b),max(a,b))]==st){ c=p[mp(min(a,b),max(a,b))]; }else { c=pp[mp(min(a,b),max(a,b))]; } int cc; for (int i=1;i<=n-4;i++){ if (pp[mp(min(a,c),max(a,c))]!=0){ ans.push_back(b); if (pp[mp(min(a,c),max(a,c))]==b){ cc=c; //这里要把c提出来,因为后面c已经改变的了,但是我们还是要用这个原来的值 c=p[mp(min(a,cc),max(a,cc))]; b=cc; a=a; }else { cc=c; c=pp[mp(min(a,cc),max(a,cc))]; b=cc; a=a; } }else { ans.push_back(a); if (pp[mp(min(b, c), max(b, c))]==a) { cc=c; c = p[mp(min(b, cc), max(b, cc))]; a = b; b = cc; }else { cc=c; c = pp[mp(min(b, cc), max(b, cc))]; a = b; b = cc; } } } if (p[mp(min(b,c),max(b,c))]){ ans.push_back(a); if (vis[b]==1){ ans.push_back(c); ans.push_back(b); }else { ans.push_back(b); ans.push_back(c); } }else { ans.push_back(b); if (vis[a]==1){ ans.push_back(c); ans.push_back(a); }else{ ans.push_back(a); ans.push_back(c); } } for (auto it:ans){ printf("%d ",it); } cout<<endl; return 0; } /* 5 4 1 2 4 3 2 2 3 5 * */