# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
91823 |
2018-12-30T08:55:07 Z |
easrui |
Islands (IOI08_islands) |
C++14 |
|
965 ms |
132096 KB |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6+5;
struct Edge{
int i,x;
long long d;
Edge(){};
Edge(int a, int b, long long c){i=a;x=b;d=c;};
};
int N;
long long D[MAX],Ans;
bool vE[MAX],vN[MAX],isL[MAX];
vector<Edge> I[MAX];
stack<int> L;
vector<int> Lp;
vector<long long> A,B,C,dis;
void DFS(int i)
{
if(vN[i]){
Lp.push_back(i);
isL[i] = true;
while(L.top()!=i){
Lp.push_back(L.top());
isL[L.top()] = true;
L.pop();
}
return;
}
vN[i] = true;
L.push(i);
for(auto it : I[i]){
if(vE[it.i]) continue;
vE[it.i] = true;
DFS(it.x);
}
if(!L.empty()) L.pop();
}
long long getD(int i, int x, long long d, int r)
{
long long res=d;
if(isL[x]) return 0;
for(auto it : I[x]){
if(it.x==r) continue;
res = max(res,getD(i,it.x,it.d+d,x));
}
return res;
}
long long getL()
{
long long res=0,tmp;
int n = Lp.size();
for(int i=0; i<n; i++)
{
long long subm = 0;
for(auto it : I[Lp[i]]){
if(it.x==Lp[(i+1)%n]) dis.push_back(it.d);
tmp = getD(Lp[i],it.x,it.d,Lp[i]);
if(D[Lp[i]]<=tmp){
subm=D[Lp[i]];
D[Lp[i]]=tmp;
}
else subm = max(subm,tmp);
res = max(res,D[Lp[i]]+subm);
}
}
A.push_back(D[Lp[0]]);
//cout << D[Lp[0]] << D[Lp[1]] << D[Lp[2]];
for(int i=1; i<n; i++) A.push_back(A[i-1]+D[Lp[i]]-D[Lp[i-1]]+dis[i-1]);
for(int i=0; i<n; i++) B.push_back(A[i]-D[Lp[i]]*2);
C.push_back(D[Lp[n-1]]+dis[n-1]);
for(int i=1; i<n-1; i++) C.push_back(C[i-1]+D[Lp[n-1-i]]-D[Lp[n-i]]+dis[n-1-i]);
tmp = B[0];
res = A[1] - B[0];
for(int i=2; i<Lp.size(); i++){
tmp = min(tmp,B[i-1]);
res = max(res,A[i] - tmp);
}
tmp = A[0];
res = max(res,C[n-2] + A[0]);
//cout << res;
for(int i=n-3; i>=0; i--){
tmp = max(tmp,A[n-2-i]);
res = max(res,C[i] + tmp);
}
//cout << C[0];
return res;
}
int main()
{
cin >> N;
for(int i=1; i<=N; i++){
int x;
long long d;
cin >> x >> d;
I[i].push_back(Edge(i,x,d));
I[x].push_back(Edge(i,i,d));
}
for(int j=1; j<=N; j++){
if(vN[j]) continue;
Lp.clear();
A.clear();
B.clear();
C.clear();
dis.clear();
DFS(j);
Ans += getL();
//cout << Ans;
}
cout << Ans;
}
/*7
3 8
7 2
4 2
1 4
1 9
3 4
2 3*/
Compilation message
islands.cpp: In function 'long long int getL()':
islands.cpp:78:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=2; i<Lp.size(); i++){
~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Incorrect |
22 ms |
23800 KB |
Output isn't correct |
3 |
Correct |
23 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
22 ms |
23800 KB |
Output is correct |
6 |
Correct |
22 ms |
23800 KB |
Output is correct |
7 |
Correct |
22 ms |
23800 KB |
Output is correct |
8 |
Correct |
22 ms |
23800 KB |
Output is correct |
9 |
Correct |
22 ms |
23800 KB |
Output is correct |
10 |
Incorrect |
24 ms |
23800 KB |
Output isn't correct |
11 |
Correct |
22 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
26 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
25260 KB |
Output is correct |
2 |
Correct |
56 ms |
28024 KB |
Output is correct |
3 |
Correct |
44 ms |
25464 KB |
Output is correct |
4 |
Incorrect |
33 ms |
24696 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
29600 KB |
Output is correct |
2 |
Correct |
109 ms |
33220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
208 ms |
40292 KB |
Output is correct |
2 |
Correct |
210 ms |
46260 KB |
Output is correct |
3 |
Correct |
284 ms |
56800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
356 ms |
52136 KB |
Output is correct |
2 |
Correct |
440 ms |
76368 KB |
Output is correct |
3 |
Correct |
472 ms |
84140 KB |
Output is correct |
4 |
Correct |
678 ms |
103144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
852 ms |
77204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
965 ms |
132096 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |