题意

https://vjudge.net/problem/CodeForces-1255C

一个长度为n的序列,给你n-2个三元组,比如p=[1,4,2,3,5],那么三元组为[1,4,2],[4,2,3],[2,3,5],其中每个三元组内的元素可以交换位置,整个三元组也可以和别的三元组整体交换位置,但不能交换不同三元组的数。求这个序列。

思路

记录每个数在所有三元组出现的次数、和每个数连了哪些数(和哪些数在一个三元组里),其中出现次数为1的肯定是序列的头或尾,因为序列可以反转(得到同样的三元组),所以随便选一个出现次数为1的数当头,然后这个数连的点中出现次数为2的就是第二个数。后面的数可以通过判断前面两个数连的数是否有相同,如果有,那么这个数就是第三个数,以此类推。

代码

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int cnt[N],ans[N],vis[N];
vector<int> v[N];
int main()
{
std::ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=1;i<=n-2;i++)
{
int a,b,c;
cin>>a>>b>>c;
cnt[a]++,cnt[b]++,cnt[c]++;
v[a].push_back(b),v[a].push_back(c);
v[b].push_back(a),v[b].push_back(c);
v[c].push_back(a),v[c].push_back(b);
}
for(int i=1;i<=n;i++)
{
if(cnt[i]==1)
{
int flag=0;
for(int j:v[i])
{
if(cnt[j]==2)
{
ans[1]=i,ans[2]=j,vis[i]=vis[j]=1;
flag=1;
break;
}
}
if(flag)
break;
}
}
for(int i=3;i<=n;i++)
{
int f=0;
for(int j:v[ans[i-1]])
{
for(int k:v[ans[i-2]])
{
if(j==k&&!vis[j])
{
ans[i]=j,vis[j]=1;
f=1;
break;
}
}
if(f)
break;
}
}
for(int i=1;i<=n;i++)
{
cout<<ans[i]<<" ";
}
cout<<endl;
return 0;
}

  

CodeForces - 1255C(构造+模拟)的更多相关文章

  1. Codeforces 174B【模拟构造】

    题意: 给你一个串只有小写字母和点,让你构造前缀是1-8,后缀是1-3长度的文件名: 思路: 那么以"."作为分割点,把字符串都拿出来,然后 首段长度<=8 OK; 中间&l ...

  2. CodeForces - 1255D (模拟+构造+贪心)

    题意 https://vjudge.net/problem/CodeForces-1255D rxc的农场里'R'表示有米,现在有K只鸡,给这k只鸡选一些格子,每个鸡可以有多个格子(每个鸡至少吃一个米 ...

  3. Codeforces 389B(十字模拟)

    Fox and Cross Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submi ...

  4. codeforces 591B Rebranding (模拟)

    Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...

  5. Codeforces 626B Cards(模拟+规律)

    B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...

  6. CodeForces - 950C Zebras 模拟变脑洞的天秀代码

    题意:给你一个01串,问其是否能拆成若干形如0101010的子串,若能,输出所有子串的0,1 的位置. 题解:一开是暴力,然后瞎找规律, 最后找到一种神奇的线性构造法:扫一遍字符串,若为0就一直竖着往 ...

  7. Codeforces 631C. Report 模拟

    C. Report time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  8. Codeforces 679B. Barnicle 模拟

    B. Barnicle time limit per test: 1 second memory limit per test :256 megabytes input: standard input ...

  9. CodeForces 382C【模拟】

    活生生打成了大模拟... #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef unsig ...

随机推荐

  1. UIView设置阴影

    UI设计师有时候希望我们的产品比较酷. 阴影是他们喜欢的效果之一. 怎么设置阴影呢? 1.设置一个四边都相同的阴影 UIImageView *testImgView = [[UIImageView a ...

  2. alluxio 安装记录及相关信息

    最近要尝试探究一下alluxio相关的知识,本博客进行对alluxio的安装过程进行备忘: 单例安装过程: https://docs.alluxio.io/os/user/stable/cn/cont ...

  3. Octave中的常用操作2

    >> ones(2:3)ans = 1 1 1 1 1 1 >> 2*ones(2:3)ans = 2 2 2 2 2 2 >> rand(3,3) 产生0~1中的 ...

  4. faster-rcnn错误信息 : tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [21] rhs shape= [2]

    faster-rcnn错误信息 : tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shap ...

  5. Theory of Storage

    https://www.intelligenteconomist.com/theory-of-storage/ The Theory of Storage describes features obs ...

  6. vscode 笔记

    设置中文 查看 --> 命令面板 --> 输入: change display language , 安装 中文, 重启 vscode . markdown 转 pdf 安装 Markdo ...

  7. 1+x 证书 Web 前端开发中级理论考试(试卷 6 )

    1+x 证书 Web 前端开发中级理论考试(试卷 6 ) 官方QQ群 1+x 证书 web 前端开发初级对应课程分析 http://blog.zh66.club/index.php/archives/ ...

  8. SQLServer临时库文件太大,迁移tempdb数据库

    问题描述: 最近公司这边tempdb库文件很大,几百GB的节奏 不过安装数据库的时候,tempdb最好不要放在C盘是放在D盘其他数据盘的 如果没有放在其他盘符下面,就需要做迁移了 解决方法: 如果te ...

  9. MongoDB for OPS 02:复制集 RS 配置

    写在前面的话 对于生产环境而言,除非是非常不重要的业务,且该业务允许我们出现一定时间的停机,我们一般才会使用单节点,且该单节点必须要有完善的备份手段. RS 复制集 我们这里采取一主两从的方式搭建复制 ...

  10. C# copy files from source directory to destination file and rename repeated files and does not override

    static void CopyFiles() { string sourceDir = @"D:\C\ll"; string destDir = @"D:\LL&quo ...