Submission #356225

# Submission time Handle Problem Language Result Execution time Memory
356225 2021-01-23T08:18:47 Z achibasadzishvili Islands (IOI08_islands) C++14
50 / 100
151 ms 131076 KB
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define N 1000002
using namespace std;
int n,p[N],l[N],fix[N],F[N],k,c[N];
long long mx[N],ans,pas;
vector<int>v[N];
map<int,int>M[N],ra[N];
vector<int>g,all;
void dfs(int x){
    if(F[x])return;
    all.pb(x);
    F[x] = 1;
    for(int i=0; i<v[x].size(); i++)
        dfs(v[x][i]);
}
void findc(int x,int par){
    if(fix[x]){
        g.pb(x);
        k = 1;
        return;
    }
    g.pb(x);
    fix[x] = 1;
    for(int i=0; i<v[x].size(); i++)
        if(v[x][i] != par || ra[x][v[x][i]] > 1){
            findc(v[x][i] , x);
            if(k)return;
        }
    g.pop_back();
}
void findmx(int x,int par){
    for(int i=0; i<v[x].size(); i++){
        if(v[x][i] != par && !c[v[x][i]]){
            findmx(v[x][i] , x);
            ans = max(ans , mx[x] + mx[v[x][i]] + M[x][v[x][i]]);
            mx[x] = max(mx[x] , mx[v[x][i]] + M[x][v[x][i]]);
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin >> n;
    
    for(int i=1; i<=n; i++){
        cin >> p[i] >> l[i];
        M[i][p[i]] = max(M[i][p[i]] , l[i]);
        M[p[i]][i] = max(M[p[i]][i] , l[i]);
        ra[i][p[i]]++;
        ra[p[i]][i]++;
        v[p[i]].pb(i);
        v[i].pb(p[i]);
    }
    ll cur,cur2,len,pre;
    for(int s=1; s<=n; s++){
        if(F[s])continue;
        ans = 0;
        k = 0;
        all.clear();
        dfs(s);
        g.clear();
        findc(s , 0);
        vector<int>cyc;
        for(int i=0; i<g.size(); i++){
            if(g[i] == g[(int)g.size() - 1]){
                for(int j=i+1; j<g.size(); j++){
                    cyc.pb(g[j]);
                    c[g[j]] = 1;
                }
                break;
            }
        }
        len = 0;
        for(int i=0; i<cyc.size(); i++){
            findmx(cyc[i] , 0);
            if(i)len += M[cyc[i]][cyc[i - 1]];
        }
        len += M[cyc[0]][cyc[cyc.size() - 1]];
        cur = mx[cyc[0]];
        cur2 = mx[cyc[0]];
        pre = 0;
        for(int i=1; i<cyc.size(); i++){
            pre += M[cyc[i]][cyc[i - 1]];
            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:17:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |     for(int i=0; i<v[x].size(); i++)
      |                  ~^~~~~~~~~~~~
islands.cpp: In function 'void findc(int, int)':
islands.cpp:28:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     for(int i=0; i<v[x].size(); i++)
      |                  ~^~~~~~~~~~~~
islands.cpp: In function 'void findmx(int, int)':
islands.cpp:36:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for(int i=0; i<v[x].size(); i++){
      |                  ~^~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |         for(int i=0; i<g.size(); i++){
      |                      ~^~~~~~~~~
islands.cpp:69:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |                 for(int j=i+1; j<g.size(); j++){
      |                                ~^~~~~~~~~
islands.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |         for(int i=0; i<cyc.size(); i++){
      |                      ~^~~~~~~~~~~
islands.cpp:85:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for(int i=1; i<cyc.size(); i++){
      |                      ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 79 ms 117868 KB Output is correct
2 Correct 80 ms 117868 KB Output is correct
3 Correct 80 ms 117996 KB Output is correct
4 Correct 75 ms 117868 KB Output is correct
5 Correct 78 ms 117740 KB Output is correct
6 Correct 78 ms 117740 KB Output is correct
7 Correct 80 ms 117740 KB Output is correct
8 Correct 75 ms 117868 KB Output is correct
9 Correct 72 ms 117868 KB Output is correct
10 Correct 73 ms 117868 KB Output is correct
11 Correct 82 ms 117868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 118124 KB Output is correct
2 Correct 80 ms 117996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 118252 KB Output is correct
2 Correct 86 ms 119020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 121708 KB Output is correct
2 Correct 151 ms 128108 KB Output is correct
3 Correct 107 ms 123204 KB Output is correct
4 Correct 85 ms 120428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 142 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 136 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 104 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 121 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 123 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -