hdu 1258 确定比赛名次
生活随笔
收集整理的這篇文章主要介紹了
hdu 1258 确定比赛名次
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有N個比賽隊(1<=N<=500),編號依次為1,2,3,。。。。,N進行比賽,比賽結束后,裁判委員會要將所有參賽隊伍從前往后依次排名,但現在裁判委員會不能直接獲得每個隊的比賽成績,只知道每場比賽的結果,即P1贏P2,用P1,P2表示,排名時P1在P2之前。現在請你編程序確定排名。?
Input輸入有若干組,每組中的第一行為二個數N(1<=N<=500),M;其中N表示隊伍的個數,M表示接著有M行的輸入數據。接下來的M行數據中,每行也有兩個整數P1,P2表示即P1隊贏了P2隊。?
Output給出一個符合要求的排名。輸出時隊伍號之間有空格,最后一名后面沒有空格。?
其他說明:符合條件的排名可能不是唯一的,此時要求輸出時編號小的隊伍在前;輸入數據保證是正確的,即輸入數據確保一定能有一個符合要求的排名。?
Sample Input 4 3 1 2 2 3 4 3Sample Output 1 2 4 3鏈式向前星存儲結構: #include<iostream> #include<algorithm> #include <math.h> #define maxn 600 #include <queue> #include <set> #include <cstring> #include <cstdio> using namespace std; struct node {int to,w,next; }edge[maxn]; int first[maxn][2]; int n,m; int index; void add(int a,int b) {edge[index].to=b;edge[index].next=first[a][1];first[a][1]=index++;first[b][0]++;//rudu } int main() {int a,b;while(cin>>n>>m){index=1;set<int>Q;set<int>::iterator it=Q.begin();vector<int>v;memset(first,0,sizeof(first));memset(edge,0,sizeof(edge));for(int i=0;i<m;++i){scanf("%d%d",&a,&b);add(a,b);}/* for(int i=1;i<=n;++i){cout<<i<<' '<<first[i][0];for(int j=first[i][1];j!=0;j=edge[j].next)cout<<' '<<edge[j].to;cout<<endl;}*/for(int i=1;i<=n;++i){if(first[i][0]==0){Q.insert(i);//break;}}int flag=0;while(Q.size()){it=Q.begin();if(flag==0){cout<<*it;flag=1;}else{cout<<' '<<*it;}Q.erase(Q.begin());for(int i=first[*it][1];i;i=edge[i].next){first[edge[i].to][0]--;if(first[edge[i].to][0]==0){Q.insert(edge[i].to);}}}cout<<endl;}return 0; }
Input輸入有若干組,每組中的第一行為二個數N(1<=N<=500),M;其中N表示隊伍的個數,M表示接著有M行的輸入數據。接下來的M行數據中,每行也有兩個整數P1,P2表示即P1隊贏了P2隊。?
Output給出一個符合要求的排名。輸出時隊伍號之間有空格,最后一名后面沒有空格。?
其他說明:符合條件的排名可能不是唯一的,此時要求輸出時編號小的隊伍在前;輸入數據保證是正確的,即輸入數據確保一定能有一個符合要求的排名。?
Sample Input 4 3 1 2 2 3 4 3Sample Output 1 2 4 3鏈式向前星存儲結構: #include<iostream> #include<algorithm> #include <math.h> #define maxn 600 #include <queue> #include <set> #include <cstring> #include <cstdio> using namespace std; struct node {int to,w,next; }edge[maxn]; int first[maxn][2]; int n,m; int index; void add(int a,int b) {edge[index].to=b;edge[index].next=first[a][1];first[a][1]=index++;first[b][0]++;//rudu } int main() {int a,b;while(cin>>n>>m){index=1;set<int>Q;set<int>::iterator it=Q.begin();vector<int>v;memset(first,0,sizeof(first));memset(edge,0,sizeof(edge));for(int i=0;i<m;++i){scanf("%d%d",&a,&b);add(a,b);}/* for(int i=1;i<=n;++i){cout<<i<<' '<<first[i][0];for(int j=first[i][1];j!=0;j=edge[j].next)cout<<' '<<edge[j].to;cout<<endl;}*/for(int i=1;i<=n;++i){if(first[i][0]==0){Q.insert(i);//break;}}int flag=0;while(Q.size()){it=Q.begin();if(flag==0){cout<<*it;flag=1;}else{cout<<' '<<*it;}Q.erase(Q.begin());for(int i=first[*it][1];i;i=edge[i].next){first[edge[i].to][0]--;if(first[edge[i].to][0]==0){Q.insert(edge[i].to);}}}cout<<endl;}return 0; }
總結
以上是生活随笔為你收集整理的hdu 1258 确定比赛名次的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 831D O
- 下一篇: poj 1860 拓扑。。