#include<bits/stdc++.h>
using namespace std ;
#define maxn 200009
#define ll long long
#define fi first
#define se second
#define pb push_back
#define left id<<1
#define right id<<1|1
#define re exit(0);
#define _lower(x) lower_bound(v.begin(),v.end(),x)-v.begin()
#define TIME 1.0*clock()/CLOCKS_PER_SEC
const int mod = 1e9+7;
const int INF = 1e9;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<ll> vl;
void add ( int &a , int b )
{
a += b ;
if ( a > mod ) a -= mod ;
if ( a < 0 ) a += mod ;
}
template <typename T> void chkmin ( T &a , T b ) { if ( a > b ) a = b ; }
template <typename T> void chkmax ( T &a , T b ) { if ( a < b ) a = b ; }
int _pow ( int a , int n )
{
if ( n == 0 ) return 1 ;
int res = _pow (a,n/2) ;
if ( n % 2 ) return 1ll*res*res%mod*a%mod ;
else return 1ll*res*res%mod ;
}
void rf ()
{
freopen ("bai1.inp","r",stdin) ;
}
int n , k ;
vii adjList [maxn] ;
ll d1 [maxn] , d2 [maxn] , d [maxn] ;
int pos ; ll best = 0 ;
void bfs ( int u )
{
memset ( d , 0x3f , sizeof d ) ;
queue <int> q ;
d [u] = 0 ;
q . push (u) ;
while ( !q.empty() )
{
int u = q.front() ; q.pop() ;
for ( auto x : adjList [u] )
{
int v = x.fi , w = x.se ;
if ( d [v] > d [u] + w ) q . push (v) , d [v] = d [u] + w ;
}
}
best = -1 ;
for ( int i = 1 ; i <= n ; i ++ ) if ( d [i] > best ) best = d [i] , pos = i ;
}
int main ()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// rf () ;
cin >> n >> k ;
for ( int i = 1 ; i < n ; i ++ )
{
int u , v , w ; cin >> u >> v >> w ;
adjList [u] . pb ({v,w}) ;
adjList [v] . pb ({u,w}) ;
}
bfs (1) ;
bfs (pos) ;
for ( int i = 1 ; i <= n ; i ++ ) d1 [i] = d [i] ;
bfs (pos) ;
for ( int i = 1 ; i <= n ; i ++ ) d2 [i] = d [i] ;
for ( int i = 1 ; i <= n ; i ++ ) cout << max (d1[i],d2[i]) << "\n" ;
}
Compilation message
Main.cpp: In function 'void rf()':
Main.cpp:42:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen ("bai1.inp","r",stdin) ;
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
16728 KB |
Output is correct |
2 |
Correct |
53 ms |
17016 KB |
Output is correct |
3 |
Correct |
39 ms |
16220 KB |
Output is correct |
4 |
Correct |
42 ms |
16724 KB |
Output is correct |
5 |
Correct |
47 ms |
16980 KB |
Output is correct |
6 |
Correct |
43 ms |
16848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |