Submission #537616

# Submission time Handle Problem Language Result Execution time Memory
537616 2022-03-15T09:53:14 Z dantoh000 Paths (RMI21_paths) C++14
12 / 100
129 ms 17548 KB
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
int n,k;
vector<ii> G[100005];
ll d[100005];
int p[100005][18];
int dep[100005];
int d1 = 0;
int d2 = 0;
ll md = 0;
void dfs(int u){
    for (auto v : G[u]){
        if (v.fi == p[u][0]) continue;
        d[v.fi] = d[u]+v.se;
        dep[v.fi] = dep[u]+1;
        p[v.fi][0] = u;
        dfs(v.fi);
    }
}
int lca(int a, int b){
    if (dep[a] > dep[b]) swap(a,b);
    int DD = dep[b] - dep[a];
    for (int k = 0; k < 18; k++){
        if (DD>>k&1){
            b = p[b][k];
        }
    }
    if (a == b) return a;
    for (int k = 17; k >= 0; k--){
        if (p[a][k] != -1 && p[a][k] != p[b][k]){
            a = p[a][k];
            b = p[b][k];
        }
    }
    assert(p[a][0] == p[b][0]);
    return p[a][0];
}
ll dist(int a, int b){
    return d[a] + d[b] - 2*d[lca(a,b)];
}
int main(){
    scanf("%d%d",&n,&k);
    for (int i = 1 ; i < n; i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        G[a].push_back({b,c});
        G[b].push_back({a,c});
    }
    memset(p,-1,sizeof(p));
    p[1][0] = -1;
    dfs(1);
    for (int k = 1; k < 18; k++){
        for (int i = 1; i <=n ;i ++ ){
            if (p[i][k-1] != -1) p[i][k] = p[p[i][k-1]][k-1];
        }
    }
    for (int i = 1; i <= n; i++){
        if (d1 == 0 || d[d1] < d[i]) d1 = i;
    }
    for (int i = 1; i <= n; i++){
        ll nd = dist(d1, i);
        if (md < nd){
            d2 = i;
            md = nd;
        }
    }
    for (int i = 1; i <= n; i++){
        printf("%lld\n",max(dist(i,d1), dist(i,d2)));
    }


}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |     scanf("%d%d",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~
Main.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         scanf("%d%d%d",&a,&b,&c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 107 ms 15660 KB Output is correct
2 Correct 129 ms 17548 KB Output is correct
3 Correct 126 ms 15576 KB Output is correct
4 Correct 99 ms 15840 KB Output is correct
5 Correct 124 ms 16624 KB Output is correct
6 Correct 122 ms 15868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -