HDU 5695 Gym Class

33 篇文章 0 订阅
Problem Description
众所周知,度度熊喜欢各类体育活动。

今天,它终于当上了梦寐以求的体育课老师。第一次课上,它发现一个有趣的事情。在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到 N ,在排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数。麻烦的是,有一些同学不希望某个(些)同学排在他(她)前面,在满足这个前提的情况下,新晋体育课老师——度度熊,希望最后的排队结果可以使得所有同学的评价分数和最大。
 

Input
第一行一个整数 T ,表示 T(1T30)  组数据。

对于每组数据,第一行输入两个整数 N M(1N100000,0M100000) ,分别表示总人数和某些同学的偏好。

接下来 M 行,每行两个整数 A  和 B(1A,BN) ,表示ID为 A 的同学不希望ID为 B 的同学排在他(她)之前。你可以认为题目保证至少有一种排列方法是符合所有要求的。
 

Output
对于每组数据,输出最大分数 。
 

Sample Input
  
  
3 1 0 2 1 1 2 3 1 3 1
 

Sample Output
  
  
1 2

6

拓扑排序,把最终的队列模拟出来就行了。

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<bitset>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
int T, cas = 1;
int n, ct[maxn], m, x, y;
int ft[maxn], nt[maxn], u[maxn], sz;
int res;
LL ans;

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        sz = 0;
        for (int i = 1; i <= n; i++) ct[i] = 0, ft[i] = -1;
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d", &x, &y);
            u[sz] = y;    nt[sz] = ft[x]; ft[x] = sz++; ct[y]++;
        }
        ans = 0;    res = n;
        priority_queue<int> p;
        for (int i = 1; i <= n; i++) if (!ct[i]) p.push(i);
        while (!p.empty())
        {
            int q = p.top();    p.pop();
            res = min(res, q);    ans += res;
            for (int i = ft[q]; i != -1; i = nt[i])
            {
                if (!(--ct[u[i]])) p.push(u[i]);
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值