Submission #627620

# Submission time Handle Problem Language Result Execution time Memory
627620 2022-08-12T17:44:16 Z Huy Paths (RMI21_paths) C++17
36 / 100
600 ms 22016 KB
/// punch then pray
#include<bits/stdc++.h>
//#define int long long
#define pii pair<int,int>
#define fi first
#define se second
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
const int N = (int)1e5 ;
const int maxN = (int)5e5 + 1;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const ll infty = 1e18 + 7;
const int base = (int)4e5;
const int block_size = 710;

void InputFile()
{
    //freopen("palpath.in","r",stdin);
    //freopen("palpath.out","w",stdout);
    //freopen("test.inp","r",stdin);
    //freopen("test.out","w",stdout);
}

void FastInput()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}

/// combining subtree nhung chi dua vao la ?
/// n log cho moi goc ?
/// n ^ 2 log full goc ?

int n,k;
vector<vector<pii>> adj;
int x[2005],y[2005],c[2005];
ll dp[2005][2005];
ll f[2005];
int subleaf[2005];

void Read()
{
    cin >> n >> k;
    adj.resize(n+1);
    for(int i = 1;i < n;i++)
    {
        cin >> x[i] >> y[i] >> c[i];
        adj[x[i]].push_back({y[i],c[i]});
        adj[y[i]].push_back({x[i],c[i]});
    }
}

void PreDFS(int u,int p)
{
    subleaf[u] = 1;
    int non_leaf = 0;
    for(pii e : adj[u])
    {
        int v = e.fi;
        int wt = e.se;
        if(v == p) continue;
        PreDFS(v,u);
        non_leaf = 1;
        subleaf[u] += subleaf[v];
    }
    subleaf[u] -= non_leaf;
}

void DFS(int u,int p)
{
    int sleaf = 0;
    for(pii e : adj[u])
    {
        int v = e.fi;
        int wt = e.se;
        if(v == p) continue;
        DFS(v,u);

        for(int i = 1;i <= min(k,sleaf + subleaf[v]);i++)
        {
            if(i <= sleaf) f[i] = dp[u][i];
            else f[i] = 0;
        }

        for(int i = 0;i <= sleaf;i++)
        {
            //f[i] = max(f[i],dp[u][i]);
            for(int j = 1;j <= min(subleaf[v],k);j++)
            {
                if(i + j > k) break;
                f[i+j] = max(f[i+j],dp[u][i] + dp[v][j] + wt);
            }
        }

        for(int i = 1;i <= min(k,sleaf + subleaf[v]);i++)
        {
            dp[u][i] = f[i];
        }

        sleaf += subleaf[v];
        sleaf = min(sleaf,k);
    }
}

void Prc_root(int id)
{
    PreDFS(id,0);
    DFS(id,0);
    ll res = 0;
    for(int i = 1;i <= n;i++)
    {
        //cout << subleaf[i] <<' ';
        for(int j = 1;j <= subleaf[i];j++)
        {
            res = max(res,dp[i][j]);
            dp[i][j] = 0;
        }
    }
    //cout << dp[7][2];
    cout << res <<'\n';
}

void Solve()
{
    for(int i = 1;i <= n;i++)
    {
        Prc_root(i);
    }
    //Prc_root(1);
}


int32_t main()
{
    FastInput();
    //InputFile();
    //int sub_type;
    //cin >> sub_type;
    //Sieve()
    int test;
    //cin >> test;
    test = 1;
    while(test--)
    {
        Read();
        Solve();
        //Debug();
    }
}
////
/////
//////

Compilation message

Main.cpp:8: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    8 | #pragma GCC optimization ("O3")
      | 
Main.cpp:9: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    9 | #pragma GCC optimization ("unroll-loops")
      | 
Main.cpp: In function 'void PreDFS(int, int)':
Main.cpp:68:13: warning: unused variable 'wt' [-Wunused-variable]
   68 |         int wt = e.se;
      |             ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 4 ms 1308 KB Output is correct
4 Correct 4 ms 1236 KB Output is correct
5 Correct 4 ms 1364 KB Output is correct
6 Correct 3 ms 1108 KB Output is correct
7 Correct 3 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 4 ms 1308 KB Output is correct
4 Correct 4 ms 1236 KB Output is correct
5 Correct 4 ms 1364 KB Output is correct
6 Correct 3 ms 1108 KB Output is correct
7 Correct 3 ms 1236 KB Output is correct
8 Correct 147 ms 8352 KB Output is correct
9 Correct 176 ms 6736 KB Output is correct
10 Correct 88 ms 5300 KB Output is correct
11 Correct 190 ms 9244 KB Output is correct
12 Correct 100 ms 5968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 4 ms 1308 KB Output is correct
4 Correct 4 ms 1236 KB Output is correct
5 Correct 4 ms 1364 KB Output is correct
6 Correct 3 ms 1108 KB Output is correct
7 Correct 3 ms 1236 KB Output is correct
8 Correct 147 ms 8352 KB Output is correct
9 Correct 176 ms 6736 KB Output is correct
10 Correct 88 ms 5300 KB Output is correct
11 Correct 190 ms 9244 KB Output is correct
12 Correct 100 ms 5968 KB Output is correct
13 Execution timed out 1096 ms 22016 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 5496 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 4 ms 1308 KB Output is correct
4 Correct 4 ms 1236 KB Output is correct
5 Correct 4 ms 1364 KB Output is correct
6 Correct 3 ms 1108 KB Output is correct
7 Correct 3 ms 1236 KB Output is correct
8 Correct 147 ms 8352 KB Output is correct
9 Correct 176 ms 6736 KB Output is correct
10 Correct 88 ms 5300 KB Output is correct
11 Correct 190 ms 9244 KB Output is correct
12 Correct 100 ms 5968 KB Output is correct
13 Execution timed out 1096 ms 22016 KB Time limit exceeded
14 Halted 0 ms 0 KB -