답안 #1108674

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108674 2024-11-04T18:41:13 Z cpptowin Paths (RMI21_paths) C++17
36 / 100
600 ms 27720 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 ll long long
#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 
{
    ll f[N];
    int 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)
        {
            ll ans = 0;
            cc = 0;
            fo(i,1,n) 
            {
                f[i] = 0;
                fo(j,0,10) par[i][j] = 0;
            }
            dfs(i);
            int cnt = k;
            priority_queue<pair<ll,int>> 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]);
                ll 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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 23888 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 23888 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
3 Correct 10 ms 23888 KB Output is correct
4 Correct 9 ms 23888 KB Output is correct
5 Correct 10 ms 24056 KB Output is correct
6 Correct 7 ms 23888 KB Output is correct
7 Correct 10 ms 23888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 23888 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
3 Correct 10 ms 23888 KB Output is correct
4 Correct 9 ms 23888 KB Output is correct
5 Correct 10 ms 24056 KB Output is correct
6 Correct 7 ms 23888 KB Output is correct
7 Correct 10 ms 23888 KB Output is correct
8 Correct 191 ms 24136 KB Output is correct
9 Correct 85 ms 23888 KB Output is correct
10 Correct 61 ms 23888 KB Output is correct
11 Correct 103 ms 23888 KB Output is correct
12 Correct 86 ms 23888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 23888 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
3 Correct 10 ms 23888 KB Output is correct
4 Correct 9 ms 23888 KB Output is correct
5 Correct 10 ms 24056 KB Output is correct
6 Correct 7 ms 23888 KB Output is correct
7 Correct 10 ms 23888 KB Output is correct
8 Correct 191 ms 24136 KB Output is correct
9 Correct 85 ms 23888 KB Output is correct
10 Correct 61 ms 23888 KB Output is correct
11 Correct 103 ms 23888 KB Output is correct
12 Correct 86 ms 23888 KB Output is correct
13 Execution timed out 1033 ms 24240 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 30 ms 27720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 23888 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
3 Correct 10 ms 23888 KB Output is correct
4 Correct 9 ms 23888 KB Output is correct
5 Correct 10 ms 24056 KB Output is correct
6 Correct 7 ms 23888 KB Output is correct
7 Correct 10 ms 23888 KB Output is correct
8 Correct 191 ms 24136 KB Output is correct
9 Correct 85 ms 23888 KB Output is correct
10 Correct 61 ms 23888 KB Output is correct
11 Correct 103 ms 23888 KB Output is correct
12 Correct 86 ms 23888 KB Output is correct
13 Execution timed out 1033 ms 24240 KB Time limit exceeded
14 Halted 0 ms 0 KB -