C. League of Leesins(Codeforces Round #601 (Div. 2))(模拟+思维)

C. League of Leesins(Codeforces Round #601 (Div. 2))(模拟+思维)

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Description

Bob is an avid fan of the video game “League of Leesins”, and today he celebrates as the League of Leesins World Championship comes to an end!

The tournament consisted of n ( n 5 ) n (n≥5) teams around the world. Before the tournament starts, Bob has made a prediction of the rankings of each team, from 1 1 -st to n n -th. After the final, he compared the prediction with the actual result and found out that the i i -th team according to his prediction ended up at the p i p_i -th position ( 1 p i n (1≤p_i≤n , all p i p_i are unique). In other words, p p is a permutation of 1 , 2 , , n 1,2,…,n .

As Bob’s favorite League player is the famous "3ga", he decided to write down every 3consecutive elements of the permutation p p . Formally, Bob created an array q q of n 2 n−2 triples, where q i = ( p i , p i + 1 , p i + 2 ) q_i=(p_i,p_i+1,p_i+2) for each 1 i n 2 1≤i≤n−2 . Bob was very proud of his array, so he showed it to his friend Alice.

After learning of Bob’s array, Alice declared that she could retrieve the permutation p p even if Bob rearranges the elements of q q and the elements within each triple. Of course, Bob did not believe in such magic, so he did just the same as above to see Alice’s respond.

For example, if n = 5 n=5 and p = [ 1 , 4 , 2 , 3 , 5 ] p=[1,4,2,3,5] , then the original array q q will be [ ( 1 , 4 , 2 ) , ( 4 , 2 , 3 ) , ( 2 , 3 , 5 ) ] [(1,4,2),(4,2,3),(2,3,5)] . Bob can then rearrange the numbers within each triple and the positions of the triples to get [ ( 4 , 3 , 2 ) , ( 2 , 3 , 5 ) , ( 4 , 1 , 2 ) ] [(4,3,2),(2,3,5),(4,1,2)] . Note that [ ( 1 , 4 , 2 ) , ( 4 , 2 , 2 ) , ( 3 , 3 , 5 ) ] [(1,4,2),(4,2,2),(3,3,5)] is not a valid rearrangement of q q , as Bob is not allowed to swap numbers belong to different triples.

As Alice’s friend, you know for sure that Alice was just trying to show off, so you decided to save her some face by giving her any permutation p p that is consistent with the array q q she was given.

Input

The first line contains a single integer n ( 5 n 1 0 5 ) n (5≤n≤10^5) — the size of permutation p p .

The i i -th of the next n 2 n−2 lines contains 3 3 integers q i , 1 , q i , 2 , q i , 3 ( 1 q i , j n ) q_{i,1}, q_{i,2}, q_{i,3} (1≤q_{i,j}≤n) — the elements of the i-th triple of the rearranged (shuffled) array q i q_i , in random order. Remember, that the numbers within each triple can be rearranged and also the positions of the triples can be rearranged.

It is guaranteed that there is at least one permutation p p that is consistent with the input.

Output

Print n distinct integers p 1 , p 2 , , p n ( 1 p i n ) p_1,p_2,…,p_n (1≤p_i≤n) such that p p is consistent with array q q .

If there are multiple answers, print any.

input

5
4 3 2
2 3 5
4 1 2

output

1 4 2 3 5 

题解

给你 n 2 n-2 个长度为 3 3 的数字对,它们是一个序列的相邻元素,让你找出符合全部数字对的序列

因为给出的数字对无序,序列也无序,所以先以每个数字为数组头存下对应的数字对的另外两个元素建立一个数组,那么长度为 1 1 的数组对应的头就可以作为序列的第一个元素(事实上这种元素能且只能找到两个,分别作为序列的第一个和最后一个元素)。

之后就可以确定序列的第二个和第三个元素

之后就能确定所有的元素。

代码

#include <iostream>
#include <algorithm>
#include <vector>
#define m_p make_pair
#define _for(i, a) for(int i = 0; i < (a); ++i)
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
using namespace std;

int n;
vector< vector< pair<int, int> > > a;	//存储以数字i+1开头的数字对的序列
vector<int> ans;	//存储答案序列
vector<bool> b;	//记录数字b[i]是否被用过

void sol() {
	ans.clear();
	b.resize(n); fill(b.begin(), b.end(), 0);
	a.resize(n);
	_for(i, n - 2) {
		int x[3];
		_for(j, 3) cin >> x[j], x[j]--;
		a[x[0]].push_back(m_p(x[1], x[2]));
		a[x[1]].push_back(m_p(x[0], x[2]));
		a[x[2]].push_back(m_p(x[0], x[1]));
	}
	_for(i, n) {
		if (a[i].size() == 1) {	//第一个数字可以确定
			ans.push_back(i);
			if (a[a[i][0].first].size() != 2) swap(a[i][0].first, a[i][0].second);	//第一个数字确定之后第二个和第三个也就可以确定了
			ans.push_back(a[i][0].first);
			ans.push_back(a[i][0].second);
			break;
		}
	}
	//_for(i, ans.size()) cout << ans[i] + 1 << " ";
	for (int i : ans) b[i] = 1;
	_rep(i, 1, n - 2) {
		for (pair<int, int> j : a[ans[i]]) {
			if (b[j.first] && b[j.second]) continue;	//代表这个序列正是包含自己的前一个序列,因为数字不可能重复,所以除去这种就只有答案数字了,自己画图模拟一下即可
			if (b[j.first]) {
				ans.push_back(j.second);
				b[j.second] = 1;
				break;
			}
			if (b[j.second]) {
				ans.push_back(j.first);
				b[j.first] = 1;
				break;
			}
		}
	}
	for (int i : ans) cout << i + 1 << " ";
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);

	cin >> n;
	sol();
	return 0;
}
发布了163 篇原创文章 · 获赞 54 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_42856843/article/details/103215471