2019 CCPC Harbin E :Exchanging Gifts 拓扑排序

传送门

分析

大致思路就是求出序列中出现次数最多的数
但是这么序列太长了,最长可以到达 1 e 18 1e^{18} 1e18,怎么去处理呢
我们把每个序列看成一个点,然后根据每个序列之间的关系建图,可以证明这是一个有向无环图,并且可以通过拓扑排序求出来每一个叶子结点被调用的次数
但是因为这道题时限卡的比较紧,所以我们需要把有用的节点取出来建图,并且要用比较优秀的快读才能卡过去

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10,M = N * 2;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
VI nums[N];
int n;
int in[N];
ll cnt[N];
map<int,ll> res;
bool st[N];
int h[N],e[M],ne[M],idx;

void add(int x,int y){
    ne[idx] = h[x],e[idx] = y,h[x] = idx++;
}

void init(){
    for(int i = 1;i <= n;i++) h[i] = -1,in[i] = 0,st[i] = false,cnt[i] = 0,nums[i].clear();
    idx = 0;
    res.clear();
}

void build(){
    queue<int> q;
    q.push(n);
    st[n] = 1;
    while(q.size()){
        int t = q.front();
        q.pop();
        for(int i = h[t];~i;i = ne[i]){
            int j = e[i];
            in[j]++;
            if(!st[j]) q.push(j),st[j] = 1;
        }
    }
}

void bfs(){
    queue<int> q;
    q.push(n);
    cnt[n] = 1;
    while(q.size()){
        int t = q.front();
        q.pop();
        if(h[t] == -1){
            for(int x:nums[t]) res[x] += 1ll * cnt[t];
        }
        else{
            for(int i = h[t];~i;i = ne[i]){
                int j = e[i];
                in[j]--;
                cnt[j] += cnt[t];
                if(!in[j]) q.push(j);
            }
        }
    }
}

int main() {
    int T;
    read(T);
    while(T--){
        read(n);
        init();
        for(int i = 1;i <= n;i++){
            int op;
            read(op);
            if(op == 1){
                int k;
                read(k);
                while(k--){
                    int x;
                    read(x);
                    nums[i].pb(x);
                }
            }
            else{
                int l,r;
                read(l),read(r);
                add(i,l),add(i,r);
            }
        }
        build();
        bfs();
        ll sum = 0,ans = 0;
        for(auto t:res){
            sum += t.se;
            ans = max(ans,t.se);
        }
        printf("%lld\n", ans > sum / 2 ? 2 * sum - 2 * ans : sum);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值