NWERC 2018——B.Brexit Negotiations

Brexit Negotiations
有向无环图,很容易想到拓扑排序,很明显我们需要把权值大的点放在最前面,然后就想到搞个优先队列,每次拓扑排序的时候出权值大的点,但是发现答案是不对的。
正向建图拓扑排序能够保证小的点一定最后取到,而如果反向建图每次取最小的可以保证最大的点最后被取到,显然我们需要的是最大的点放在最前面因此需要方向建图。

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int N=400010;
int h[N],e[N],ne[N],idx;
int d[N];
int w[N];
int n,res;
void add(int a,int b)
{
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}
void topsort()
{
    priority_queue<pii,vector<pii>,greater<pii> >q;
    for(int i=1;i<=n;i++)
        if(!d[i]) q.push({w[i],i});
    int tt=n;
    while(q.size())
    {
        int t=q.top().second;
        q.pop();
        tt--;
        res=max(res,w[t]+tt);
        
        for(int i=h[t];i!=-1;i=ne[i])
        {
            int j=e[i];
            if(--d[j]==0) q.push({w[j],j});
        }
    }
}
int main()
{
    //IO;
    int T=1;
    //cin>>T;
    for(int ca=1;ca<=T;ca++)
    {
        cin>>n;
        memset(h,-1,sizeof h);
        for(int i=1;i<=n;i++)
        {
            int cnt;
            cin>>w[i]>>cnt;
            while(cnt--)
            {
                int a;
                cin>>a;
                add(i,a);
                d[a]++;
            }
        }
        topsort();
        cout<<res<<'\n';
    }
    return 0;
}

总结:虽然有时候很难理性的分析出结果,但是对于贪心需要大胆的取猜结论多尝试?做了很长时间没有结果如果当时多试试可能就能碰出答案了,虽然很玄hhh

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值