/* made by amunduzbaev */
#include "bits/stdc++.h"
using namespace std;
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template<class T> using oset = tree<T,
//~ null_type, less_equal<T>, rb_tree_tag,
//~ tree_order_statistics_node_update>;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vv vector
#define mem(arr, v) memset(arr, v, sizeof arr)
#define int long long
#define degub(x) cout<<#x<<" : "<<x<<"\n"
#define GG cout<<"here"<<endl;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vii;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T> bool umax(T& a, const T& b) { return a < b ? a = b, true : false; }
template<int sz> using tut = array<int, sz>;
void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
const int N = 2e5+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
#define MULTI 0
int n, m, k, t, q, ans, res, a[N];
int par[N], sz[N], used[N];
vpii edges[N];
int f(int x) { return (par[x] == x ? x : par[x] = f(par[x])); }
void merge(int a, int b, int c){
if(f(a) == f(b)) return;
edges[a].pb({b, c}), edges[b].pb({a, c});
a = f(a), b = f(b);
if(sz[b] > sz[a]) swap(a, b);
sz[a] += sz[b], par[b] = a;
}
vii tt;
pii dfs(int u){
used[u] = 1;
pii mx = {0, u};
tt.pb(u);
for(auto x : edges[u]){
if(used[x.ff]) continue;
pii tt = dfs(x.ff);
if(umax(mx.ff, tt.ff + x.ss)) mx.ss = tt.ss;
} return mx;
}
void solve(int t_case){
cin>>n;
vv<tut<3>> ee;
for(int i=1;i<=n;i++){
int a, b; cin>>a>>b;
ee.pb({b, a, i});
}
sort(rall(ee));
//~ for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) par[i] = i;
for(auto x : ee){
int a = x[1], b = x[2], c = x[0];
merge(a, b, c);
}
//~ for(int i=1;i<=n;i++){
//~ for(auto x : edges[i]) cout<<i<<" "<<x.ff<<" "<<x.ss<<"\n";
//~ }
for(int i=1;i<=n;i++){
if(used[i]) continue;
tt.clear();
int a = dfs(i).ss;
for(auto x : tt) used[x] = 0;
res += dfs(a).ff;
}
cout<<res<<"\n";
}
signed main(){
NeedForSpeed
if(MULTI){
int t; cin>>t;
for(int t_case = 1; t_case <= t; t_case++) solve(t_case);
} else solve(1);
return 0;
}
Compilation message
islands.cpp: In function 'void usaco(std::string)':
islands.cpp:38:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
islands.cpp:39:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5012 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
5200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
6728 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
13576 KB |
Output is correct |
2 |
Incorrect |
107 ms |
18444 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
33420 KB |
Output is correct |
2 |
Runtime error |
143 ms |
49236 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
193 ms |
39304 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
466 ms |
79732 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
428 ms |
79136 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |