Codeforces Round #610 (Div. 2)E(模拟,DFS)

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

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 vector<int>v[100007];
 5 bool vis[100007];
 6 void dfs(int x){
 7     vis[x]=1;
 8     for(auto it:v[x])
 9         if(!vis[it])
10             dfs(it);
11     cout<<x<<" ";//把三角形看作是一个结点,输出叶子结点即为外圈的三角形,它们是可以被先切的,内圈的三角形后输出为后切的
12 }
13 int main(){
14     ios::sync_with_stdio(false);
15     cin.tie(NULL);
16     cout.tie(NULL);
17     int t;
18     cin>>t;
19     while(t--){
20         int n;
21         cin>>n;
22         for(int i=1;i<=n;++i){
23             vis[i]=0;
24             v[i].clear();
25         }
26         int adj[n+1]={0};//每次和三角形中另外两个点异或,最终得到的是它相邻的两个点的异或和
27         map<pair<int,int>,vector<int> >mp;
28         for(int i=1;i<=n-2;++i){
29             int a,b,c;
30             cin>>a>>b>>c;
31             adj[a]^=b;
32             adj[a]^=c;
33             adj[b]^=a;
34             adj[b]^=c;
35             adj[c]^=a;
36             adj[c]^=b;
37             if(a>b)
38                 swap(a,b);
39             if(b>c)
40                 swap(b,c);
41             if(a>b)
42                 swap(a,b);//为了使abc有序,否则可能因为输入顺序的原因把同对的两个点顺序搞反
43             mp[{a,b}].push_back(i);
44             mp[{b,c}].push_back(i);
45             mp[{a,c}].push_back(i);
46         }
47         int x=0,y=0;
48         for(auto it:mp){
49             if(it.second.size()==1){//只属于一个三角形的边一定是原本多边形的一条边
50                 x=it.first.first;//x和y一定相邻
51                 y=it.first.second;
52                 break;
53             }
54         }
55         cout<<x<<" "<<y<<" ";
56         for(int i=1;i<=n-2;++i){//已知x是y的一个相邻点,又知道y的两个相邻点的异或和adj[y],可以得到另一个相邻点x^adj[y]
57             adj[y]^=x;
58             cout<<adj[y]<<" ";
59             x=y;
60             y=adj[y];
61         }
62         cout<<"\n";
63         //x=0,y=0;
64         for(auto it:mp){
65             if(it.second.size()==2){//某条边被两个三角形所共用,就将这两个三角形之间连一条边
66                 x=it.second[0];
67                 y=it.second[1];
68                 v[x].push_back(y);
69                 v[y].push_back(x);
70             }
71         }
72         dfs(1);
73         cout<<"\n";
74     }
75     return 0;
76 }

原文地址:https://www.cnblogs.com/ldudxy/p/12182509.html

时间: 2024-08-01 15:46:24

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

Codeforces Round #610 (Div. 2) a/b/c

题目 传送门 A Temporarily unavailable   standard input/output 1 s, 256 MB  给一个线段ab, 问除去 c点圆心半径r覆盖的 线段多长,如果圆在线段外 直接输出 ab 长度就行, 若在线段内 则输出cout << max(b-a-max((min(c+r,b)-max(a,c-r)),0), 0) ,迷糊的话纸上画下大概就能明白.B1 K for the Price of One (Easy Version) , B2 K for

570D Codeforces Round #316 (Div. 2) D(dfs序,时间戳,二分

题目:一棵树上每个节点有个字符值,询问每个节点的深度为h的子节点的字符是否能组成一个回文串. 思路:首先是奇妙的dfs序和时间戳,通过记录每个节点的dfs进出时间,可以发现某个节点的子节点的进出时间均在该节点的进出时间范围内(这是很直观的dfs的性质),这样可以把树形结构转变为线性结构,方便进行各种处理.dfs一遍处理时间戳,每个节点的深度,并记录每个深度的节点都有哪些,此时每个深度的节点就是排好序的.然后对于一个询问,可以利用二分查找确定子节点在该层的哪一段.对于每一层,预先处理每个字符的前缀

题解——Codeforces Round #508 (Div. 2) T1 (模拟)

依照题意暴力模拟即可A掉 #include <cstdio> #include <algorithm> #include <cstring> #include <set> using namespace std; char s[100100]; int n,k,barrel[30]; int main(){ scanf("%d %d",&n,&k); scanf("%s",s+1); for(int i

Codeforces Round #306 (Div. 2)——C模拟——Divisibility by Eight

You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes. Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit

B. Nirvana Codeforces Round #549 (Div. 2) (递归dfs)

---恢复内容开始--- Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater value of the product makes the nirvana deeper. Help Kurt find the maximum possible product of digits among all integers from 1 to n. Input

Codeforces Round #610 (Div. 2) 题解

Temporarily unavailable K for the Price of One (Hard Version) Petya and Exam Temporarily unavailable \[ Time Limit: 1 s\quad Memory Limit: 256 MB \] 直接计算出 \([c-r, c+r]\) 在 \([a, b]\) 中的范围有多大,然后减掉就可以了. view #include <map> #include <set> #includ

Codeforces Round #610 (Div. 2)

比赛链接 A 求两条线段交的长度. \({\frak{code:}}\) #include<bits/stdc++.h> #define IL inline #define LL long long using namespace std; const int N=3e3+5,p=998244353; int a,b,c,r; IL int in(){ char c;int f=1; while((c=getchar())<'0'||c>'9') if(c=='-') f=-1;

Codeforces Round #457 (Div. 2)

Codeforces Round #457 (Div. 2) A.模拟. B.模拟.比赛的时候这题出锅了,然后就unrated了.注意要先保证最大值最小,再保证字典序最大. C.构造.挑一个大于N的质数然后连啊连就可以了. D.可持久化Trie.弄两个Trie.一个记录字符串,另一个记录数字. 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 stru

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to