[Codeforces Round #601 (Div. 2)]C. League of Leesins(构造技巧)

找到一个出现一次的作起点,找到一个出现两次并且和起点在同一3元组的为第二个点。
把3元化为3个两元关系
g[x][i] = y说明x,y相距<=2.
把找到的点x都标记上v[x]=-1.
对于以找到的最后两点s1,s2中,要找s3,s3是唯一一个没有标记而且和s1距离<=2的点,(g[s1][i]中没有标记的)

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> g[100100];
int v[100100];

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n-2; i++) {
		int x, y, z;
		//scanf("%d%d%d", &x, &y, &z);
		cin >> x >> y >> z;
		v[x]++; v[y]++; v[z]++;
		g[x].push_back(y);
		g[x].push_back(z);
		g[y].push_back(x);
		g[y].push_back(z);
		g[z].push_back(x);
		g[z].push_back(y);
	}
	int s1 = 0, s2 = 0;
	for (int i = 1; i <= n; i++)
		if (v[i] == 1) s1 = i;
	for (int i = 1; i <= n; i++)
		if (v[i] == 2) {
			bool ok = 0;
			for (int to : g[s1])
				if (to == i) ok = 1;
			if (ok) s2 = i;
		}

	v[s1] = v[s2] = -1;
	printf("%d %d", s1, s2);
	for (int i = 3; i <= n; i++) {
		int nt = 0;
		for (int to : g[s1]) {
			if (v[to] != -1)
				nt = to;
		}
		s1 = s2; s2 = nt;
		v[nt] = -1;
		printf(" %d", nt);
	}
	return 0;
}
 

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) teams around the world. Before the tournament starts, Bob has made a prediction of the rankings of each team, from 1-st to n-th. After the final, he compared the prediction with the actual result and found out that the i-th team according to his prediction ended up at the pi-th position (1≤pi≤n, all pi are unique). In other words, p is a permutation of 1,2,…,n.
As Bob’s favorite League player is the famous “3ga”, he decided to write down every 3 consecutive elements of the permutation p. Formally, Bob created an array q of n−2 triples, where qi=(pi,pi+1,pi+2) for each 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 even if Bob rearranges the elements of 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 and p=[1,4,2,3,5], then the original array q will be [(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)]. Note that [(1,4,2),(4,2,2),(3,3,5)] is not a valid rearrangement of 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 that is consistent with the array q she was given.
Input
The first line contains a single integer n (5≤n≤105) — the size of permutation p.
The i-th of the next n−2 lines contains 3 integers qi,1, qi,2, qi,3 (1≤qi,j≤n) — the elements of the i-th triple of the rearranged (shuffled) array qi, 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 that is consistent with the input.
Output
Print n distinct integers p1,p2,…,pn (1≤pi≤n) such that p is consistent with array q.
If there are multiple answers, print any.

猜你喜欢

转载自blog.csdn.net/qq_33831360/article/details/103170975