\quad 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!
\quad 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 p i p_{i} pi-th position (1≤ p i p_{i} pi≤n, all p i p_{i} piare unique). In other words, p p_{} p is a permutation of 1,2,…,n.
\quad 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 q i q_{i} qi=( p i p_{i} pi, p i + 1 p_{i+1} pi+1, p i + 2 p_{i+2} pi+2) for each 1≤i≤n−2. Bob was very proud of his array, so he showed it to his friend Alice.
\quad 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.
\quad For example, if n=5 and p = [ 1 , 4 , 2 , 3 , 5 ] p=\left[ 1,4,2,3,5\right] p=[1,4,2,3,5], then the original array q will be [ ( 1 , 4 , 2 ) , ( 4 , 2 , 3 ) , ( 2 , 3 , 5 ) ] \left[\left(1,4,2\right),\left(4,2,3\right),\left(2,3,5\right)\right] [(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 ) ] \left[\left(4,3,2\right),\left(2,3,5\right),\left(4,1,2\right)\right] [(4,3,2),(2,3,5),(4,1,2)]Note that [ ( 1 , 4 , 2 ) , ( 4 , 2 , 3 ) , ( 3 , 3 , 5 ) ] \left[\left(1,4,2\right),\left(4,2,3\right),\left(3,3,5\right)\right] [(1,4,2),(4,2,3),(3,3,5)]is not a valid rearrangement of q, as Bob is not allowed to swap numbers belong to different triples.
\quad 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.
\quad The first line contains a single integer n ( 5 ≤ n ≤ 5≤n≤ 5≤n≤ 1 0 5 10^{5} 105) — the size of permutation p.
\quad The i − t h i-th i−th of the next n−2 lines contains 3 integers q i , 1 q_{i,1} qi,1, q i , 2 q_{i,2} qi,2, q i , 3 q_{i,3} qi,3 ( 1 ≤ 1≤ 1≤ q i , j q_{i,j} qi,j ≤ n ≤n ≤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.
\quad It is guaranteed that there is at least one permutation p that is consistent with the input.
\quad Print n distinct integers p 1 p_{1} p1, p 2 p_{2} p2,…, p n p_{n} pn ( 1 ≤ p i ≤ n 1≤p_{i}≤n 1≤pi≤n) such that p is consistent with array q q q.
If there are multiple answers, print any.
5
4 3 2
2 3 5
4 1 2
1 4 2 3 5
题意: 给出一个排列,每连续3个数取一次,并且3个数的顺序都可以改变和每3个数的排列顺序。
比如说原始n个数为 1 2 3 4 5,然后就有3个数组
1 2 3
2 3 4
3 4 5
把这三个数组内部可以随便排序,
2 3 1
4 2 3
5 4 3
然后要你求任意一种原来的n个数。
思路就是 更新每次起点,和起点有关系的点肯定只有2个,确认起点后把和起点有关的点的集合里删除起点,然后重新找起点。正解应该是拓扑排序的怎么是我不会。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const int maxn = 3e5 + 5;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret*10 + ch - '0';
ch = getchar();
}
return ret*sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
int qpow(int m, int k, int mod)
{
int res = 1, t = m;
while (k)
{
if (k&1)
res = res * t % mod;
t = t * t % mod;
k >>= 1;
}
return res;
}
ll gcd(ll a,ll b)
{
return b==0?a : gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
return a*b/gcd(a,b);
}
const int N = 1e5 + 10;
set<int>res[N];
int vis[N];
struct node
{
int a,b,c;
} a[N];
int ans[N];
set<int>mo;
int n,tot;
int st,ed;
int flag;
void dfs(int now, int cnt)
{
if(cnt==n-1)
return ;
set<int>::iterator it;
for(it=res[now].begin() ; it!=res[now].end() ; it++)
{
if(vis[*it]!=2)
res[*it].erase(now);
}
for(it=res[now].begin(); it!=res[now].end(); it++)
{
if(res[*it].size()==2&&!vis[*it])
{
ans[++tot]=*it;
vis[*it]=1;
dfs(*it,cnt+1);
}
}
}
int main()
{
tot=0;
scanf("%d",&n);
for(int i=1; i<=n-2; i++)
{
int a, b, c;
scanf("%d %d %d",&a,&b,&c);
res[a].insert(b),res[a].insert(c);
res[b].insert(a),res[b].insert(c);
res[c].insert(a),res[c].insert(b);
}
flag=0;
st=0,ed=0;
for(int i=1; i<=n; i++)
if(res[i].size()==2&&!flag)
st=i,flag=1;
else if(res[i].size()==2)
{
ed=i;
break;
}
ans[++tot]=st;
vis[st]=1;
set<int>::iterator it;
for(it=res[ed].begin() ; it!=res[ed].end() ; it++)
vis[*it]=2;
dfs(st,1);
for(it=res[ed].begin() ; it!=res[ed].end() ; it++)
{
if(vis[*it]==2&&res[*it].size()==4)
{
ans[++tot]=*it;
}
}
for(it=res[ed].begin() ; it!=res[ed].end(); it++)
{
if(vis[*it]==2 && res[*it].size()==3)
{
ans[++tot]=*it;
ans[++tot]=ed;
break;
}
}
for(int i=1; i<=tot; i++)
{
printf("%d ",ans[i]);
}
return 0;
}
自己写的太复杂了,看到一个简单的代码但是复杂度比DFS高一点。
#include
#define ll long long
using namespace std;
const int MAXN=1e5+10;
vector<int>v[MAXN];
map<int,int>mp;
int vis[MAXN];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n-2;i++)
{
int x,y,z;
cin>>x>>y>>z;
v[x].push_back(y);
v[x].push_back(z);
v[y].push_back(x);
v[y].push_back(z);
v[z].push_back(y);
v[z].push_back(x);
mp[x]++,mp[y]++,mp[z]++;
}
int x,y;
for(int i=1;i<=n;i++)
{
if(mp[i]==1)
{
x=i;
break;
}
}
if(mp[v[x][0]]==2)
{
y=v[x][0];
}
else
y=v[x][1];
vis[x]=vis[y]=1;
cout<<x<<" "<<y<<" ";
for(int i=1;i<=n-2;i++)
{
int z;
for(int j=0;j<v[x].size();j++)
{
if(!vis[v[x][j]])
{
z=v[x][j];
break;
}
}
vis[z]=1;
cout<<z<<" ";
x=y,y=z;
}
cout<<endl;
return 0;
}