#include <bits/stdc++.h>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e18
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// input
template<class T> void IN(T& x) {cin >> x;}
template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); }
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
// dp
template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;}
template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;}
void program();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
program();
}
//===================//
// begin program //
//===================//
const int MX = 1e6+10;
const int N = (1<<20);
int n;
ll a[MX], l[MX];
vi adj[MX];
vii adjW[MX];
// finding cycles
bitset<MX> visited;
int cycleStart;
vii cycle;
bool dfsVisit(int u, int p) {
if(visited[u]) {
cycleStart = u;
return true;
}
bool res = false;
visited[u] = 1;
int cnt = 0;
FOR(v,adjW[u]) {
if(v.fi != p || cnt) {
if(dfsVisit(v.fi, u)) {
cycle.pb({v.fi,v.se});
if(u != cycleStart)
res = true;
}
} else {
cnt++;
}
}
return res;
}
// finding depth
int dfsDepth(int u, int p, int i1, int i2) {
int res = 0;
FOR(v,adjW[u]) {
if(v.fi == p) continue;
if(v.fi == i1) continue;
if(v.fi == i2) continue;
res = max(res, v.se + dfsDepth(v.fi,u,i1,i2));
}
return res;
}
void program() {
IN(n);
RE1(i,n) IN(a[i],l[i]);
RE1(i,n) {
adj [i ].pb(a[i]);
adj [a[i]].pb(i);
adjW[i ].pb({a[i],l[i]});
adjW[a[i]].pb({i ,l[i]});
}
ll ans = 0;
RE1(i,n) {
if(visited[i]) continue;
cycle.clear();
dfsVisit(i,-1);
cycle.pop_back();
vll a, b, c; b.pb(0);
ll m = cycle.size(), totB = 0;
ll curBest = 0;
FOR(u,cycle) a.pb(u.fi), b.pb(u.se);
RE(i,m) c.pb(dfsDepth(a[i], -1, a[(i+1)%m], a[(i-1+m)%m]));
FOR(i,b) totB += i;
RE(i,m) b[i+1] += b[i];
RE(i,m) REP(j,i+1,m) {
curBest = max(curBest, b[j]-b[i]+c[i]+c[j]);
curBest = max(curBest, totB-b[j]+b[i]+c[i]+c[j]);
}
ans += curBest;
}
OUTL(ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
47312 KB |
Output is correct |
2 |
Incorrect |
33 ms |
47304 KB |
Output isn't correct |
3 |
Correct |
35 ms |
47312 KB |
Output is correct |
4 |
Incorrect |
32 ms |
47308 KB |
Output isn't correct |
5 |
Correct |
34 ms |
47308 KB |
Output is correct |
6 |
Incorrect |
34 ms |
47288 KB |
Output isn't correct |
7 |
Incorrect |
31 ms |
47300 KB |
Output isn't correct |
8 |
Incorrect |
31 ms |
47264 KB |
Output isn't correct |
9 |
Incorrect |
32 ms |
47288 KB |
Output isn't correct |
10 |
Incorrect |
30 ms |
47388 KB |
Output isn't correct |
11 |
Correct |
31 ms |
47276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
47428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
47488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
55 ms |
48996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
494 ms |
54968 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
606 ms |
69500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1698 ms |
87936 KB |
Output is correct |
2 |
Execution timed out |
2074 ms |
122536 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
354 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
417 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |