hdu 5242 (优先队列贪心+瞎搞)

hdu 5242 (优先队列贪心+瞎搞)_第1张图片

#include 

using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int t,n,m,fa[maxn];
ll val[maxn];
seta[maxn];
void DFS(int u,int father)
{
    set::iterator it;
    int sum=a[u].size(),tm;
    for(it=a[u].begin();it!=a[u].end();it++)
    {
        int v=*it;
        if(v==father)
        {
            tm=v;
            continue;
        }
        fa[v]=u;
        if((sum==2&&u!=1)||(sum==1&&u==1))
        {
            val[v]+=val[u];
            val[u]=0;
           // fa[v]=fa[u];
        }
        DFS(v,u);
    }
    a[u].erase(tm);
}
struct node{
    int x;
    ll val;
    node(int _x,ll _val)
    {
        x=_x;
        val=_val;
    }
    bool operator < (const node &a)const
    {
        return val>a.val;
    }
};
void solve(int x)
{
    int y=x;
    x=fa[x];
    val[y]=0;
    while(a[x].size()==1)
    {
        val[x]=0;
        y=x;
        x=fa[x];
    }

    a[x].erase(y);
    val[y]=0;
    while(a[x].size()==1)
    {
        set::iterator it=a[x].begin();
        int v=*it;
        val[v]+=val[x];
        val[x]=0;
        x=v;
    }
}
priority_queueq;
int main()
{
    //cout << "Hello world!" << endl;
    //freopen("in.txt","r",stdin);
    scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            a[i].clear();
            scanf("%lld",&val[i]);
        }
        for(int i=1;i0)
        {
            node fuck=q.top();
            q.pop();
            int u=fuck.x;
            if(val[u]!=fuck.val)
            {
                fuck.val=val[u];
                q.push(fuck);
                continue;
            }
            m--;
            solve(u);
        }
        ll ans=0;
        for(int i=1;i<=n;i++)
        ans+=val[i];
        printf("Case #%d: %lld\n",++cas,ans);
    }
    return 0;
}

 

你可能感兴趣的:(贪心)