题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285点击打开链接
确定比赛名次
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27765 Accepted Submission(s): 11075
其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。
4 3 1 2 2 3 4 3
1 2 4 3
感觉这道题题目有点说不清楚 他需要每次判断完之后就重新选择最小的数 同一个排名全部判断完才选择小的数
用一个邻接表记录每个数接下来的数就行
然后判断那些入度为0的点
注意可能有重复边
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
int n=0;int m=0;
int outedge[555];
vector<int > adjtable[555];
vector<int > ::iterator it;
vector<int > ans;
int main()
{
while(cin >> n >> m)
{
memset(outedge,0,sizeof(outedge));
for(int i=0;i<=500;i++)
adjtable[i].clear();
ans.clear();
for(int i=0;i<m;i++)
{
int x,y;
cin >> x >> y;
vector<int > ::iterator its;
if((adjtable[x].end()==find(adjtable[x].begin(),adjtable[x].end(),y)))
{
outedge[y]++;
adjtable[x].push_back(y);
}
}
for(int i=1;i<=n;i++)
sort(adjtable[i].begin(),adjtable[i].end());
while(1)
{
int flag=1;
for(int i=1;i<=n;i++)
if(outedge[i]==0)
flag=0;
if(!flag)
{
for(int i=1;i<=n;i++)
{
if(outedge[i]==0)
{
ans.push_back(i);
for(it=adjtable[i].begin();it!=adjtable[i].end();it++)
outedge[(*it)]--;
outedge[i]--;
break;
}
}
}
else
break;
}
for(it=ans.begin();it!=ans.end()-1;it++)
cout << (*it) << " ";
cout << (*(ans.end()-1))<< endl;
}
}
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task isonly possible if other tasks have already been executed.InputThe input will consist of several instances of the problem. Each instance begins with a line containingtwo integers, 1 ≤ n ≤ 100 and m. n is the number of tasks (numbered from 1 to n) and m is thenumber of direct precedence relations between tasks. After this, there will be m lines with two integersi and j, representing the fact that task i must be executed before task j.An instance with n = m = 0 will finish the input.OutputFor each instance, print a line with n integers representing the tasks in a possible order of execution.Sample Input5 41 22 31 31 50 0Sample Output1 4 2 5 3
最烦uva。。丑的半死。。
代码上面的就能过 而且不需要判断那些苛刻条件