답안 #942905

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
942905 2024-03-11T06:44:43 Z thunopro Arboras (RMI20_arboras) C++14
11 / 100
20 ms 13468 KB
#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 , nq ; 
vi adjList [maxn] ; 

ll dp [maxn] , w [maxn] , fdp [maxn] ; 

void dfs ( int u ) 
{
	ll mx1 = 0 , mx2 = 0 ; 
	for ( auto v : adjList [u] ) 
	{
		dfs (v) ;
		if ( dp [v] + w [v] > mx1 ) mx2 = mx1 , mx1 = dp [v] + w [v] ; 
		else if ( dp [v] + w [v] > mx2 ) mx2 = dp [v] + w [v] ; 
	}
	fdp [u] = mx1 + mx2 ; 
	dp [u] = mx1 ; 
}

void solve_sub1 () 
{
	for ( int i = 0 ; i < n ; i ++ ) dp [i] = 0 ; 
	dfs (0) ; 
	int res = 0 ;
	for ( int i = 0 ; i < n ; i ++ ) add (res,fdp[i]%mod) ; 
	cout << res << "\n" ; 
}
void sub1 () 
{
	solve_sub1 () ; 
	while ( nq -- ) 
	{
		int pos , v ; cin >> pos >> v ; 
		w [pos] += v ; 
		solve_sub1 () ; 
	}
}
int main () 
{
	ios_base::sync_with_stdio(0); 
	cin.tie(0);cout.tie(0);
//	rf () ; 
	cin >> n ;
	 
	for ( int i = 1 ; i < n ; i ++ ) 
	{
		int x ; cin >> x ; 
		adjList [x] . pb (i) ;
	}
	
	for ( int i = 1 ; i < n ; i ++ ) cin >> w [i] ; 
	
	cin >> nq ; 
	 
	if ( n <= 1000 && nq <= 1000 ) sub1 () ; 
	
}

Compilation message

arboras.cpp: In function 'void rf()':
arboras.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 Correct 10 ms 9564 KB Output is correct
2 Correct 11 ms 9564 KB Output is correct
3 Correct 10 ms 9816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 12124 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 13468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9564 KB Output is correct
2 Correct 11 ms 9564 KB Output is correct
3 Correct 10 ms 9816 KB Output is correct
4 Incorrect 18 ms 12124 KB Output isn't correct
5 Halted 0 ms 0 KB -