이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
//#define ll long long
#define f first
#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-lopps")
#define int long long
#define double long double
using namespace std;
const int mod=998244353,mxn=3e5+5,inf=1e18,minf=-1e18,lg=62;
//#undef int
int n,k,m,q;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
struct slope{
priority_queue<int>point;
int m=0,c=0,ans=0;
int getsz(){return point.size();}
void print(){
vector<int>keep;
while(!point.empty())keep.pb(point.top()),point.pop();
sort(all(keep));
for(auto i:keep)cout<<i<<" ",point.push(i);
cout<<'\n';
}
};
slope dp[mxn+10];
vector<int>adj[mxn+10];
int up[mxn+10];
void merge(int x,int y){
if(dp[x].getsz()<dp[y].getsz())swap(dp[x],dp[y]);
dp[x].m+=dp[y].m;
dp[x].c+=dp[y].c;
while(!dp[y].point.empty())dp[x].point.push(dp[y].point.top()),dp[y].point.pop();
}
void dfs(int cur,int p){
for(auto i:adj[cur])if(i!=p)dfs(i,cur),merge(cur,i);
if(cur>n)dp[cur].point.push(0),dp[cur].point.push(0),dp[cur].m=1,dp[cur].c=0;
while(dp[cur].m>1)dp[cur].m--,dp[cur].c+=dp[cur].point.top(),dp[cur].point.pop();
int a,b;
a=dp[cur].point.top(),dp[cur].point.pop();
b=dp[cur].point.top(),dp[cur].point.pop();
dp[cur].point.push(a+up[cur]),dp[cur].point.push(b+up[cur]);
dp[cur].c-=up[cur];
//shifting
}
int32_t main(){
fastio
cin>>n>>m;
for(int i=2;i<=n+m;i++){
int a,b;cin>>a>>b;
adj[a].pb(i);
up[i]=b;
}
dfs(1,-1);
cout<<dp[1].m*(dp[1].point.top())+dp[1].c;
}
/*
dp[i][x]=sum of dp[child][x];
then we shift dp[i][x]=dp[i][x-k] when k is the cost of parent edge
we can keep the slope chaning point
merge all child small to large and shift the dp by k
*/
컴파일 시 표준 에러 (stderr) 메시지
fireworks.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
fireworks.cpp:39:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
39 | void setIO(string name){
| ^
fireworks.cpp:47:12: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
47 | int getsz(){return point.size();}
| ^
fireworks.cpp:48:13: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
48 | void print(){
| ^
fireworks.cpp:59:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
59 | void merge(int x,int y){
| ^
fireworks.cpp:65:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
65 | void dfs(int cur,int p){
| ^
fireworks.cpp:77:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
77 | int32_t main(){
| ^
fireworks.cpp: In function 'void setIO(std::string)':
fireworks.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fireworks.cpp:42:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |