#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <utility>
#include <cmath>
#include <numeric>
#include <assert.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 200007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
vector < pair < int,ll > > adj[M];
ll n, ans, grp[M], mx[M], curgroup;
bool vist[M], on[M];
void connect(int node)
{
grp[node] = curgroup;
vist[node] = 1;
for(auto i : adj[node]){
if(vist[i.F]) continue;
connect(i.F);
}
return;
}
ll dfs(int node, ll cost)
{
ll ret = cost;
on[node] = 1;
for(auto i : adj[node]){
if(on[i.F]) continue;
ret = max(ret, dfs(i.F, cost + i.S));
}
on[node] = 0;
return ret;
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
ll x, y;
cin >> x >> y;
adj[x].pb({i, y});
adj[i].pb({x, y});
}
for(int i = 1; i <= n; ++i){
curgroup = i;
if(!grp[i]) connect(i);
mx[grp[i]] = max(mx[grp[i]], dfs(i, 0));
}
for(int i = 1; i <= n; ++i) ans += mx[i];
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
8 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
4996 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
5016 KB |
Output is correct |
7 |
Correct |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5196 KB |
Output is correct |
2 |
Correct |
654 ms |
5452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2048 ms |
6308 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2059 ms |
9936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2043 ms |
19136 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
70 ms |
31008 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
7 ms |
9928 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
16 ms |
12432 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |