답안 #939940

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
939940 2024-03-07T01:59:20 Z vjudge1 Chase (CEOI17_chase) C++17
70 / 100
629 ms 94908 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
 
using i64 = long long;
 
template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}
 
template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}
 
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int n, k; cin >> n >> k;
    vector <int> a(n);
    for ( auto &u: a ) cin >> u;
    vector <vector<int>> G(n);
    for ( int i = 0; i + 1 < n; i++ ){
        int u, v; cin >> u >> v;
        --u, --v;
        G[u].pb(v), G[v].pb(u);
    }
    if ( !k ){ return cout << "0\n", 0; }
    const int inf = 1e15;
    auto opt = [&](int rt){
        vector <vector<int>> dp(n, vector <int> (k + 1));
        vector <int> s(n);
        auto dfs2 = [&](auto dfs2, int u, int p) -> void{
			for ( auto &v: G[u] ){
				if ( v != p ){
					s[u] += a[v];
					dfs2(dfs2, v, u);
				}
			}
		};
		dfs2(dfs2, rt, -1);
        auto dfs = [&](auto dfs, int u, int p) -> void{
            for ( auto &v: G[u] ){
                if ( v == p ) continue;
                for ( int j = 0; j <= k; j++ ){
					chmax(dp[v][j], dp[u][j]);
					if ( j > 0 ){
						chmax(dp[v][j], dp[u][j - 1] + s[v]);
					}
                }
                dfs(dfs, v, u);
            }
        };
        dp[rt][1] = s[rt];
        dfs(dfs, rt, -1);
        int ret = 0;
        for ( int i = 0; i < n; i++ ){
            for ( int j = 0; j <= k; j++ ){
				chmax(ret, dp[i][j]);
            }
        }
        return ret;
    };
    int ans = 0;
    for ( int i = 0; i < n; i++ ){
        chmax(ans, opt(i));
        if ( n > 1e3 ) break;
    }
    cout << ans;
 
    cout << '\n';
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:45:15: warning: unused variable 'inf' [-Wunused-variable]
   45 |     const int inf = 1e15;
      |               ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 612 ms 1608 KB Output is correct
8 Correct 65 ms 600 KB Output is correct
9 Correct 58 ms 600 KB Output is correct
10 Correct 629 ms 1460 KB Output is correct
11 Correct 215 ms 728 KB Output is correct
12 Correct 90 ms 648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 94908 KB Output is correct
2 Correct 137 ms 94800 KB Output is correct
3 Correct 104 ms 92600 KB Output is correct
4 Correct 47 ms 15440 KB Output is correct
5 Correct 150 ms 92156 KB Output is correct
6 Correct 147 ms 92244 KB Output is correct
7 Correct 163 ms 92204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 612 ms 1608 KB Output is correct
8 Correct 65 ms 600 KB Output is correct
9 Correct 58 ms 600 KB Output is correct
10 Correct 629 ms 1460 KB Output is correct
11 Correct 215 ms 728 KB Output is correct
12 Correct 90 ms 648 KB Output is correct
13 Correct 142 ms 94908 KB Output is correct
14 Correct 137 ms 94800 KB Output is correct
15 Correct 104 ms 92600 KB Output is correct
16 Correct 47 ms 15440 KB Output is correct
17 Correct 150 ms 92156 KB Output is correct
18 Correct 147 ms 92244 KB Output is correct
19 Correct 163 ms 92204 KB Output is correct
20 Incorrect 154 ms 92248 KB Output isn't correct
21 Halted 0 ms 0 KB -