이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
///Input format is not always nice, first nodes may not be in the keychain ring
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll, int> li;
int n;
vector<ii> G[1000005];
li P[1000005];
int Pct;
int incyc[1000005];
int vis[1000005];
ll ANS;
ll ans;
int cyclehead;
void dfs(int u, int p){
vis[u] = 2;
int join =0;
int ct = 0;
for (auto v: G[u]){
if (vis[v.fi] == 2){
if (v.fi == p){
if (ct == 1){
cyclehead = p;
//printf("%d %d repeated\n",u,v.fi);
incyc[u] = 1;
join = v.se;
}
else {
ct++;
}
continue;
}
cyclehead = v.fi;
incyc[u] = 1;
join = v.se;
continue;
}
if (vis[v.fi]) continue;
dfs(v.fi,u);
if (incyc[v.fi] && v.fi != cyclehead) {
incyc[u] = 1;
join = v.se;
continue;
}
}
if (incyc[u]){
//printf("at %d : %d\n",u,join);
P[Pct++] = {u,join};
}
vis[u] = 1;
}
ll dfs2(int u, int p){
ll d2 = 0;
ll d = 0;
for (auto v: G[u]){
if (v.fi == p || incyc[v.fi]) continue;
ll nd = dfs2(v.fi,u)+v.se;
if (d <= nd){
d2 = d;
d = nd;
}
else d2 = max(d2,nd);
}
ans = max(ans,d2+d);
return d;
}
ll solve(int root){
ans = Pct = 0;
dfs(root,-1);
//printf("at %d\n",root);
reverse(P,P+Pct);
for (int i = 0; i < Pct; i++){
P[i].fi = dfs2(P[i].fi,-1);
//printf("%d: %d %d\n",x.fi,d[x.fi],x.se);
}
if (Pct == 2){
return max(ans,P[0].fi+P[1].fi+max(P[0].se,P[1].se));
}
ll S = 0;
ll Sl = 0;
for (int i = 0; i < Pct; i++){
S += P[i].se;
}
ll MX = P[0].fi;
ll MX2 = P[0].fi;
for (int i = 1; i < Pct; i++){
ll x = P[i].fi;
//printf("at %lld %lld\n",d[x],Sl[x]);
//printf("MX: %lld + %lld\n",MX,d[x] + Sl[x]);
//printf("MX2: %lld + %lld\n",MX2,S + d[x] - Sl[x]);
Sl += P[i-1].se;
ans = max(ans, x + Sl + MX);
ans = max(ans, S + x - Sl + MX2);
MX = max(MX, x - Sl);
MX2 = max(MX2, x + Sl);
}
//printf("%lld max\n",ans);
return ans;
}
int main(){
scanf("%d",&n);
for (int i = 1; i <= n; i++){
int j,L;
scanf("%d%d",&j,&L);
G[i].push_back({j,L});
G[j].push_back({i,L});
}
for (int i = 1; i <= n; i++){
if (vis[i] == 0){
ANS += solve(i);
}
}
printf("%lld",ANS);
}
컴파일 시 표준 에러 (stderr) 메시지
islands.cpp: In function 'int main()':
islands.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
106 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
islands.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
109 | scanf("%d%d",&j,&L);
| ~~~~~^~~~~~~~~~~~~~
# | 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... |