TZOJ 3389: Rankings
3389: Rankings 
总提交: 32 测试通过:16
描述
There are n teams (labelled from 1 to n) who take part in a programming competition every year, and at the end they are ranked in order of merit. The rankings for last year are known. This year, the jury wants to make the event less competitive, and decides not to publish such a ranking list (since teams near the bottom might get disheartened). Instead, they will produce a complete list of pairs of teams whose relative rank order has changed from last year to this year. For example, if team 13 placed above team 6 last year, but team 6 placed above team 13 this year, the pair (6, 13) is announced. This would enable teams to track their progress against a particular opposing team, but not give them a clear sense of where they stand overall.
Of course, this isn't going to stop your team from trying to determine the overall ranking list. Given last year's rankings and a complete list of the pairs of teams whose relative rank order has changed, reconstruct as much of this year's standings as possible. It is possible that the jury might have made an error, so if the data given is inconsistent with any possible ranking list for this year, you should also detect this.输入
On the first line a positive integer: the number of test cases, at most 100. After that per test case:
One line with an integer n (2 ≤ n ≤ 500): the number of teams.
One line with n integers ti (1 ≤ ti ≤ n): the rankings for last year, from best team to worst team. ti represents the team who came in position i (1-indexed) on the ranklist. All the ti will be distinct.
One line with an integer m (0 ≤ m ≤ 25 000): the number of pairs whose relative rank order has changed.
m lines with two integers ai and bi (1 ≤ ai < bi ≤ n) each: a pair of teams whose relative rank order has changed. Each such pair will be mentioned exactly once.
输出
Per test case:
One line with n integers: the rankings for this year, from best to worst, where the i-th term (1-indexed) represents the team in position i. If this team cannot be determined with certainty, the integer should be replaced with a `?' character. If the data for a particular test case is inconsistent with any possible ranking list for this year, the line must contain "IMPOSSIBLE" instead.
样例输入
3
5
5 4 3 2 1
2
2 4
3 4
3
2 3 1
0
4
1 2 3 4
3
1 2
3 4
2 3
样例输出
5 3 2 4 1
2 3 1
IMPOSSIBLE
题意:有n个队伍,给定其排名,再给定t个关系x和y,如果x队的排名高于y队,则变为y队的排名高于x队 ,如果y队的排名高于x队,则变为x队的排名高于y队。
求新的排名序列。若无法实现排名则输出”IMPOSSIBLE“,若能实现排名则输出排名训练,对于不明确的排名则用 "?" 替代;
思路:根据队伍排名关系建图,并进行拓扑排序.
#include<bits/stdc++.h> using namespace std; int f[505][505],rk[505],in[505],n; void topo(){ int num=0; queue<int> q; for(int i=1;i<=n;i++){ if(in[i]==0)q.push(i); } vector<int> v; while(!q.empty()){ int u=q.front(); q.pop(); for(int i=1;i<=n;i++){ if(f[u][i]==1){ in[i]--; if(in[i]==0)q.push(i); } } num++; v.push_back(u); } int flag=0; if(num==n){ for(int i=0;i<v.size();i++){ int x=v[i]; int sum=0; for(int j=1;j<=n;j++){ if(f[j][x]==1)sum++; } if(flag)printf(" "); if(sum==i)printf("%d",x); else printf("?"); flag=1; } printf("\n"); } else printf("IMPOSSIBLE\n"); } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&rk[i]); memset(f,0,sizeof(f)); memset(in,0,sizeof(in)); for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ f[rk[i]][rk[j]]=1; } } int k; scanf("%d",&k); for(int i=1;i<=k;i++){ int x,y; scanf("%d%d",&x,&y); if(f[x][y]==1)f[x][y]=0,f[y][x]=1; else if(f[y][x]==1)f[x][y]=1,f[y][x]=0; } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(f[i][j]==1) in[j]++; } } topo(); } }
浙公网安备 33010602011771号