码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4857 逃生

时间:2016-07-15 13:12:52      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

逃生

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3780    Accepted Submission(s): 1082


Problem Description
糟糕的事情发生啦,现在大家都忙着逃命。但是逃命的通道很窄,大家只能排成一行。

现在有n个人,从1标号到n。同时有一些奇怪的约束条件,每个都形如:a必须在b之前。
同时,社会是不平等的,这些人有的穷有的富。1号最富,2号第二富,以此类推。有钱人就贿赂负责人,所以他们有一些好处。

负责人现在可以安排大家排队的顺序,由于收了好处,所以他要让1号尽量靠前,如果此时还有多种情况,就再让2号尽量靠前,如果还有多种情况,就让3号尽量靠前,以此类推。

那么你就要安排大家的顺序。我们保证一定有解。
 

 

Input
第一行一个整数T(1 <= T <= 5),表示测试数据的个数。
然后对于每个测试数据,第一行有两个整数n(1 <= n <= 30000)和m(1 <= m <= 100000),分别表示人数和约束的个数。

然后m行,每行两个整数a和b,表示有一个约束a号必须在b号之前。a和b必然不同。
 

 

Output
对每个测试数据,输出一行排队的顺序,用空格隔开。
 

 

Sample Input
1 5 10 3 5 1 4 2 5 1 2 3 4 1 4 2 3 1 5 3 5 1 2
 

 

Sample Output
1 2 3 4 5
 

 

Author
CLJ
 

 

Source
 
1
3 1
3 1
结果应该是  3 1 2
 
 
我们应该考虑吧大的放在后面。而不是把小的放在前面  因为小的放在前面的话比如  3->1  2  我们会有  2  3 1 这个序列
 
如果反向建边  1->3  2  并且用大根堆 会有   2 1 3这个结果逆序输出就是正确结果
/* ***********************************************
Author        :guanjun
Created Time  :2016/7/15 9:48:04
File Name     :hdu4857.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 30010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
vector<int>v[maxn];
vector<int>ans;
int in[maxn],n,m;
void topsort(){
    priority_queue<int>q;
    for(int i=1;i<=n;i++){
        if(in[i]==0)q.push(i);
    }
    int num=0;
    while(!q.empty()){
        int u=q.top();q.pop();
        ans.push_back(u);
        for(int i=0;i<v[u].size();i++){
            int x=v[u][i];
            in[x]--;
            if(in[x]==0){
                q.push(x);
            }
        }
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int t,x,y;
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i=1;i<=n;i++)v[i].clear();
        cle(in);
        ans.clear();
        for(int i=1;i<=m;i++){
            scanf("%d%d",&x,&y);
            v[y].push_back(x);
            in[x]++;
        }
        topsort();
        for(int i=ans.size()-1;i>=0;i--){
            printf("%d%c",ans[i],i==0?10: );
        }
    }
    return 0;
}

 

HDU 4857 逃生

标签:

原文地址:http://www.cnblogs.com/pk28/p/5673172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!