# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1036275 |
2024-07-27T07:59:54 Z |
12345678 |
Islands (IOI08_islands) |
C++17 |
|
2000 ms |
131072 KB |
#include <bits/stdc++.h>
using namespace std;
const int nx=1e6+5;
#define ll long long
int vs[nx], n, x, cnt, rt, df=-1;
ll w, mx[nx], pw[nx], ans;
vector<tuple<int, int, ll>> d[nx];
vector<pair<int, ll>> c;
bool dfs(int &u, int &pidx)
{
vs[u]=1;
for (auto [v, idx, w]:d[u])
{
if (idx==pidx) continue;
if (!vs[v])
{
pw[v]=w;
//cout<<"here "<<u<<' '<<v<<' '<<w<<'\n',
if (dfs(v, u)) vs[u]=2;
}
else if (vs[v]==1) pw[v]=w, vs[u]=2, rt=v;
}
if (vs[u]==2) c.push_back({u, pw[u]});
if (vs[u]==2&&u!=rt) return 1;
return 0;
}
void dfs2(int u, int pidx)
{
for (auto [v, idx, w]:d[u]) if (idx!=pidx&&vs[v]==1) dfs2(v, idx), mx[u]=max(mx[u], mx[v]+w);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>x>>w, d[i].push_back({x, i, w}), d[x].push_back({i, i, w});
for (int i=1; i<=n; i++)
{
if (!vs[i])
{
c.clear();
dfs(i, df);
for (auto [u, p]:c) dfs2(u, df);
reverse(c.begin(), c.end());
for (int i=2; i<c.size(); i++) c[i].second+=c[i-1].second;
ll sm=c[0].second+c[c.size()-1].second, tmp=0;
c[0].second=0;
for (int j=0; j<c.size(); j++)
{
for (int k=j+1; k<c.size(); k++)
{
tmp=max(tmp, mx[c[j].first]+mx[c[k].first]+max(c[k].second-c[j].second, sm-(c[k].second-c[j].second)));
}
}
ans+=tmp;
}
}
cout<<ans;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:51:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i=2; i<c.size(); i++) c[i].second+=c[i-1].second;
| ~^~~~~~~~~
islands.cpp:54:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int j=0; j<c.size(); j++)
| ~^~~~~~~~~
islands.cpp:56:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int k=j+1; k<c.size(); k++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Incorrect |
10 ms |
23900 KB |
Output isn't correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Incorrect |
10 ms |
23792 KB |
Output isn't correct |
5 |
Correct |
11 ms |
23964 KB |
Output is correct |
6 |
Correct |
10 ms |
23896 KB |
Output is correct |
7 |
Correct |
10 ms |
23900 KB |
Output is correct |
8 |
Incorrect |
10 ms |
23900 KB |
Output isn't correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Incorrect |
11 ms |
23896 KB |
Output isn't correct |
11 |
Correct |
10 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
23900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
23900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
25436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
755 ms |
30660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2028 ms |
44484 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1490 ms |
58456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
198 ms |
97940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
226 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |