답안 #949330

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949330 2024-03-19T06:27:00 Z Minbaev Magic Tree (CEOI19_magictree) C++17
0 / 100
51 ms 51232 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define f first
#define int long long
#define s second
#define pii pair<int,int>

template<class T>bool umax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
	tree_order_statistics_node_update> ordered_set;
	
const int N = 1e6 + 5;
const int inf = 1e17 + 7;
const int mod = 1e9 + 7;

int n,m,k;
vector<int>g[N],w(N),type(N),dp(N);

pii dfs(int x, int pr){
	
	pii sum = {0,0};
	
	for(auto to:g[x]){
		if(to == pr)continue;
		
		pii val = dfs(to,x);
		
		if(type[to] == type[x]){
			umax(dp[x],dp[to] + w[to]);
		}
		else if(type[x] == 2){
			dp[x] = max({dp[x],val.f, dp[to]});
		}
		else{
			umax(dp[x],dp[to] + w[to]);
		}
		
		if(type[to] == 1){
			sum.f += w[to];
		}
		if(type[to] == 2){
			sum.s += w[to];
		}
		sum.f += val.f;
		sum.s += val.s;
	}
	
	if(type[x] == 1)sum.f += w[x];
	else sum.s += w[x];
	
	return sum;
	
}

void solve(){
	
	cin >> n >> m >> k;
	
	for(int i = 2;i<=n;i++){
		int a;
		cin >> a;
		g[a].pb(i);
	}
	
	for(int i = 1;i<=m;i++){
		int a,b,c;
		cin >> a >> b >> c;
		type[a] = c;
		w[a] = b;
	}
	
	dfs(1,-1);
	
	cout << dp[1] <<'\n';
	
}
/*
6 6 2
1
2
3
4
5
1 1 1
2 2 2
3 3 1
4 4 2
5 5 2
6 6 1
// Success consists of going from failure to failure without loss of enthusiasm
// Problems means that you are trying
// Soberis dolboyeb
*/
signed main()
{
	
//	freopen("seq.in", "r", stdin);
//  freopen("seq.out", "w", stdout);
	ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
	int tt=1;//cin>>tt;
	while(tt--)solve();

}

# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 47216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 50008 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 47452 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 51 ms 51232 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 47216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 16 ms 47708 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 47216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 47216 KB Output isn't correct
2 Halted 0 ms 0 KB -