# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
889513 |
2023-12-19T21:03:06 Z |
Macker |
Islands (IOI08_islands) |
C++17 |
|
233 ms |
131072 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()
struct node{
vector<pair<int, int>> adj = {};
vector<int> binJmp;
vector<int> binDp;
vector<int> binSum;
bool isCyc = 0;
int mxd = 0;
int vis = 0;
};
vector<node> v;
vector<vector<int>> cycles;
int precDfs(int a, int p){
v[a].vis = 1;
int cyc = -1;
for (auto &i : v[a].adj) {
int b = i.second;
if(!v[b].vis){
cyc = max(cyc, precDfs(b, a));
}
else if(v[b].vis && b != p){
cyc = b;
}
}
if(cyc != -1){
v[a].isCyc = 1;
cycles.back().push_back(a);
}
if(cyc == a) return -1;
return cyc;
}
int ddfs(int a){
v[a].vis = 2;
int mxd = 0;
for (auto &i : v[a].adj) {
node b = v[i.second];
if(b.vis < 2 && !b.isCyc){
mxd = max(mxd, ddfs(i.second) + i.first);
}
}
v[a].mxd = mxd;
return mxd;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
v.resize(n);
for (int i = 0; i < n; i++) {
int a, l; cin >> a >> l; a--;
v[i].binDp.push_back(l);
v[i].binSum.push_back(l);
v[i].binJmp.push_back(a);
v[i].adj.push_back({l, a});
v[a].adj.push_back({l, i});
}
for (int i = 0; i < n; i++) sort(all(v[i].adj), greater<pair<int, int>>());
for (int i = 0; i < n; i++) {
v[i].binJmp.resize(20);
v[i].binSum.resize(20);
v[i].binDp.resize(20);
if(!v[i].vis){
cycles.push_back({});
precDfs(i, i);
}
}
for (int i = 0; i < n; i++) {
if(v[i].vis < 2 && v[i].isCyc){
ddfs(i);
}
}
int res = 0;
for (int i = 0; i < cycles.size(); i++) {
for (int j = 0; j < cycles[i].size(); j++) {
int a = cycles[i][j];
v[a].binDp[0] += v[v[a].binJmp[0]].mxd;
}
for (int k = 1; k < 20; k++) {
for (int j = 0; j < cycles[i].size(); j++) {
int a = cycles[i][j];
int b = v[a].binJmp[k - 1];
v[a].binSum[k] = v[a].binSum[k - 1] + v[b].binSum[k - 1];
v[a].binJmp[k] = v[b].binJmp[k - 1];
int x = v[a].binDp[k - 1];
int y = v[a].binSum[k - 1] + v[b].binDp[k - 1];
v[a].binDp[k] = max(x, y);
}
}
int cmx = 0;
int mxJmp = cycles[i].size() - 1;
for (int k = 0; k < cycles[i].size(); k++) {
int dp = 0;
int cur = cycles[i][k];
int s = 0;
for (int j = 19; j >= 0; j--) {
if(mxJmp & (1 << j)){
dp = max(dp, s + v[cur].binDp[j]);
cur = v[cur].binJmp[j];
s += v[cur].binSum[j];
}
}
cmx = max(cmx, dp + v[cycles[i][k]].mxd);
}
res += cmx;
}
cout << res << endl;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int i = 0; i < cycles.size(); i++) {
| ~~^~~~~~~~~~~~~~~
islands.cpp:87:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int j = 0; j < cycles[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~
islands.cpp:92:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int j = 0; j < cycles[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~
islands.cpp:104:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for (int k = 0; k < cycles[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1368 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
7516 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
68 ms |
25480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
233 ms |
94036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
206 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
84 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
88 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |