이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define N 1000001
vector<pair<int, long long> > adj[N];
bool in[N];
vector<int> s;
vector<int> cyc;
vector<long long> dp;
bool inCyc[N];
long long res;
long long dfs(int v, int p){
long long b1 = 0, b2 = 0;
for(auto xx : adj[v]){
int x = xx.first;
long long c = xx.second;
if(inCyc[x] || p == x) continue;
auto q = dfs(x, v);
long long up;
up = q + c;
if(b1 < up){
b2 = b1;
b1 = up;
} else b2 = max(b2, up);
}
res = max(res, b1 + b2);
return b1;
}
long long ans = 0;
unordered_map<int, unordered_map<int, long long> > lol;
vector<long long> p;
vector<int> cyc2;
vector<long long> dp2, p2;
void solve(){
dp.clear();
p.clear();
p2.clear();
cyc.clear();
cyc2.clear();
dp2.clear();
res = 0;
for(int x : s){
if(inCyc[x]) {
p.push_back(p.empty() ? 0 : p.back() + lol[x][cyc.back()]);
cyc.push_back(x);
dp.push_back(dfs(x, 0));
}
}
long long full = p.back() + lol[cyc[0]][cyc.back()];
long long tmp = dp[0] - p[0];
for(int i = 1; i < cyc.size(); i++){
res = max(res, tmp + dp[i] + p[i]);
tmp = max(tmp, dp[i] - p[i]);
}
cyc2.push_back(cyc[0]);
dp2.push_back(dp[0]);
p2.push_back(0);
for(int i = cyc.size() - 1; i > 0; i--){
p2.push_back(p2.back() + lol[cyc[i]][cyc2.back()]);
cyc2.push_back(cyc[i]);
dp2.push_back(dp[i]);
}
tmp = dp2[0] - p2[0];
for(int i = 1; i < cyc.size(); i++){
res = max(res, tmp + dp2[i] + p2[i]);
tmp = max(tmp, dp2[i] - p2[i]);
}
ans += res;
}
int trav(int i, int p){
s.push_back(i);
in[i] = 1;
int res = 0;
bool ok = 0;
for(auto xx : adj[i]){
int x = xx.first;
if(p == x && ok == 0){
ok = 1;
continue;
}
if(in[x]){
res = x;
continue;
}
int t = trav(x, i);
if(res < t)res = t;
}
if(res) inCyc[i] = 1;
if(res == i) return 0;
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i = 1; i <= n; i++){
int e;
long long l;
cin>>e>>l;
adj[e].push_back({i, l});
adj[i].push_back({e, l});
lol[e][i] = max(lol[e][i], l);
lol[i][e] = max(lol[i][e], l);
}
for(int i = 1; i <= n; i++){
if(in[i]) continue;
s.clear();
trav(i, 0);
solve();
}
cout<<ans;
}
컴파일 시 표준 에러 (stderr) 메시지
islands.cpp: In function 'void solve()':
islands.cpp:55:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < cyc.size(); i++){
~~^~~~~~~~~~~~
islands.cpp:69:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < cyc.size(); i++){
~~^~~~~~~~~~~~
islands.cpp:53:12: warning: unused variable 'full' [-Wunused-variable]
long long full = p.back() + lol[cyc[0]][cyc.back()];
^~~~
# | 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... |