Submission #588732

# Submission time Handle Problem Language Result Execution time Memory
588732 2022-07-04T01:20:08 Z Bench0310 Islands (IOI08_islands) C++17
90 / 100
871 ms 131072 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N=1000001;
int u[N];
int vidx[N];
int ucost[N];
bool in_cycle[N];
ll dp[N];
ll down[N];
array<int,3> v[2*N];
int vsz=0;
array<int,3> bad_edges[N];
int badsz=0;
int cc[N];
int ccsz=0;
int cycle[N];
int cyclesz=0;

int find_set(int a)
{
    if(a==u[a]) return a;
    else return u[a]=find_set(u[a]);
}

int merge_sets(int a,int b)
{
    a=find_set(a);
    b=find_set(b);
    if(a==b) return 0;
    u[b]=a;
    return 1;
}

void chmax(ll &a,ll b){a=max(a,b);}

void dfs(int a)
{
    cc[ccsz++]=a;
    ll one=0,two=0;
    for(int i=vidx[a];i<vsz&&v[i][0]==a;i++)
    {
        auto [_,to,c]=v[i];
        if(to==u[a]) continue;
        u[to]=a;
        ucost[to]=c;
        dfs(to);
        ll len=c+down[to];
        if(len>one){two=one; one=len;}
        else if(len>two){two=len;}
        chmax(dp[a],dp[to]);
        chmax(down[a],len);
    }
    chmax(dp[a],one+two);
}

void go(int a,int p)
{
    for(int i=vidx[a];i<vsz&&v[i][0]==a;i++)
    {
        auto [_,to,c]=v[i];
        if(to==p||in_cycle[to]) continue;
        go(to,a);
        chmax(down[a],c+down[to]);
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i=1;i<=n;i++) u[i]=i;
    for(int a=1;a<=n;a++)
    {
        int b,c;
        cin >> b >> c;
        if(merge_sets(a,b))
        {
            v[vsz++]={a,b,c};
            v[vsz++]={b,a,c};
        }
        else bad_edges[badsz++]={a,b,c};
    }
    sort(v,v+vsz);
    for(int i=vsz-1;i>=0;i--) vidx[v[i][0]]=i;
    ll res=0;
    memset(u,0,sizeof(u));
    for(int o=0;o<badsz;o++)
    {
        auto [x,y,bad_cost]=bad_edges[o];
        dfs(x);
        ll now=dp[x];
        int t=y;
        while(t!=0)
        {
            cycle[cyclesz++]=t;
            in_cycle[t]=1;
            t=u[t];
        }
        for(int i=0;i<ccsz;i++) down[cc[i]]=dp[cc[i]]=0;
        for(int i=0;i<cyclesz;i++) go(cycle[i],0);
        ll path=0;
        for(int i=0;i<cyclesz;i++)
        {
            if(i>0) dp[cycle[i]]=dp[cycle[i-1]];
            chmax(dp[cycle[i]],path+down[cycle[i]]);
            path+=ucost[cycle[i]];
        }
        path=0;
        ll opt=0;
        for(int i=cyclesz-1;i>=0;i--)
        {
            path+=ucost[cycle[i]];
            chmax(opt,path+down[cycle[i]]);
            if(i>0) chmax(now,dp[cycle[i-1]]+bad_cost+opt);
        }
        res+=now;
        ccsz=cyclesz=0;
    }
    cout << res << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 2 ms 4180 KB Output is correct
5 Correct 2 ms 4308 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 2 ms 4180 KB Output is correct
8 Correct 2 ms 4304 KB Output is correct
9 Correct 2 ms 4180 KB Output is correct
10 Correct 2 ms 4308 KB Output is correct
11 Correct 2 ms 4308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4360 KB Output is correct
2 Correct 3 ms 4308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 4 ms 4692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 5204 KB Output is correct
2 Correct 17 ms 7540 KB Output is correct
3 Correct 11 ms 5380 KB Output is correct
4 Correct 7 ms 4820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 8916 KB Output is correct
2 Correct 32 ms 10884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 16616 KB Output is correct
2 Correct 76 ms 23040 KB Output is correct
3 Correct 93 ms 31900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 144 ms 24944 KB Output is correct
2 Correct 193 ms 49736 KB Output is correct
3 Correct 186 ms 59620 KB Output is correct
4 Correct 245 ms 75372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 407 ms 59464 KB Output is correct
2 Correct 609 ms 104464 KB Output is correct
3 Correct 251 ms 44064 KB Output is correct
4 Correct 388 ms 83484 KB Output is correct
5 Correct 328 ms 84164 KB Output is correct
6 Correct 871 ms 53036 KB Output is correct
7 Correct 372 ms 113812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 315 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -