The 2019 China Collegiate Programming Contest Harbin Site E Exchanging Gifts 快读模板

#include
using namespace std;
#define ll long long
const int N=1e6+10;
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++;
}
template <typename _Tp> inline void read(_Tp&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 t,n,vis[N];
ll in[N],cnt[N];
vector<int> v[N],g[N];
map<ll,ll> mp[N];
unordered_map<ll,ll> mp2;
void init(){
    mp2.clear();
    for(int i=1;i<=n;i++){
        in[i]=0;
        cnt[i]=0;
        vis[i]=0;
        g[i].clear();
        v[i].clear();
        mp[i].clear();
    }
}
void pre(){
    queue<int> q;
    q.push(n);
    vis[n]=1;
    while(q.size()){
        int tp=q.front();
        q.pop();
        int sz=v[tp].size();
        for(int i=0;i<sz;i++){
            int to=v[tp][i];
            in[to]++;
            if(vis[to]==0){
                q.push(to);
                vis[to]=1;
            }
        }
    }
}
void bfs(){
    cnt[n]=1;
    queue<int> q;
    q.push(n);
    while(q.size()){
        int tp=q.front();q.pop();
        int sz=v[tp].size();
        for(int i=0;i<sz;i++){
            int to=v[tp][i];
            in[to]--;
            cnt[to]+=cnt[tp];
            if(in[to]==0) q.push(to);
        }
    }
    ll sum=0,maxi=0;
    for(int i=1;i<=n;i++){
        if(v[i].size()==0&&cnt[i]){
            for(auto it:g[i]){
                mp2[it]+=cnt[i];
            }
        }
    }
    for(auto it:mp2){
        sum+=it.second;
        maxi=max(maxi,it.second);
    }
    if(maxi*2>=sum){
        printf("%lld\n",(ll)2*(sum-maxi));
//        cout<<<
    }else{
//        cout<
        printf("%lld\n",sum);
    }
}
int main(){
    read(t);
    while(t--){
        read(n);
        init();
        for(int i=1;i<=n;i++){
            int op;
            read(op);
            if(op==1){
                int x,y;read(x);
                for(int j=1;j<=x;j++) {
                    read(y);
                    g[i].emplace_back(y);
                }
            }else{
                int x,y;read(x);read(y);
                v[i].emplace_back(x);v[i].emplace_back(y);
            }
        }
        pre();
        bfs();
    }
}

你可能感兴趣的:(acm,c++,开发语言,后端)