確定比賽名詞 pro
blem
desc
ript
io
n\colorproblem description
proble
mdes
crip
tion
有n
nn個比賽隊(
1<=n
<
=500
)(1<=n<=500)
(1<=n
<=5
00),編號依次為1,2
,3...
n1,2,3...n
1,2,3.
..n,進行比賽,比賽結束後,裁判委員會要將所有參賽隊伍從前往後依次排名,但現在裁判委員會不能直接獲得每個隊的比賽成績,只知道每場比賽的結果,即p1贏p2,用p1,p2表示,排名時p1在p2之前。現在請你程式設計序確定排名。
i np
ut
\colorinput
inpu
t輸入有若干組,每組中的第一行為二個數n(1
<=n
<
=500),
mn(1<=n<=500),m
n(1<=n
<=5
00),
m;其中n表示隊伍的個數,m
mm表示接著有m行的輸入資料。接下來的m行資料中,每行也有兩個整數p1,p2表示即p1隊贏了p2隊。
o ut
pu
t\coloroutput
output
給出乙個符合要求的排名。輸出時隊伍號之間有空格,最後一名後面沒有空格。
其他說明:符合條件的排名可能不是唯一的,此時要求輸出時編號小的隊伍在前;輸入資料保證是正確的,即輸入資料確保一定能有乙個符合要求的排名。
s am
plei
nput
\colorsample input
sample
inpu
t
4 3
1 22 3
4 3
sam
pleo
utpu
t\colorsample output
sample
outp
ut
1 2 4 3
如果a
aa戰勝b
bb,我們可以假設有一條邊從a
aa連向b
bb,然後我們設定乙個陣列r[]
rr[
],表示戰敗的次數,那麼r[b]++
。之後就進行拓撲排序輸出就行了,要注意輸出時編號小的在前面,那麼就可以借助優先佇列來完成。
#include
using
namespace std;
using ll =
long
long
;const
int n =
1e5+10;
const
int inf =
0x3f3f3f3f
;const
double eps =
1e-7
;#define endl '\n'
#define pb(a) push_back(a)
#define all(x) x.begin(), x.end()
#define size(x) int(x.size())
#define ios ios::sync_with_stdio(0);
int r[
510]
;vector<
int> g[
510]
;bool vis[
510]
;int
main()
//拓撲排序模板
priority_queue<
int, vector<
int>
, greater<
int>> q;
for(
int i =
1; i <= n;
++i)if(
!r[i]
) q.
push
(i);
//先把可以第一名的入列
vector<
int> ans;
while
(!q.
empty()
)}}for
(int i =
0; i < n -1;
++i)
printf
("%d "
, ans[i]);
printf
("%d\n"
, ans[n -1]
);}return0;
}
確定比賽名次 典型的拓撲排序 優先佇列
有n個比賽隊 1 n 500 編號依次為1,2,3,n進行比賽,比賽結束後,裁判委員會要將所有參賽隊伍從前往後依次排名,但現在裁判委員會不能直接獲得每個隊的比賽成績,只知道每場比賽的結果,即p1贏p2,用p1,p2表示,排名時p1在p2之前。現在請你程式設計序確定排名。input 輸入有若干組,每組...
拓撲排序 確定比賽名次
題目 description 有n個比賽隊 1 n 500 編號依次為1,2,3,n進行比賽,比賽結束後,裁判委員會要將所有參賽隊伍從前往後依次排名,但現在裁判委員會不能直接獲得每個隊的比賽成績,只知道每場比賽的結果,即p1贏p2,用p1,p2表示,排名時p1在p2之前。現在請你程式設計序確定排名。...
確定比賽名次 (拓撲排序)
有n個比賽隊 1 n 500 編號依次為1,2,3,n進行比賽,比賽結束後,裁判委員會要將所有參賽隊伍從前往後依次排名,但現在裁判委員會不能直接獲得每個隊的比賽成績,只知道每場比賽的結果,即p1贏p2,用p1,p2表示,排名時p1在p2之前。現在請你程式設計序確定排名。input 輸入有若干組,每組...