Submission #356279

# Submission time Handle Problem Language Result Execution time Memory
356279 2021-01-23T09:01:52 Z achibasadzishvili Islands (IOI08_islands) C++17
90 / 100
1281 ms 131076 KB
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define N 1000002
using namespace std;
int n,p[N],l[N],k,c[N];
bool fix[N],F[N];
long long len;
long long mx[N],ans,pas;
vector<pair<int,int> >v[N];
int g[N],dis[N],szg,szdis,d[N],cyc[N],szd,szc;
void dfs(int x){
    if(F[x])return;
    F[x] = 1;
    for(int i=0; i<v[x].size(); i++)
        dfs(v[x][i].f);
}
void findc(int x,int par){
    if(fix[x]){
        g[szg] = x;
        dis[szdis] = l[par];
        szg++;
        szdis++;
        k = 1;
        return;
    }
    g[szg] = (x);
    szg++;
    fix[x] = 1;
    for(int i=0; i<v[x].size(); i++)
        if(v[x][i].s != par){
            dis[szdis] = (l[v[x][i].s]);szdis++;
            findc(v[x][i].f , v[x][i].s);
            if(k)return;
            szdis--;
        }
    szg--;
}
void findmx(int x,int par){
    for(int i=0; i<v[x].size(); i++){
        if(v[x][i].s != par && !c[v[x][i].f]){
            findmx(v[x][i].f , v[x][i].s);
            ans = max(ans , mx[x] + mx[v[x][i].f] + l[v[x][i].s]);
            mx[x] = max(mx[x] , mx[v[x][i].f] + l[v[x][i].s]);
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin >> n;
    
    for(int i=1; i<=n; i++){
        cin >> p[i] >> l[i];
        v[p[i]].pb(mp(i , i));
        v[i].pb(mp(p[i] , i));
    }
    ll cur,cur2,pre;
    for(int s=1; s<=n; s++){
        if(F[s])continue;
        len = 0;
        ans = 0;
        k = 0;
        dfs(s);
        szg = 0;
        szdis = 0;
        findc(s , 0);
        szc = 0;
        szd = 0;
        for(int i=0; i<szg; i++){
            if(g[i] == g[szg - 1]){
                for(int j=i+1; j<szg; j++){
                    cyc[szc] = (g[j]);szc++;
                    d[szd] = (dis[j - 1]);szd++;
                    c[g[j]] = 1;
                }
                break;
            }
        }
        for(int i=0; i<szc; i++){
            findmx(cyc[i] , 0);
            len += d[i];
        }
        cur = mx[cyc[0]];
        cur2 = mx[cyc[0]];
        pre = 0;
        for(int i=1; i<szc; i++){
            pre += d[i];
            ans = max(ans , cur + mx[cyc[i]] + pre);
            ans = max(ans , cur2 + len + mx[cyc[i]] - pre);
            cur = max(cur , mx[cyc[i]] - pre);
            cur2 = max(cur2 , mx[cyc[i]] + pre);
        }
        pas += ans;
    }
    
    cout << pas << '\n';
    return 0;
}

Compilation message

islands.cpp: In function 'void dfs(int)':
islands.cpp:18:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for(int i=0; i<v[x].size(); i++)
      |                  ~^~~~~~~~~~~~
islands.cpp: In function 'void findc(int, int)':
islands.cpp:33:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for(int i=0; i<v[x].size(); i++)
      |                  ~^~~~~~~~~~~~
islands.cpp: In function 'void findmx(int, int)':
islands.cpp:43:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for(int i=0; i<v[x].size(); i++){
      |                  ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
3 Correct 16 ms 23916 KB Output is correct
4 Correct 16 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 16 ms 23916 KB Output is correct
7 Correct 16 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 19 ms 23916 KB Output is correct
10 Correct 18 ms 23916 KB Output is correct
11 Correct 16 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24172 KB Output is correct
2 Correct 17 ms 24300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 24940 KB Output is correct
2 Correct 35 ms 26988 KB Output is correct
3 Correct 27 ms 25184 KB Output is correct
4 Correct 21 ms 24428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 28140 KB Output is correct
2 Correct 51 ms 30572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 36928 KB Output is correct
2 Correct 102 ms 39404 KB Output is correct
3 Correct 133 ms 48108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 47084 KB Output is correct
2 Correct 212 ms 66156 KB Output is correct
3 Correct 228 ms 67692 KB Output is correct
4 Correct 276 ms 85740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 364 ms 72520 KB Output is correct
2 Correct 842 ms 108012 KB Output is correct
3 Correct 312 ms 70892 KB Output is correct
4 Correct 400 ms 101612 KB Output is correct
5 Correct 395 ms 102176 KB Output is correct
6 Correct 1281 ms 77904 KB Output is correct
7 Correct 439 ms 131072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 473 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -