給出一個(gè)n個(gè)數(shù)的序列1,然后有m個(gè)改動(dòng)(a, b),在序列2中a跟b在序列中的相對(duì)順序改變。求符合題意的序列2。
題中說(shuō)道如果一個(gè)數(shù)的位置不確定,則輸出‘?' ,仔細(xì)想想,這種情況是不會(huì)存在的,因?yàn)樵诮o定的序列1中,所有數(shù)都會(huì)有相對(duì)順序,因此無(wú)論怎么修改數(shù)對(duì)的相對(duì)順序,結(jié)果總是確定的。
?
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<bitset>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
#define eps 1e-10
using namespace std;
const int maxn = 555;
int T, n, m, rank1[maxn], rank2[maxn], pos[maxn], in[maxn], g[maxn][maxn];
bool topo()
{
int tot = 0;
queue<int> q;
REP(i, n) if(in[i] == 0) q.push(i);
while(!q.empty())
{
int u = q.front(); q.pop();
rank2[tot++] = u;
REP(v, n) if(g[u][v])
{
in[v]--;
if(in[v] == 0) q.push(v);
}
}
return tot == n;
}
int main()
{
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
int a, b;
REP(i, n)
{
scanf("%d", &rank1[i]);rank1[i]--;
pos[rank1[i]] = i;
}
CLR(g, 0); CLR(in, 0);
REP(i, n) FF(j, i+1, n) g[rank1[i]][rank1[j]] = 1, in[rank1[j]]++;
scanf("%d", &m);
while(m--)
{
scanf("%d%d", &a, &b);a--;b--;
swap(g[a][b], g[b][a]);
if(pos[a] < pos[b]) in[a]++, in[b]--;
else in[a]--, in[b]++;
}
if(topo()) REP(i, n) printf("%d%c", rank2[i]+1, i == n-1 ? '\n' : ' ');
else puts("IMPOSSIBLE");
}
return 0;
}
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

