이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#define MAXN 400007
using namespace std;
struct event{
int par;
long long s;
int minw;
};
int n,parent[MAXN],to[MAXN],last,comp[MAXN],x;
int win[MAXN],lose[MAXN],k;
vector<int> v[MAXN];
long long curr,dist[MAXN],pref,minloops,tocomp[MAXN];
int pos[MAXN];
pair<int,int> dp[MAXN][20];
bool li[MAXN][20];
pair<int,int> ff(int x,int p){
if(x==0)return {n,0};
if(p==0)return {parent[x],win[parent[x]]};
if(li[x][p])return dp[x][p];
li[x][p]=true;
dp[x][p].first=ff(ff(x,p-1).first,p-1).first;
dp[x][p].second=max( ff(x,p-1).second , ff(ff(x,p-1).first,p-1).second );
return dp[x][p];
}
event dp2[MAXN][30];
bool li2[MAXN][30];
event ff2(int x,int p){
if(p==0)return {to[x],lose[x],win[x]};
if(li2[x][p])return dp2[x][p];
li2[x][p]=true;
dp2[x][p].par=ff2(ff2(x,p-1).par,p-1).par;
dp2[x][p].s=ff2(x,p-1).s + ff2(ff2(x,p-1).par,p-1).s;
dp2[x][p].minw=min( ff2(x,p-1).minw , ff2(ff2(x,p-1).par,p-1).minw );
return dp2[x][p];
}
void dfs(int x,long long len){
len+=win[x]; dist[x]=len;
for(int i=0;i<v[x].size();i++){
dfs(v[x][i],len);
}
}
bool in[MAXN],dali,incyc[MAXN];
vector<int> cycle[MAXN];
long long sum[MAXN];
int u[MAXN],tim,minw[MAXN];
void init(int N,vector<int> s,vector<int> p,vector<int> w,vector<int> l){
n=N;
for(int i=0;i<n;i++){
parent[i]=w[i];
v[parent[i]].push_back(i);
to[i]=l[i];
win[i]=s[i]; lose[i]=p[i];
}
parent[n]=n; to[n]=n;
dfs(n,0);
for(int i=0;i<n;i++){
if(u[i]!=0)continue;
tim++; x=i;
while(true){
u[x]=tim; x=to[x];
if(u[x]>0 and u[x]!=tim)break;
if(u[x]==tim){
last=x; k++; comp[x]=k; sum[k]=lose[x];
cycle[k].push_back({x}); incyc[x]=true; x=to[x];
while(x!=last){
cycle[k].push_back(x); comp[x]=k;
sum[k]+=lose[x]; u[x]=tim; incyc[x]=true;
x=to[x];
}
break;
}
}
}
for(int i=0;i<=k;i++){
minw[i]=100000000;
for(int f=0;f<cycle[i].size();f++){
pos[cycle[i][f]]=f;
minw[i]=min(minw[i],win[cycle[i][f]]);
}
}
}
long long simulate(int x, int z){
curr=z;
while(x!=n){
if(curr<win[x]){
for(int i=29;i>=0;i--){
if(curr+ff2(x,i).s<ff2(x,i).minw){
curr+=ff2(x,i).s;
x=ff2(x,i).par;
}
}
while(curr<win[x]){
curr+=lose[x]; x=to[x];
}
}
last=x;
for(int i=19;i>=0;i--){
if(ff(x,i).first!=n and ff(x,i).second<=curr){
x=ff(x,i).first;
}
}
curr+=dist[last]-dist[parent[x]];
x=parent[x];
}
return curr;
}
/*
int main(){
init(3, {2, 6, 9}, {3, 1, 2}, {2, 2, 3}, {1, 0, 1});
cout<<simulate(0,1)<<"\n";
cout<<simulate(2,3)<<"\n";
}
*/
컴파일 시 표준 에러 (stderr) 메시지
dungeons.cpp: In function 'void dfs(int, long long int)':
dungeons.cpp:53:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
dungeons.cpp: In function 'void init(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
dungeons.cpp:100:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int f=0;f<cycle[i].size();f++){
| ~^~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |