답안 #826367

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
826367 2023-08-15T13:32:58 Z hafo Džumbus (COCI19_dzumbus) C++14
110 / 110
50 ms 34136 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e3 + 7;
const ll oo = 1e18 + 69;

int n, m, a[maxn], u, v, q, s, sz[maxn];
vector<int> g[maxn];
ll dp[maxn][maxn][3], f[maxn][maxn][3], res[maxn];
bool vis[maxn];

void dfs2(int u) {
    vis[u] = 1;
    for(auto v:g[u]) {
        if(vis[v]) continue;
        dfs2(v);
    }
}

void dfs(int u, int par) {
    for(auto v:g[u]) {
        if(v == par) continue;
        dfs(v, u);
    }

    for(int i = 0; i <= Size(g[u]); i++) 
        for(int j = 0; j <= n; j++) 
            for(int t = 0; t < 3; t++) f[i][j][t] = oo;
    
    if(par != -1) {
        sz[u] = 1;
        f[0][0][1] = a[u];
    }
    f[0][0][0] = 0;

    for(int i = 1; i <= Size(g[u]); i++) {
        int v = g[u][i - 1];
        if(v == par) {
            for(int j = 0; j <= n; j++) 
                for(int t = 0; t < 3; t++) f[i][j][t] = f[i - 1][j][t];
            continue;
        }

        for(int cur = 0; cur <= sz[u]; cur++) {
            for(int pre = 0; pre <= sz[v]; pre++) {
                for(int t = 0; t < 3; t++) {
                    mini(f[i][cur + pre][0], f[i - 1][cur][0] + dp[v][pre][t]);
                }

                mini(f[i][cur + pre][1], f[i - 1][cur][1] + dp[v][pre][0]);

                mini(f[i][cur + pre][2], f[i - 1][cur][2] + dp[v][pre][0]);
                mini(f[i][cur + pre][2], f[i - 1][cur][2] + dp[v][pre][2]);
                if(cur + pre + 1 <= n) {
                    mini(f[i][cur + pre + 1][2], f[i - 1][cur][2] + dp[v][pre][1]);
                    mini(f[i][cur + pre + 1][2], f[i - 1][cur][1] + dp[v][pre][2]);
                }
                if(cur + pre + 2 <= n) {
                    mini(f[i][cur + pre + 2][2], f[i - 1][cur][1] + dp[v][pre][1]);
                }
            }
        }
        sz[u] += sz[v];
    }

    for(int j = 0; j <= n; j++)
        for(int t = 0; t < 3; t++) dp[u][j][t] = f[Size(g[u])][j][t]; 
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n>>m;
    for(int i = 1; i <= n; i++) cin>>a[i];
    while(m--) {
        cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
    }

    for(int i = 1; i <= n; i++) 
        if(!vis[i]) {
            g[0].pb(i);
            g[i].pb(0);
            dfs2(i);
        }

    dfs(0, -1);
    for(int i = 0; i <= n; i++) {
        res[i] = oo;
        for(int t = 0; t < 3; t++) mini(res[i], dp[0][i][t]);
    }
    
    for(int i = n - 1; i >= 0; i--) mini(res[i], res[i + 1]);
    cin>>q;
    while(q--) {
        cin>>s;
        int l = 2, r = n, mid, ans = 0;
        while(l <= r) {
            mid = l + r >> 1;
            if(res[mid] <= s) {
                ans = mid;
                l = mid + 1;
            } else r = mid - 1;
        }
        cout<<ans<<"\n";
    }
    return 0;
}

Compilation message

dzumbus.cpp: In function 'int main()':
dzumbus.cpp:119:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  119 |             mid = l + r >> 1;
      |                   ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 24276 KB Output is correct
2 Correct 18 ms 24172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 24276 KB Output is correct
2 Correct 18 ms 24172 KB Output is correct
3 Correct 48 ms 26520 KB Output is correct
4 Correct 47 ms 27048 KB Output is correct
5 Correct 46 ms 26572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 3060 KB Output is correct
2 Correct 26 ms 2944 KB Output is correct
3 Correct 28 ms 3500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 24276 KB Output is correct
2 Correct 18 ms 24172 KB Output is correct
3 Correct 48 ms 26520 KB Output is correct
4 Correct 47 ms 27048 KB Output is correct
5 Correct 46 ms 26572 KB Output is correct
6 Correct 34 ms 3060 KB Output is correct
7 Correct 26 ms 2944 KB Output is correct
8 Correct 28 ms 3500 KB Output is correct
9 Correct 43 ms 26696 KB Output is correct
10 Correct 48 ms 30480 KB Output is correct
11 Correct 50 ms 34136 KB Output is correct