Submission #7960391


Source Code Expand

// May this submission get accepted!

#pragma GCC optimize ("O3")
#pragma GCC target ("tune=native")
#pragma GCC target ("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i,n) for (long long i=0, i##_len=(n); i<i##_len; i++)
#define RANGE(i,is,ie) for (long long i=(is), i##_end=(ie); i<=i##_end; i++)
#define DSRNG(i,is,ie) for (long long i=(is), i##_end=(ie); i>=i##_end; i--)
#define STEP(i, is, ie, step) for (long long i=(is), i##_end=(ie), i##_step = (step); i<=i##_end; i+=i##_step)
#define UNIQUE(v) do { sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end()); } while (false)
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b; return true;} return false; }
template<class T> bool chmin(T &a, const T &b) {if (a > b) {a = b; return true;} return false; }
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")
#define DUMP(q) cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q) do { cerr << "[DEBUG] " #q ": ["; REP(i, (q).size()) { cerr << (q)[i] << (i == i_len-1 ? "" : ", "); } cerr << "] at " __FILE__ ":" << __LINE__ << endl; } while (false)
template<class T> T gcd(T a, T b) { if (a < b) swap(a, b); while (b) swap(a %= b, b); return a; }
template<class T> T lcm(const T a, const T b) { return a / gcd(a, b) * b; }

// gcc拡張マクロ
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
#define DANCE_ long
#define ROBOT_ unsigned
#define HUMAN_ signed
#define CHOKUDAI_ const
using  ll = DANCE_ HUMAN_ DANCE_;
using ull = DANCE_ ROBOT_ DANCE_;
using cll = DANCE_ DANCE_ CHOKUDAI_;
using  ld = long double;
using namespace std;

// モジュール

// Union-Find モジュール
struct union_find {
    vector<ull> par;
    vector<ull> num;
    union_find(ull n) : par(n, 0), num(n, 1) { iota(ALL_OF(par), 0); }
    ull root(ull i) { return par[i] == i ? i : (par[i] = root(par[i])); }
    bool same(ull i, ull j) { return root(i) == root(j); }
    void unite(ull i, ull j) {
        ull ri = root(i);
        ull rj = root(j);
        if (ri != rj) {
            if (num[ri] > num[rj]) swap(ri, rj);
            par[ri] = rj;
            num[rj] += num[ri];
        }
    }
};

// ナップザック問題
ll solve(vector<ll> &w, vector<ll> &v, ll wmax) {

    // dp[w] := 荷物の総重量 w のときに詰めうる価値の和の最大値
    vector<ll> dp(wmax + 1, 0);
    REP(i, v.size()) {
        DSRNG(u, wmax, w[i]) {
            chmax(dp[u], dp[u-w[i]] + v[i]);
        }
    }
    return *max_element(ALL_OF(dp));

}

// 処理内容
int main() {
    
    // インタラクティブ問題では除去した方がいいかも
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    ll n, m, W; cin >> n >> m >> W;
    vector<ll> w(n), v(n), a(m), b(m);
    REP(i, n) cin >> w[i] >> v[i];
    REP(i, m) cin >> a[i] >> b[i];

    union_find uf(n);
    REP(i, m) { // 0-indexed
        a[i]--;
        b[i]--;
        uf.unite(a[i], b[i]);
    }
    REP(i, n) uf.root(i);
    vector<ull> unq = uf.par; UNIQUE(unq);
    map<ll, ll> sahz;
    ll n2 = unq.size();
    REP(i, n2) sahz[unq[i]] = i;

    vector<ll> w2(n2, 0), v2(n2, 0);
    REP(i, n) {
        w2[sahz[uf.par[i]]] += w[i];
        v2[sahz[uf.par[i]]] += v[i];
    }

    REP(i, n2) {
        cerr << w2[i] << " " << v2[i] << endl;
    }

    cout << solve(w2, v2, W) << endl;
    
}

Submission Info

Submission Time
Task B - ナップサック問題
User ganmodokix
Language C++14 (GCC 5.4.1)
Score 200
Code Size 3763 Byte
Status AC
Exec Time 2 ms
Memory 384 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 200 / 200
Status
AC × 4
AC × 25
Set Name Test Cases
Sample 0_sample_00, 0_sample_01, 0_sample_02, 0_sample_03
All 0_sample_00, 0_sample_01, 0_sample_02, 0_sample_03, 1_random_00, 1_random_01, 1_random_02, 1_random_03, 1_random_04, 1_random_05, 1_random_06, 1_random_07, 1_random_08, 1_random_09, 1_random_10, 1_random_11, 1_random_12, 1_random_13, 1_random_14, 1_random_15, 1_random_16, 1_random_17, 1_random_18, 1_random_19, 2_maximum
Case Name Status Exec Time Memory
0_sample_00 AC 2 ms 256 KiB
0_sample_01 AC 2 ms 256 KiB
0_sample_02 AC 2 ms 256 KiB
0_sample_03 AC 2 ms 384 KiB
1_random_00 AC 2 ms 384 KiB
1_random_01 AC 2 ms 384 KiB
1_random_02 AC 2 ms 256 KiB
1_random_03 AC 2 ms 256 KiB
1_random_04 AC 2 ms 256 KiB
1_random_05 AC 2 ms 256 KiB
1_random_06 AC 2 ms 384 KiB
1_random_07 AC 2 ms 256 KiB
1_random_08 AC 2 ms 384 KiB
1_random_09 AC 2 ms 384 KiB
1_random_10 AC 2 ms 256 KiB
1_random_11 AC 2 ms 384 KiB
1_random_12 AC 2 ms 384 KiB
1_random_13 AC 2 ms 256 KiB
1_random_14 AC 2 ms 384 KiB
1_random_15 AC 2 ms 256 KiB
1_random_16 AC 2 ms 384 KiB
1_random_17 AC 2 ms 256 KiB
1_random_18 AC 2 ms 384 KiB
1_random_19 AC 2 ms 384 KiB
2_maximum AC 2 ms 384 KiB