답안 #23344

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
23344 2017-05-06T11:37:23 Z Nurlykhan 구슬과 끈 (APIO14_beads) C++14
0 / 100
5 ms 512 KB
#include <bits/stdc++.h>
 
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long 
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
 
using namespace std;
 
const int N = (int) 2e5 + 7;
const int M = (int) 2e6 + 7;
const ll LINF = (ll) 1e16;
const int INF = (int) 1e9 + 7;
const double EPS = (double) 1e-9;
int n;
struct edge {
    int a, b, c;

    edge() {}
};
edge a[N];
ll dp[N];

bool ok(int mask, edge x, edge y) {
    if (x.a == y.a || x.a == y.b) {
        bool bad = 0;
        for (int i = 0; i < (1 << (n - 1)); i++) {
            if ((mask >> i) % 2 && (a[i].a == x.a || a[i].b == x.a)) {
                bad = 1;
            }
        }
        if (!bad) return true;
    }
    if (x.b == y.a || x.b == y.b) { 
       bool bad = 0;
        for (int i = 0; i < (1 << (n - 1)); i++) {
            if ((mask >> i) % 2 && (a[i].a == x.b || a[i].b == x.b)) {
                bad = 1;
            }
        }
        if (!bad) return true;
    }
    return false;
}

int main() {
    #define fn "balls"
    #ifdef witch
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #else
//        freopen(fn".in", "r", stdin);
//        freopen(fn".out", "w", stdout);
    #endif
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        cin >> a[i].a >> a[i].b >> a[i].c;
    }
    for (int mask = 0; mask < (1 << (n - 1)); mask++) {
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1; j++) {
                if ((mask >> i) % 2 || (mask >> j) % 2 || i == j) 
                    continue;
                if (ok(mask, a[i], a[j])) {
                    ll &tmp = dp[mask | (1 << i) | (1 << j)];
                    tmp = max(tmp, dp[mask] + a[i].c + a[j].c);
                }  
            }
        }
    }
    cout << *max_element(dp, dp + N);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Incorrect 4 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Incorrect 4 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Incorrect 4 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Incorrect 4 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -