先找到一条多边形的边,然后循环一圈输出多边形上的点。把每个三角形看作一个结点,以两个三角形之间公用边为边建立一张图,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. TODO:rest和restful接口是什么?

    todo: 参考: http://www.ruanyifeng.com/blog/2018/10/restful-api-best-practices.html https://www.jianshu ...

  2. 温故知新的错题训练:Coin game

    传送门:http://192.168.173.163/JudgeOnline/problem.php?cid=1244&pid=1 输赢规则:无法再放下硬币的人就输. 博弈论的基本假定:他俩都 ...

  3. 算法竞赛入门经典第二版 竖式问题 P42

    #include<bits/stdc++.h> using namespace std; int inset(char *s,int num) { //判断数字是否在数字集中 int le ...

  4. shiro认证和授权

    一.shiro基础概念 Authentication:身份认证 / 登录,验证用户是不是拥有相应的身份: Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限:即判断用户 ...

  5. 【C语言】输入一个整数x并判断x是否存在于数组a中

    #include<stdio.h> int main() { ] = { ,,,,,,,, };//数组初始化 printf("请输入要查找的数据:\n"); scan ...

  6. 数据库ETL同步 cdc开启,Git同步url添加用户名密码

    添加双主键 alter table TChargeRecordMainNew add constraint pk_name primary key (任务编码,序号) ---------------- ...

  7. git 解决每次更新代码都要输入用户名密码

    git config --global credential.helper store git pull /git push (第一次输入,后续就不用再次数据)

  8. 终极教程【zhong】

    just for a better future! 资源教程               aiim                   综合类 前端知识体系 前端知识结构 Web前端开发大系概览 We ...

  9. hdu1716 排列2

    12  21 123 132  213 231 321 312 .... 每次都将后面n-1位进行全排列.递归的出口当起始坐标等于终止坐标时.需要还原. 设计标记数组.因为需要从小到大输出. #def ...

  10. Java.util.Calendar类

    Java.util.Calendar类 package myProject; import java.text.SimpleDateFormat; import java.util.Calendar; ...