先找到一条多边形的边,然后循环一圈输出多边形上的点。把每个三角形看作一个结点,以两个三角形之间公用边为边建立一张图,DFS输出叶子结点,则得到先切后切的顺序。

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
vector<int>v[];
bool vis[];
void dfs(int x){
vis[x]=;
for(auto it:v[x])
if(!vis[it])
dfs(it);
cout<<x<<" ";//把三角形看作是一个结点,输出叶子结点即为外圈的三角形,它们是可以被先切的,内圈的三角形后输出为后切的
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=;i<=n;++i){
vis[i]=;
v[i].clear();
}
int adj[n+]={};//每次和三角形中另外两个点异或,最终得到的是它相邻的两个点的异或和
map<pair<int,int>,vector<int> >mp;
for(int i=;i<=n-;++i){
int a,b,c;
cin>>a>>b>>c;
adj[a]^=b;
adj[a]^=c;
adj[b]^=a;
adj[b]^=c;
adj[c]^=a;
adj[c]^=b;
if(a>b)
swap(a,b);
if(b>c)
swap(b,c);
if(a>b)
swap(a,b);//为了使abc有序,否则可能因为输入顺序的原因把同对的两个点顺序搞反
mp[{a,b}].push_back(i);
mp[{b,c}].push_back(i);
mp[{a,c}].push_back(i);
}
int x=,y=;
for(auto it:mp){
if(it.second.size()==){//只属于一个三角形的边一定是原本多边形的一条边
x=it.first.first;//x和y一定相邻
y=it.first.second;
break;
}
}
cout<<x<<" "<<y<<" ";
for(int i=;i<=n-;++i){//已知x是y的一个相邻点,又知道y的两个相邻点的异或和adj[y],可以得到另一个相邻点x^adj[y]
adj[y]^=x;
cout<<adj[y]<<" ";
x=y;
y=adj[y];
}
cout<<"\n";
//x=0,y=0;
for(auto it:mp){
if(it.second.size()==){//某条边被两个三角形所共用,就将这两个三角形之间连一条边
x=it.second[];
y=it.second[];
v[x].push_back(y);
v[y].push_back(x);
}
}
dfs();
cout<<"\n";
}
return ;
}

Codeforces Round #610 (Div. 2)E(模拟,DFS)的更多相关文章

  1. Codeforces Round #610 (Div. 2) A-E简要题解

    contest链接: https://codeforces.com/contest/1282 A. Temporarily unavailable 题意: 给一个区间L,R通有网络,有个点x,在x+r ...

  2. Codeforces Round #222 (Div. 1) A. Maze dfs

    A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...

  3. Codeforces Round #245 (Div. 2) C. Xor-tree DFS

    C. Xor-tree Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...

  4. Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈

    D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  5. Codeforces Round #398 (Div. 2) C. Garland —— DFS

    题目链接:http://codeforces.com/contest/767/problem/C 题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串.问怎样剪才能使三串葡萄的质量相等. 首先要做 ...

  6. Codeforces Round #249 (Div. 2) (模拟)

    C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. Codeforces Round #321 (Div. 2)C(tree dfs)

    题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...

  8. Codeforces Round #366 (Div. 2) C 模拟queue

    C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  9. Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. SpringData JPA快速入门和基本的CRUD操作以及Specifications条件查询

    SpringData JPA概述: SpringData JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和操作 ...

  2. Codeforces 383C Propagating tree, 线段树, 黑白染色思想

    按深度染色,奇深度的点存反权值. #include <bits/stdc++.h> using namespace std; vector <]; ],a[],s[],vis[],i ...

  3. yii2表单提交CSRF验证

    Yii2表单提交默认需要验证CSRF,如果CSRF验证不通过,则表单提交失败,解决方法如下: 第一种解决办法是关闭Csrf public $enableCsrfValidation = false; ...

  4. ubuntu Redis安装及配置

    1.安装 1.1 下载压缩包:wget http://download.redis.io/releases/redis-5.0.4.tar.gz1.2 解压:tar xzf redis-5.0.4.t ...

  5. ApiBehaviorOptions 统一模型验证配置不生效

    ApiBehaviorOptions 的统一模型验证配置一定要放到(.AddMvc)后面.

  6. LightOJ-1370 Bi-shoe and Phi-shoe (欧拉函数+二分)

    Problem Description Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe ...

  7. ACWing 238 银河英雄传说

    最近带权并查集这块比较薄弱,直接看食物链看不懂,就老实一步步来了. 有一个划分为N列的星际战场,各列依次编号为1,2,…,N. 有N艘战舰,也依次编号为1,2,…,N,其中第i号战舰处于第i列. 有T ...

  8. 【Unity|C#】基础篇(5)——分部类与分部函数(partial)

    [学习资料] <C#图解教程>(第6章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.c ...

  9. C语言sprintf函数的深入理解

    由于sprintf跟printf在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出.这也导致sprintf比printf有用得多.所以本文着重介绍sprintf, ...

  10. Python2安装MySQLdb

    在http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python下载对应的包版本,如果是win7 64位2.7版本的python,就下载 MySQL_p ...