Submission #715297

# Submission time Handle Problem Language Result Execution time Memory
715297 2023-03-26T11:50:56 Z Ahmed57 Islands (IOI08_islands) C++14
24 / 100
341 ms 89304 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")

using namespace std;
#define int long long
stack<int> st;
vector<vector<int>> all;
vector<pair<int,pair<long long,long long>>> adj[200001];
int tmp[200001];
int vis[200001];
bool ss = 0;
void cycle(int i,int pr){
    st.push(i);
    if(tmp[i]){
        vector<int> cyc;
        cyc.push_back(i);
        st.pop();
        while(st.top()!=i){
            cyc.push_back(st.top());st.pop();
        }
        for(int j = cyc.size()-1;j>=0;j--)st.push(cyc[j]);
        all.push_back(cyc);
        st.pop();
        ss = 1;
        return;
    }
    tmp[i] = 1;
    for(auto j:adj[i]){
        if(j.second.second==pr)continue;
        cycle(j.first,j.second.second);
        if(ss){
            tmp[i] = 0;
            st.pop();
            return ;
        }
    }
    tmp[i] = 0;
    st.pop();
}
void fil(int i){
    vis[i]=1;
    for(auto j:adj[i]){
        if(!vis[j.first]){
            fil(j.first);
        }
    }
}
long long maxDep[200005];
long long TreeDim[200005];
int ele[200005];
long long sst,maa,node;
void ma(int i,int pr,long long dep){
    maxDep[sst] =max(maxDep[sst],dep);
    for(auto j:adj[i]){
        if(j.first==pr||ele[j.first]==1)continue;
        ma(j.first,i,dep+j.second.first);
    }
}
void Dim(int i,int pr,long long dep){
    if(maa<dep){
        maa =dep;
        node = i;
    }
    TreeDim[sst] =max(TreeDim[sst],dep);
    for(auto j:adj[i]){
        if(j.first==pr||ele[j.first]==1)continue;
        Dim(j.first,i,dep+j.second.first);
    }
}
signed main(){
    int n;cin>>n;
    map<pair<int,int>,long long>mp;
    for(int i = 1;i<=n;i++){
        long long a,b;
        cin>>a>>b;
        mp[{i,a}] = b;
        mp[{a,i}] = b;
        adj[i].push_back({a,{b,i}});
        adj[a].push_back({i,{b,i}});
    }
    for(int i = 1;i<=n;i++){
        if(!vis[i]){
            ss = 0;
            cycle(i,-1);
            fil(i);
        }
    }

    for(auto i:all){
        int len = i.size();
        for(int j = 0;j<i.size();j++){
            ele[i[(j+1)%len]] = 1;
            ele[i[((j-1)%len+len)%len]] = 1;
            sst = i[j];maa = -1,node = i[j];
            ma(i[j],-1,0);
            Dim(node,-1,0);
            maa = -1;
            Dim(node,-1,0);
            ele[i[(j+1)%len]] = 0;
            ele[i[((j-1)%len+len)%len]] = 0;
        }
    }
    long long sum = 0;
    long long cost[200001];
    for(auto i:all){
        if(i.size()==2){
            long long ans = max(TreeDim[i[0]],TreeDim[i[1]]);
            long long e = 0;
            for(auto j:adj[i[0]]){
                if(j.first==i[1])e = max(e,j.second.first);
            }
            ans = max(ans,maxDep[i[0]]+maxDep[i[1]]+e);
            sum+=ans;
            continue;
        }
        long long ans = 0;
        for(auto j:i)ans = max(ans,TreeDim[j]);
        //cout<<ans<<endl;
        multiset<pair<long long,int>> s;
        long long sz = mp[{i[0],i[1]}];
        int len = i.size();
        for(int j = 1;j<i.size();j++){
            //cout<<i[j]<<"hh"<<(-sz)+maxDep[i[j]]<<endl;
            s.insert({(-sz)+maxDep[i[j]],i[j]});
            cost[i[j]] = (-sz)+maxDep[i[j]];
            //cout<<sz<<"\n";
            sz+=mp[{i[j],i[(j+1)%len]}];
        }
        auto it = s.end();it--;
        ans = max(ans,maxDep[i[0]]+sz+(*it).first);
        //cout<<(*it).second<<"\n";
        //cout<<ans<<endl;
        //cout<<i[0]<<" "<<i[1]<<"\n";
        long long global = 0;
        for(int j = i.size()-1;j>=1;j--){
            s.insert({(-global),i[(j+1)%len]});
            cost[i[(j+1)%len]] = (-global);
            global-=mp[{i[j],i[(j+1)%len]}];
            pair<long long,int> p = {cost[i[j]],i[j]};
            auto it=s.lower_bound(p);
            s.erase(it);
            it = s.end();it--;
            ans = max(ans,(maxDep[i[j]]+sz+(*it).first)+global);
            //cout<<(maxDep[i[j]]+sz+(*it).first)+global<<endl;
        }
        sum+=ans;
        //cout<<sum<<"\n";
    }
    cout<<sum<<endl;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:91:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         for(int j = 0;j<i.size();j++){
      |                       ~^~~~~~~~~
islands.cpp:122:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |         for(int j = 1;j<i.size();j++){
      |                       ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Incorrect 4 ms 6572 KB Output isn't correct
3 Correct 4 ms 6740 KB Output is correct
4 Correct 3 ms 6488 KB Output is correct
5 Correct 3 ms 6484 KB Output is correct
6 Correct 3 ms 6484 KB Output is correct
7 Incorrect 3 ms 6612 KB Output isn't correct
8 Incorrect 3 ms 6612 KB Output isn't correct
9 Correct 4 ms 6612 KB Output is correct
10 Correct 4 ms 6612 KB Output is correct
11 Correct 4 ms 6612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 6868 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 6996 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 33 ms 10444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 109 ms 21540 KB Output is correct
2 Incorrect 160 ms 33032 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 341 ms 55700 KB Output is correct
2 Runtime error 219 ms 86520 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 218 ms 89180 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 284 ms 89304 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 37 ms 20200 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -