이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
int n;
vector<ii> G[1000005];
vector<ii> P;
int incyc[1000005];
int vis[1000005];
ll d[1000005];
ll Sl[1000005];
ll ANS;
ll ans;
void dfs(int u, int p){
vis[u] = 2;
int join =0;
ll d2 = 0;
int ct = 0;
for (auto v: G[u]){
if (vis[v.fi] == 2){
if (v.fi == p){
if (ct == 1){
//printf("%d %d repeated\n",u,v.fi);
incyc[u] = 1;
join = v.se;
}
else {
ct++;
}
continue;
}
incyc[u] = 1;
join = v.se;
continue;
}
if (vis[v.fi]) continue;
dfs(v.fi,u);
if (incyc[v.fi]) {
incyc[u] = 1;
join = v.se;
continue;
}
ll nd = d[v.fi]+v.se;
if (d[u] <= nd){
d2 = d[u];
d[u] = nd;
}
else d2 = max(d2,nd);
}
ans = max(ans,d2+d[u]);
if (incyc[u]){
//printf("at %d : %d\n",u,join);
P.push_back({u,join});
}
vis[u] = 1;
}
ll solve(int root){
ans = 0;
P.clear();
dfs(root,-1);
//printf("at %d\n",root);
if (P.size() == 2){
return max(ans,d[P[0].fi]+d[P[1].fi]+max(P[0].se,P[1].se));
}
reverse(P.begin(),P.end());
for (auto x : P){
//printf("%d: %d %d\n",x.fi,d[x.fi],x.se);
}
ll S = P[0].se;
Sl[P[0].fi] = 0;
for (int i = 1; i < P.size(); i++){
Sl[P[i].fi] = Sl[P[i-1].fi]+P[i-1].se;
S += P[i].se;
}
ll MX = d[P[0].fi];
ll MX2 = d[P[0].fi];
for (int i = 1; i < P.size(); i++){
int 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]);
ans = max(ans, d[x] + Sl[x] + MX);
ans = max(ans, S + d[x] - Sl[x] + MX2);
MX = max(MX, d[x] - Sl[x]);
MX2 = max(MX2, d[x] + Sl[x]);
}
//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 'll solve(int)':
islands.cpp:69:15: warning: variable 'x' set but not used [-Wunused-but-set-variable]
for (auto x : P){
^
islands.cpp:74:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < P.size(); i++){
~~^~~~~~~~~~
islands.cpp:80:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < P.size(); i++){
~~^~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
islands.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
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... |