QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#204044 | #2429. Conquer The World | Sorting | WA | 217ms | 99060kb | C++20 | 3.7kb | 2023-10-07 00:26:37 | 2023-10-07 00:26:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
const int MAXN = 3e5 + 7 ;
int n ;
vector < pii > v[ MAXN ] ;
pii a[ MAXN ] ;
bool is_root[ MAXN ] ;
int sub[ MAXN ] ;
int val[ MAXN ] ;
ll pref[ MAXN ] ;
pii tot ;
ll ans , dp[ MAXN ] ;
void init ( int x , int prv ) {
sub[ x ] = val[ x ] ;
for ( auto [ y , len ] : v[ x ] ) {
if ( y == prv ) { continue ; }
pref[ y ] = pref[ x ] + len ;
init ( y , x ) ;
sub[ x ] += sub[ y ] ;
}
if ( sub[ x ] < 0 ) {
ans += ( pref[ x ] - pref[ prv ] ) * ( -sub[ x ] ) ;
val[ x ] += ( -sub[ x ] ) ;
val[ prv ] -= ( -sub[ x ] ) ;
}
else if ( tot.fi - tot.se - sub[ x ] < 0 ) {
int hh = tot.fi - tot.se - sub[ x ] ;
ans += ( pref[ x ] - pref[ prv ] ) * ( -hh ) ;
val[ x ] -= ( -hh ) ;
val[ prv ] += ( -hh ) ;
}
}
multiset < pair < ll , ll > > complete[ MAXN ] ;
multiset < ll > lft[ MAXN ] ;
void dfs ( int x , int prv ) {
for ( auto [ y , len ] : v[ x ] ) {
if ( y == prv ) { continue ; }
dfs ( y , x ) ;
dp[ x ] += dp[ y ] ;
if ( complete[ x ].size ( ) < complete[ y ].size ( ) ) {
swap ( complete[ x ] , complete[ y ] ) ;
}
if ( lft[ x ].size ( ) < lft[ y ].size ( ) ) {
swap ( lft[ x ] , lft[ y ] ) ;
}
}
for ( auto [ y , len ] : v[ x ] ) {
if ( y == prv ) { continue ; }
for ( auto sr : complete[ y ] ) {
complete[ x ].insert ( sr ) ;
}
for ( auto sr : lft[ y ] ) {
lft[ x ].insert ( sr ) ;
}
}
while ( val[ x ] > 0 ) {
-- val[ x ] ;
lft[ x ].insert ( pref[ x ] ) ;
}
while ( val[ x ] < 0 ) {
++ val[ x ] ;
assert ( lft[ x ].empty ( ) == false ) ;
auto it = lft[ x ].begin ( ) ;
ll aux = *it - pref[ x ] ; lft[ x ].erase ( it ) ;
dp[ x ] += aux ;
complete[ x ].insert ( { -aux + pref[ x ] , aux + pref[ x ] } ) ;
}
/**
while ( val[ x ] < 0 ) {
++ val[ x ] ;
ll aux = ( 1LL << 40 ) ;
dp[ x ] += aux ;
complete[ x ].insert ( { -aux , x } ) ;
}
**/
while ( complete[ x ].empty ( ) == false && lft[ x ].empty ( ) == false ) {
auto it1 = complete[ x ].begin ( ) ;
auto it2 = lft[ x ].begin ( ) ;
auto [ add , nw ] = *it1 ;
ll cand = *it2 ;
if ( add + cand - 2 * pref[ x ] < 0 ) {
complete[ x ].erase ( it1 ) ;
lft[ x ].erase ( it2 ) ;
dp[ x ] += add + cand - 2 * pref[ x ] ;
complete[ x ].insert ( { - ( cand - pref[ x ] ) + pref[ x ] , cand } ) ;
lft[ x ].insert ( nw ) ;
}
else { break ; }
}
}
void solve ( ) {
cin >> n ;
for ( int i = 1 , x , y , z ; i < n ; ++ i ) {
cin >> x >> y >> z ;
v[ x ].push_back ( { y , z } ) ;
v[ y ].push_back ( { x , z } ) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ].fi >> a[ i ].se ;
int aux = min ( a[ i ].fi , a[ i ].se ) ;
a[ i ].fi -= aux , a[ i ].se -= aux ;
tot.fi += a[ i ].fi , tot.se += a[ i ].se ;
val[ i ] = a[ i ].fi - a[ i ].se ;
}
init ( 1 , -1 ) ;
dfs ( 1 , -1 ) ;
cout << ans + dp[ 1 ] << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
Details
Test #1:
score: 100
Accepted
time: 3ms
memory: 44356kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 46392kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 46544kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 44544kb
Test #5:
score: 0
Accepted
time: 6ms
memory: 46500kb
Test #6:
score: 0
Accepted
time: 5ms
memory: 47072kb
Test #7:
score: 0
Accepted
time: 3ms
memory: 46664kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 46604kb
Test #9:
score: 0
Accepted
time: 110ms
memory: 92712kb
Test #10:
score: 0
Accepted
time: 52ms
memory: 61724kb
Test #11:
score: 0
Accepted
time: 217ms
memory: 99060kb
Test #12:
score: -100
Wrong Answer
time: 0ms
memory: 44572kb