Codeforces - 1399E2 Weights Division


传送门:Codeforces - 1399E2

思路分析

记录下每条边经过的次数,首先考虑贪心,每次操作肯定是选择价值最大的边,但是这样并不能保证结果最优,因为代价是不同的
记录下两种代价删除$x$次得到的价值,枚举操作几次代价为1的边,二分找到需要操作几次代价为2的边

AC代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e6+10;
#define int long long
ll s,tot;
int n,cnt,siz[N],in[N],num[N],head[N],a[N],cost[N];

struct node {
    int next,v,w,id;
} e[N];

void add(int u,int v,int w,int id) {
    e[++cnt].id=id;
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt;
}

void dfs(int u,int fa) {
    siz[u]=0;
    if(in[u]==1 && u!=1) siz[u]=1;
    for(int i=head[u]; i; i=e[i].next) {
        int v=e[i].v;
        int w=e[i].w;
        int id=e[i].id;
        if(v==fa) continue;
        dfs(v,u);
        siz[u]+=siz[v];
        num[id]+=siz[v];
    }
}

struct Node {
    int w,val,need;
    friend bool operator < (Node x,Node y) {
        return x.w<y.w;
    }
    int cal() {
        return val*need-(val/2)*need;
    }
};

void debug(vector<int> x){
    for(auto i : x) cout<<i<<" ";
    cout<<endl;
}

signed main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    int T;
    read(T);
    while(T--) {
        read(n,s);
        for(int i=1; i<=n; i++) head[i]=0,in[i]=0,num[i]=0;
        cnt=1;
        for(int i=1; i<n; i++) {
            int u,v,w,cc;
            read(u,v,w,cc);
            in[u]++;
            in[v]++;
            cost[i]=cc;
            add(u,v,w,i);
            add(v,u,w,i);
            a[i]=w;
        }
        dfs(1,1);
        tot=0;
        for(int i=1; i<n; i++) tot+=a[i]*num[i];
        int ans=INF;
        ll sum=tot-s;

        auto solve = [&](int id){
            priority_queue<Node>q;
            vector<int>tmp;
            ll res = 0;
            for(int i=1;i<n;i++){
                if(cost[i]==id){
                    q.push({(a[i]-a[i]/2)*num[i],a[i],num[i]});
                }
            }
            tmp.pb(res);
            while(!q.empty()){
                Node now = q.top();
                q.pop();
                if(now.w==0) break;
                res+=now.w;
                tmp.pb(res);
                now.val/=2;
                now.w=now.cal();
                q.push(now);
            }
            return tmp;
        };
        
        vector<int>one,two;
        one=solve(1);
        two=solve(2);
        for(int i=0;i<sz(one);i++){
            int pos = lower_bound(all(two),sum-one[i]) - two.begin();
            if(pos==(int)sz(two)) continue;
            if(one[i]+two[pos]>=sum) ans=min(ans,i+pos*2);    
        }
        printf("%lld\n",ans);
    }




    return 0;
}


文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}