CF741B Arpa's weak amphitheater and Mehrdad's valuable Hoses disjoint-set 01 backpack

title

CF741B

Simplify the meaning of the questions:

There \ (n-\) Personal \ ((. 1 <n-<= 1000) \ =) , each having a weight \ (w_i (1 \ leqslant w_i \ leqslant 1000) \) and a charm value \ (b_i (1 \ b_i leqslant \ leqslant 10 ^ 6) \) .

\ (n-\) there between individuals \ ({n-1) n (} {2}, 10 ^ 5)) \ m (1 \ leqslant m \ leqslant min (\ frac) a relationship. The first \ (i \) a relationship between two numbers \ (x_i \) and \ (y_i \) form, that the first \ (x_i \) Individuals and \ (y_i \) personal friends, friend relationship is bidirectional.

Known if \ (a \) and \ (b \) is a friend, \ (b \) and \ (c \) is a friend, then \ (a \) and \ (c \) is a friend. Now \ (Mehrdad \) to invite some people came to the party, so that the total weight of these people does not exceed \ (w_i (1 \ leqslant w_i \ leqslant 1000) \) , and Charisma sum as large as possible. With a circle of friends who can only invite one of them, or all of the person, or a person not invited.

analysis

  1. Who determined in the same group, so think of disjoint-set. Then use \ (vector \) containers will be stored together in the same group of people.
  2. According to the above requirements for each container of people either take all, or only a take, or not take, then this is actually a simple \ (01 \) knapsack problem.
  3. So this question is solved, mention IQ!

code

#include<bits/stdc++.h>

#define file(s) freopen(s".in","r",stdin), freopen(s".out","w",stdout)

#define G ch=getchar()
#define DeBug(x) std::cout<<#x<<':'<<x<<std::endl

const int MaxN=1e3+10;

namespace IO
{
    char buf[1<<15],*fs,*ft;
    inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; }
    template<typename T>inline void read(T &x)
    {
        x=0;
        T f=1, G;
        while (!isdigit(ch) && ch^'-') G;
        if (ch=='-') f=-1, G;
        while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), G;
        x*=f;
    }

    char Out[1<<24],*fe=Out;
    inline void flush() { fwrite(Out,1,fe-Out,stdout); fe=Out; }
    template<typename T>inline void write(T x,char str)
    {
        if (!x) *fe++=48;
        if (x<0) *fe++='-', x=-x;
        T num=0, ch[20];
        while (x) ch[++num]=x%10+48, x/=10;
        while (num) *fe++=ch[num--];
        *fe++=str;
    }
}

using IO::read;
using IO::write;

template<typename T>inline bool chkMin(T &a,const T &b) { return a>b ? (a=b, true) : false; }
template<typename T>inline bool chkMax(T &a,const T &b) { return a<b ? (a=b, true) : false; }
template<typename T>inline T min(T a,T b) { return a<b ? a : b; }
template<typename T>inline T max(T a,T b) { return a>b ? a : b; }

namespace Union_Set
{
    int fa[MaxN];
    inline int get(int x)
    {
        return fa[x]==x ? x : fa[x]=get(fa[x]);
    }

    inline void merge(int x, int y)
    {
        int fx=get(x), fy=get(y);
        if (fx^fy) fa[fx]=fy;
    }
}

using Union_Set::get;
using Union_Set::merge;

int w[MaxN], b[MaxN], f[MaxN];
std::vector<int> g[MaxN];
int main()
{
    int n, m, W;
    read(n), read(m), read(W);//总重
    for (int i=1; i<=n; ++i) read(w[i]);//重量
    for (int i=1; i<=n; ++i) read(b[i]);//魅力值

    for (int i=1; i<=n; ++i) Union_Set::fa[i]=i;
    for (int i=1, x, y; i<=m; ++i) read(x), read(y), merge(x, y);

    for (int i=0; i<MaxN; ++i) g[i].clear();
    for (int i=1; i<=n; ++i) g[get(i)].push_back(i);

    memset(f, 0, sizeof(f));
    for (int i=1; i<=n; ++i)
    {
        if (get(i)^i) continue;//一个人也不邀请
        for (int j=W; j>=0; --j)
        {
            int sumw=0, sumb=0;
            for (int k=0; k<(int)g[i].size(); ++k)//或者只能邀请其中的一个人
            {
                sumw+=w[g[i][k]], sumb+=b[g[i][k]];
                if (j>=w[g[i][k]]) chkMax(f[j], f[j-w[g[i][k]]]+b[g[i][k]]);
            }
            if (j>=sumw) chkMax(f[j], f[j-sumw]+sumb);//或者全部人
        }
    }
    write(f[W], '\n');
    IO::flush();
    return 0;
}

Guess you like

Origin www.cnblogs.com/G-hsm/p/CF741B.html