#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpi;
#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define ff first
#define ss second
const int MOD = 1e9 + 7;
const ll INF = 1e18;
vi p, rnk, setsz;
int n;
int findset(int i){ return p[i] == i? i: findset(p[i]);}
void unite(int i, int j)
{
if(findset(i) == findset(j))
return;
int x= findset(i);
int y = findset(j);
if(rnk[x] > rnk[y])
swap(x,y);
p[x] = y;
if(rnk[x] == rnk[y])
rnk[y] ++;
setsz[y] += setsz[x];
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
vector<pair<ll,pll>> edges;
cin >> n;
int deg[n+1];
memset(deg,0,sizeof(deg));
p.assign(n+1,0);
rnk.assign(n+1,0);
setsz.assign(n+1,1);
for(ll i = 1; i <=n; i ++)
{
p[i-1] = i-1;
ll u, dist;
cin >> u >> dist;
edges.pb({dist,{i,u}});
}
sort(all(edges));
reverse(all(edges));
ll ans = 0;
for(int j = 0; j < n; j ++)
{
ll d = edges[j].ff;
ll u = edges[j].ss.ff - 1;
ll v = edges[j].ss.ss - 1;
if(findset(u) == findset(v))
continue;
else if(deg[u] < 2 && deg[v] < 2)
{
deg[u] ++;
deg[v] ++;
ans += d;
unite(u,v);
}
}
cout << ans << '\n';
}
/*
Sample test case:
7
3 8
7 2
4 2
1 4
1 9
3 4
2 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
1224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
19 ms |
3312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
78 ms |
11656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
169 ms |
22796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
398 ms |
55056 KB |
Output is correct |
2 |
Incorrect |
477 ms |
54872 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
508 ms |
55124 KB |
Output is correct |
2 |
Incorrect |
541 ms |
54980 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |