답안 #992555

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
992555 2024-06-04T16:29:58 Z Abito Paths (RMI21_paths) C++17
36 / 100
600 ms 1012 KB
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
//#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=2005;
int n,k,tin[N],sz[N],par[N],t,v[N],tree[N],lx,rx;
vector<pair<int,ll>> adj[N];
pair<ll,int> seg[4*N+5];
ll dis[N],lazy[4*N+5],val;
bool vis[N];
void dfs(int x,int p){
    par[x]=p;
    sz[x]=1;
    t++;
    tin[x]=t;
    tree[t]=x;
    for (auto u:adj[x]){
        if (p==u.F) continue;
        v[u.F]=u.S;
        dis[u.F]=dis[x]+u.S;
        dfs(u.F,x);
        sz[x]+=sz[u.F];
    }return;
}
void build(int x,int l,int r){
    lazy[x]=0;
    if (l==r){
        seg[x]={dis[tree[l]],tree[l]};
        return;
    }int mid=(l+r)/2;
    build((x<<1),l,mid);
    build((x<<1)+1,mid+1,r);
    seg[x]=max(seg[(x<<1)],seg[(x<<1)+1]);
    return;
}
void push(int x){
    seg[(x<<1)].F+=lazy[x];
    seg[(x<<1)+1].F+=lazy[x];
    lazy[(x<<1)]+=lazy[x];
    lazy[(x<<1)+1]+=lazy[x];
    lazy[x]=0;
    return;
}
void edit(int x,int l,int r){
    if (l>rx || r<lx) return;
    if (l>=lx && r<=rx){
        seg[x].F+=val;
        lazy[x]+=val;
        return;
    }int mid=(l+r)/2;
    push(x);
    edit((x<<1),l,mid);
    edit((x<<1)+1,mid+1,r);
    seg[x]=max(seg[(x<<1)],seg[(x<<1)+1]);
    return;
}
int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    cin>>n>>k;
    for (int i=1;i<n;i++){
        int x,y,w;
        cin>>x>>y>>w;
        adj[x].pb({y,w});
        adj[y].pb({x,w});
    }int l=0;
    for (int i=1;i<=n;i++) l+=bool(adj[i].size()==1);
    k=min(k,l);
    for (int i=1;i<=n;i++){
        t=0;dis[i]=0;v[i]=0;par[i]=0;
        memset(vis,0,sizeof(vis));
        dfs(i,0);
        build(1,1,n);
        ll ans=0;
        for (int kk=1;kk<=k;kk++){
            int x=seg[1].S;
            //cout<<x<<' ';
            while (!vis[x] && x){
                ans+=v[x];
                lx=tin[x];
                rx=tin[x]+sz[x]-1;
                val=-v[x];
                if (v[x]) edit(1,1,n);
                v[x]=0;
                vis[x]=1;
                x=par[x];
            }
        }cout<<ans<<endl;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 3 ms 348 KB Output is correct
4 Correct 5 ms 564 KB Output is correct
5 Correct 2 ms 568 KB Output is correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 3 ms 348 KB Output is correct
4 Correct 5 ms 564 KB Output is correct
5 Correct 2 ms 568 KB Output is correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 105 ms 600 KB Output is correct
9 Correct 150 ms 600 KB Output is correct
10 Correct 155 ms 600 KB Output is correct
11 Correct 46 ms 664 KB Output is correct
12 Correct 94 ms 676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 3 ms 348 KB Output is correct
4 Correct 5 ms 564 KB Output is correct
5 Correct 2 ms 568 KB Output is correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 105 ms 600 KB Output is correct
9 Correct 150 ms 600 KB Output is correct
10 Correct 155 ms 600 KB Output is correct
11 Correct 46 ms 664 KB Output is correct
12 Correct 94 ms 676 KB Output is correct
13 Execution timed out 679 ms 1012 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 3 ms 348 KB Output is correct
4 Correct 5 ms 564 KB Output is correct
5 Correct 2 ms 568 KB Output is correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 105 ms 600 KB Output is correct
9 Correct 150 ms 600 KB Output is correct
10 Correct 155 ms 600 KB Output is correct
11 Correct 46 ms 664 KB Output is correct
12 Correct 94 ms 676 KB Output is correct
13 Execution timed out 679 ms 1012 KB Time limit exceeded
14 Halted 0 ms 0 KB -