Submission #1108669

# Submission time Handle Problem Language Result Execution time Memory
1108669 2024-11-04T18:37:12 Z cpptowin Paths (RMI21_paths) C++17
36 / 100
600 ms 61004 KB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 2010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
    x += k;
    x %= mod;
    if(x < 0) x += mod;
}
void del(int &x, int k)
{
    x -= k;
    x %= mod;
    if(x < 0) x += mod;
}
vii ke[maxn];
int n,k;
namespace sub4 
{
    int f[N],pos[N];
    int cc = 0;
    int topo[N];
    int h[N],par[N][11];
    void dfs(int u) 
    {
        pos[u] = ++cc;
        topo[cc] = u;
        for(auto [v,w] : ke[u]) if(v != par[u][0]) 
        {
            par[v][0] = u;
            h[v] = h[u] + 1;
            fo(i,1,10) par[v][i] = par[par[v][i - 1]][i - 1];
            f[v] = f[u] + w;
            dfs(v);
        }
    }
    int lca(int u, int v)
    {
        if (h[u] < h[v])
            swap(u, v);
        int del = h[u] - h[v];
        fod(i, 10, 0) if (bit(del, i)) u = par[u][i];
        if (u == v)
            return u;
        fod(i, 10, 0) if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
        return par[u][0];
    }
    void solve()
    {
        fo(i,1,n)
        {
            int ans = 0;
            memset(f,0,sizeof f);
            cc = 0;
            memset(h,0,sizeof h);
            memset(par,0,sizeof par);
            memset(pos,0,sizeof pos);
            memset(topo,0,sizeof topo);
            dfs(i);
            int cnt = k;
            priority_queue<pii> q;
            fo(i,1,n) if(ss(ke[i]) == 1) 
            {
                q.push({f[i],i});
            }
            set<int> st;
            while(cnt > 0 and ss(q))
            {
                auto [du,u] = q.top();
                q.pop();
                auto it = st.lower_bound(pos[u]);
                int val = f[u];
                if(it != st.begin())
                {
                    auto it1 = prev(it);
                    minimize(val,f[u] - f[lca(topo[*it1],u)]);
                }
                if(it != st.end())
                {
                    minimize(val,f[u] - f[lca(topo[*it],u)]);
                }
                if(val >= q.top().fi) 
                {
                    ans += val;
                    st.insert(pos[u]);
                    cnt--;
                }
                else q.push({val,u});
            }
            cout << ans;en;
        }
    }
}
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    fo(i,1,n - 1) 
    {
        int u,v,w;
        cin >> u >> v >> w;
        ke[u].pb(v,w);
        ke[v].pb(u,w);
    }
    sub4::solve();
}

Compilation message

Main.cpp:136:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  136 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  142 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 24144 KB Output is correct
2 Correct 5 ms 24144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 24144 KB Output is correct
2 Correct 5 ms 24144 KB Output is correct
3 Correct 11 ms 24144 KB Output is correct
4 Correct 9 ms 24028 KB Output is correct
5 Correct 9 ms 24144 KB Output is correct
6 Correct 7 ms 24144 KB Output is correct
7 Correct 9 ms 24144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 24144 KB Output is correct
2 Correct 5 ms 24144 KB Output is correct
3 Correct 11 ms 24144 KB Output is correct
4 Correct 9 ms 24028 KB Output is correct
5 Correct 9 ms 24144 KB Output is correct
6 Correct 7 ms 24144 KB Output is correct
7 Correct 9 ms 24144 KB Output is correct
8 Correct 200 ms 24276 KB Output is correct
9 Correct 99 ms 24084 KB Output is correct
10 Correct 70 ms 24144 KB Output is correct
11 Correct 121 ms 24284 KB Output is correct
12 Correct 88 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 24144 KB Output is correct
2 Correct 5 ms 24144 KB Output is correct
3 Correct 11 ms 24144 KB Output is correct
4 Correct 9 ms 24028 KB Output is correct
5 Correct 9 ms 24144 KB Output is correct
6 Correct 7 ms 24144 KB Output is correct
7 Correct 9 ms 24144 KB Output is correct
8 Correct 200 ms 24276 KB Output is correct
9 Correct 99 ms 24084 KB Output is correct
10 Correct 70 ms 24144 KB Output is correct
11 Correct 121 ms 24284 KB Output is correct
12 Correct 88 ms 24312 KB Output is correct
13 Execution timed out 1052 ms 24400 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 70 ms 61004 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 24144 KB Output is correct
2 Correct 5 ms 24144 KB Output is correct
3 Correct 11 ms 24144 KB Output is correct
4 Correct 9 ms 24028 KB Output is correct
5 Correct 9 ms 24144 KB Output is correct
6 Correct 7 ms 24144 KB Output is correct
7 Correct 9 ms 24144 KB Output is correct
8 Correct 200 ms 24276 KB Output is correct
9 Correct 99 ms 24084 KB Output is correct
10 Correct 70 ms 24144 KB Output is correct
11 Correct 121 ms 24284 KB Output is correct
12 Correct 88 ms 24312 KB Output is correct
13 Execution timed out 1052 ms 24400 KB Time limit exceeded
14 Halted 0 ms 0 KB -