Submission #770083

# Submission time Handle Problem Language Result Execution time Memory
770083 2023-06-30T18:19:16 Z vjudge1 Paths (RMI21_paths) C++17
36 / 100
600 ms 1140 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#define ve vector
#define vi vector<int>
#define vii vector<ii>
#define ii pair<int,int>
#define fi first
#define se second
#define ll long long
#define INF 1e9+7
#define pb push_back
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag,
    tree_order_statistics_node_update>;
const int MOD = 1e9+7;
const int nax = 2000+5;
const int l = 25;
void readio(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
}
//segment Tree
ll lazy[nax*4];
pair<ll,int> st[nax*4];
ve<pair<ll,int>> leef;
void build(int v, int l, int r){
    if(l == r){
        st[v] = leef[l];
        lazy[v] = 0;
        return;
    }
    int md = (l+r)/2;
    build(v*2,l,md);
    build(v*2+1,md+1,r);
    st[v] = max(st[v*2], st[v*2+1]);
    lazy[v] = 0;
}
void push(int v){
    if(!lazy[v]) return;
    st[v*2].fi += lazy[v];
    st[v*2+1].fi += lazy[v];
    lazy[v*2] += lazy[v];
    lazy[v*2+1] += lazy[v];
    lazy[v] = 0;
}
void update(int v, int l, int r, int i, int j, ll val){
    if(r < i || l > j) return;
    if(i <= l && r <= j){
        st[v].fi += val;
        lazy[v] += val;
        return;
    }
    push(v);
    int md = (l+r)/2;
    update(v*2,l,md,i,j,val);
    update(v*2+1,md+1,r,i,j,val);
    st[v] = max(st[v*2], st[v*2+1]);
}
ve<pair<int,ll>> adj[nax];
bool vis[nax];
ll dep[nax], w[nax];
int up[nax][l+5];
int timer = 0, root, tin[nax], tout[nax], cnt = 0, tstin[nax], tstout[nax];
void dfs(int u, int p, ll path){
    tin[u] = ++timer;
    tstin[u] = cnt;
    up[u][0] = p;
    vis[u] = 0;
    /*for (int i = 1; i <= l; ++i)
        up[u][i] = up[up[u][i-1]][i-1];*/
    for(auto x : adj[u]){
        if(x.fi == p) continue;
        dep[x.fi] = dep[u]+x.se;
        w[x.fi] = x.se;
        dfs(x.fi,u,path+x.se);
    }
    if(adj[u].size() == 1 && u!=root){
        leef.pb({path, u});
        cnt++;
    }
    tstout[u] = cnt;
    tout[u] = ++timer;
}
void ass(int u){
    if(vis[u] || u == root) return;
    update(1, 0, cnt-1, tstin[u],tstout[u]-1, -w[u]);
    vis[u] = 1;
    ass(up[u][0]);
}



// Subtask 5
pair<ll,int> ans;
bool isAnc(int u, int v){
    return (tin[u] <= tin[v] && tout[v] <= tout[u]);
}
int lca(int u, int v)
{
    if (isAnc(u, v))
        return u;
    if (isAnc(v, u))
        return v;
    for (int i = l; i >= 0; --i) {
        if (!isAnc(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}
ll distance(int a, int b){
    int c = lca(a,b);
    //cout << a << " " << dep[a] << " " << b << " " << dep[b] << endl;
    return (dep[a]+dep[b]-2*dep[c]);
}
void diam(int u, int p, ll cur){
    if(ans.fi < cur) ans.fi = cur, ans.se = u;
    for(auto x : adj[u]){
        if(x.fi == p) continue;
        diam(x.fi, u, cur+x.se);
    }
}




int main()
{
    optimise;
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < n-1; i++){
        int a,b,c;
        cin >> a >> b >> c;
        a--,b--;
        adj[a].pb({b,c});
        adj[b].pb({a,c});
    } 
    if(k == 1){
        dfs(0,0,0);
        int a,b;
        ans = {0,0};
        diam(0,0,0);
        a = ans.se;
        ans = {0,0};
        diam(a,a,0);
        b = ans.se;
        for (int i = 0; i < n; ++i)
        {
            cout << max(distance(a,i), distance(b,i)) << endl;
        }
        return 0;
    }
    for (int i = 0; i < n; ++i)
    {
        ll mx = 0;
        root = i;
        cnt = 0;
        timer = 0;
        leef.clear();
        dfs(i,i,0);
        build(1,0,cnt-1);
        for (int j = 0; j < k; ++j)
        {
            pair<ll,int> a = st[1];
            mx += a.fi;
            ass(a.se);
        }
        cout << mx << '\n';
    }
    //cout << op << endl;
}

Compilation message

Main.cpp: In function 'void readio()':
Main.cpp:25:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 77 ms 644 KB Output is correct
9 Correct 92 ms 716 KB Output is correct
10 Correct 68 ms 652 KB Output is correct
11 Correct 44 ms 688 KB Output is correct
12 Correct 57 ms 648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 77 ms 644 KB Output is correct
9 Correct 92 ms 716 KB Output is correct
10 Correct 68 ms 652 KB Output is correct
11 Correct 44 ms 688 KB Output is correct
12 Correct 57 ms 648 KB Output is correct
13 Correct 584 ms 852 KB Output is correct
14 Correct 444 ms 1140 KB Output is correct
15 Correct 246 ms 868 KB Output is correct
16 Correct 404 ms 964 KB Output is correct
17 Correct 486 ms 872 KB Output is correct
18 Correct 227 ms 972 KB Output is correct
19 Execution timed out 628 ms 900 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 77 ms 644 KB Output is correct
9 Correct 92 ms 716 KB Output is correct
10 Correct 68 ms 652 KB Output is correct
11 Correct 44 ms 688 KB Output is correct
12 Correct 57 ms 648 KB Output is correct
13 Correct 584 ms 852 KB Output is correct
14 Correct 444 ms 1140 KB Output is correct
15 Correct 246 ms 868 KB Output is correct
16 Correct 404 ms 964 KB Output is correct
17 Correct 486 ms 872 KB Output is correct
18 Correct 227 ms 972 KB Output is correct
19 Execution timed out 628 ms 900 KB Time limit exceeded
20 Halted 0 ms 0 KB -