답안 #896242

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
896242 2024-01-01T05:10:27 Z 8pete8 Magic Tree (CEOI19_magictree) C++17
70 / 100
105 ms 42152 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<float.h>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
//#define int long long
#define double long double
const int mod=998244353,mxn=1e5,lg=22;//inf=1e18,minf=-1e18,Mxn=100000;
vector<int>adj[mxn+10];
int hvy[mxn+10],sz[mxn+10],sum[mxn+10];
ll dp[mxn+10][22];
map<int,ll>mp[mxn+10];
pii info[mxn+10];
multiset<int>v[mxn+10];
int n,m,k;
ll ans=0;
void init(int cur,int p){
	sz[cur]=1;
	for(auto i:adj[cur]){
		init(i,cur);
		sz[cur]+=sz[i];
	}
	for(auto i:adj[cur])if(sz[hvy[cur]]<sz[i])hvy[cur]=i;
}
void dfs(int cur,int p){
	for(auto i:adj[cur])dfs(i,cur);
	for(auto i:adj[cur])for(int j=0;j<=k;j++)dp[cur][j]+=dp[i][j];
	dp[cur][info[cur].f]+=info[cur].s;
	for(int j=1;j<=k;j++)dp[cur][j]=max(dp[cur][j],dp[cur][j-1]);
}
ll mx=0;
void dfs2(int cur,int p){
	for(auto i:adj[cur])dfs2(i,cur);
	for(auto i:adj[cur]){
		if(i==hvy[cur])continue;
		if(mp[i].size()>mp[cur].size())swap(mp[cur],mp[i]);
		for(auto j:mp[i])mp[cur][j.f]+=j.s;
	}
	mp[cur][info[cur].f]+=info[cur].s;
	auto it=mp[cur].upper_bound(info[cur].f);
	while(it!=mp[cur].end()&&info[cur].s>0){
		if(it==mp[cur].end())assert(0);
		if(info[cur].s>=it->s)info[cur].s-=it->s,mp[cur].erase(it);
		else {it->s-=info[cur].s;break;}
	}
}
void solve(int cur,int p){
	for(auto i:adj[cur])solve(i,cur);
	if(hvy[cur])swap(v[cur],v[hvy[cur]]);
	for(auto i:adj[cur])if(i!=hvy[cur])for(auto j:v[i])v[cur].insert(j);
	if(info[cur].s==0)return;
	auto it=v[cur].upper_bound(info[cur].f);
	if(it!=v[cur].end())v[cur].erase(it);
	v[cur].insert(info[cur].f);

}
int32_t main(){
    fastio
	cin>>n>>m>>k;
	for(int i=2;i<=n;i++){
		int p;cin>>p;
		adj[p].pb(i);
	}
	bool yes=true;
	for(int i=0;i<m;i++){
		int v,d,w;cin>>v>>d>>w;
		info[v]={d,w};
		ans+=w;
		if(w!=1)yes=false;
	}
	if(yes){//w=1
		init(1,-1);
		solve(1,-1);
		cout<<v[1].size();
	}
	else if(k<=20){
		ans=0;
		dfs(1,-1);
		for(int i=0;i<=k;i++)ans=max(ans,dp[1][i]);
		cout<<ans;
	}
	else{
		ans=0;
		dfs2(1,-1);
		for(auto i:mp[1])ans+=i.s;
		cout<<ans;
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 3 ms 14916 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 3 ms 14752 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14960 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 26912 KB Output is correct
2 Correct 36 ms 26452 KB Output is correct
3 Correct 105 ms 41552 KB Output is correct
4 Correct 54 ms 25296 KB Output is correct
5 Correct 81 ms 26452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 4 ms 14940 KB Output is correct
3 Correct 4 ms 14940 KB Output is correct
4 Correct 43 ms 28980 KB Output is correct
5 Correct 63 ms 33876 KB Output is correct
6 Correct 51 ms 29008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 32852 KB Output is correct
2 Correct 41 ms 32848 KB Output is correct
3 Correct 41 ms 36944 KB Output is correct
4 Correct 27 ms 32216 KB Output is correct
5 Correct 37 ms 42152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 3 ms 14916 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 3 ms 14752 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14960 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
10 Correct 79 ms 31464 KB Output is correct
11 Correct 86 ms 31824 KB Output is correct
12 Correct 52 ms 24344 KB Output is correct
13 Correct 42 ms 23764 KB Output is correct
14 Correct 50 ms 29140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 17 ms 30556 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 3 ms 14916 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 3 ms 14752 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14960 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 43 ms 28980 KB Output is correct
14 Correct 63 ms 33876 KB Output is correct
15 Correct 51 ms 29008 KB Output is correct
16 Correct 79 ms 31464 KB Output is correct
17 Correct 86 ms 31824 KB Output is correct
18 Correct 52 ms 24344 KB Output is correct
19 Correct 42 ms 23764 KB Output is correct
20 Correct 50 ms 29140 KB Output is correct
21 Correct 16 ms 17688 KB Output is correct
22 Correct 62 ms 25204 KB Output is correct
23 Correct 55 ms 25272 KB Output is correct
24 Correct 91 ms 31428 KB Output is correct
25 Correct 56 ms 23788 KB Output is correct
26 Correct 75 ms 24712 KB Output is correct
27 Correct 55 ms 23816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 3 ms 14916 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 3 ms 14752 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14960 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
10 Correct 52 ms 26912 KB Output is correct
11 Correct 36 ms 26452 KB Output is correct
12 Correct 105 ms 41552 KB Output is correct
13 Correct 54 ms 25296 KB Output is correct
14 Correct 81 ms 26452 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 4 ms 14940 KB Output is correct
17 Correct 4 ms 14940 KB Output is correct
18 Correct 43 ms 28980 KB Output is correct
19 Correct 63 ms 33876 KB Output is correct
20 Correct 51 ms 29008 KB Output is correct
21 Correct 43 ms 32852 KB Output is correct
22 Correct 41 ms 32848 KB Output is correct
23 Correct 41 ms 36944 KB Output is correct
24 Correct 27 ms 32216 KB Output is correct
25 Correct 37 ms 42152 KB Output is correct
26 Correct 79 ms 31464 KB Output is correct
27 Correct 86 ms 31824 KB Output is correct
28 Correct 52 ms 24344 KB Output is correct
29 Correct 42 ms 23764 KB Output is correct
30 Correct 50 ms 29140 KB Output is correct
31 Runtime error 17 ms 30556 KB Execution killed with signal 6
32 Halted 0 ms 0 KB -