이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <queue>
#include <assert.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pll;
ll N;
ll x,y;
vector<pll> grafo[1000005];
ll F[1000005];
ll H[1000005];
ll W[1000005];
bool V[1000005];
bool C[1000005];
ll res,ans;
ll s,c,l,r;
priority_queue<pll, vector<pll>, greater<pll>> PQ;
int VAL(int k){
V[k]=true;
ll a=0;
for(pll f:grafo[k])
if(!V[f.first] and !C[f.first])
a+=f.second+VAL(f.first);
return a;
}
void DFS(int k, int g){
if(k==x)
if(c++>1)
return;
ll p=H[k];
while(PQ.top().second+l<=r)
PQ.pop();
ll d=PQ.top().first;
ans=max(ans,s+p-d);
PQ.push({s-p,r});
d=min(d,s-p);
r++;
for(pll f:grafo[k])
if(C[f.first] and f.first!=g){
s+=f.second;
//cout<<"DFS "<<k<<" "<<s<<" "<<p<<" "<<d<<" "<<c<<endl;
DFS(f.first,k);
return;
}
}
int main(){
cin>>N;
for(int i=0;i<N;i++){
cin>>x>>y;
x--;
F[i]=x;
W[i]=y;
grafo[i].push_back({x,y});
grafo[x].push_back({i,y});
}
for(int i=0;i<N;i++)
if(!V[i]){
x=y=i;
l=0;
do{
x=F[x];
y=F[F[y]];
}while(x!=y);
do{
C[x]=true;
x=F[x];
l++;
}while(x!=y);
do{
H[x]=VAL(x);
x=F[x];
}while(x!=y);
r=0;
s=0;
c=0;
res=0;
while(!PQ.empty())
PQ.pop();
PQ.push({1e18,1e18});
if(l==2)
res=max(W[x],W[F[x]])+H[x]+H[F[x]];
else
DFS(x,-1);
ans+=res;
}
cout<<ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |