This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pld pair<long double, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define ff first
#define ss second
#define SZ(x) ((int)(x).size())
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 1e6+100;
int N;
vector<pii> adj[MAXN];
void read(){
cin >> N;
for(int i = 0; i < N; i++){
int u, w;
cin >> u >> w;
--u;
adj[i].pb({u, w});
adj[u].pb({i, w});
}
}
bool vis[MAXN], vis2[MAXN];
vector<int> componente;
void dfs(int v){
vis[v] = true;
componente.pb(v);
for(auto u : adj[v]){
if(!vis[u.ff]){
dfs(u.ff);
}
}
}
ll dfs2(int v){
ll ans = 0;
vis2[v] = true;
for(auto u : adj[v]){
if(!vis2[u.ff]){
ans = max(ans, dfs2(u.ff) + u.ss);
}
}
vis2[v] = false;
return ans;
}
void solve(){
read();
ll ans = 0;
for(int i = 0; i < N; i++){
if(!vis[i]){
dfs(i);
ll res = 0;
for(auto v : componente){
res = max(res, dfs2(v));
}
ans += res;
componente.clear();
}
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# | 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... |