답안 #864489

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
864489 2023-10-23T04:42:43 Z HuyQuang_re_Zero Islands (IOI08_islands) C++17
80 / 100
1136 ms 131072 KB
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define N 1000005
#define II pair <int,int>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define fst first
#define snd second
#define BIT(x,i) ((x>>i)&1)
#define Log(x) (31-__builtin_clz((int)x))
#define LogLL(x) (63-__builtin_clzll((ll)x))
#define pi acos(-1)
#define to_radian(x) (x*pi/180.0)
#define to_degree(x) (x*180.0/pi)
using namespace std;
vector <int> a[N];
struct edge { int u,v,k; };
edge e[N];
int tr[N],trc[N],cycle[N],nxt[N];
bool incycle[N],visited[N];
ll n,i,u,v,k,c[N],dis[N],ncycle,sum,res;
const ll oo=round(1e18);
struct Fenwick_Tree
{
    int n;
    vector <ll> bit,d;
    void Init(int _n)
    {
        n=_n;
        bit.resize(n+1);
        d.resize(n+1);
        for(int i=1;i<=n;i++) bit[i]=d[i]=-oo;
    }
    void update(int i,ll k)
    {
        d[i]=max(d[i],k);
        while(i<=n) bit[i]=max(bit[i],k),i+=(i & -i);
    }
    ll get(int l,int r)
    {
        ll res=-oo,i=r;
        while(i>=l)
            if(i-(i & -i)>=l) res=max(res,bit[i]),i-=(i & -i);
            else res=max(res,d[i]),i--;
        return res;
    }
} FT;

void dfs(int u)
{
    visited[u]=1;
    auto consider=[&](int adj)
    {
        int v=(e[adj].u==u ? e[adj].v : e[adj].u);
        if(visited[v]==0) dfs(v);
    };
    for(int adj:a[u]) consider(adj);
    consider(nxt[u]);
}

void Find_cycle(int u,int x)
{
    auto consider=[&](int adj)
    {
        if(adj==x) return;
        int v=v=(e[adj].u==u ? e[adj].v : e[adj].u),k=e[adj].k;
        if(tr[v]==0)
        {
            tr[v]=u; trc[v]=k;
            Find_cycle(v,adj);
        }
        else if(ncycle==0)
        {
            sum=0;
            while(u!=v)
            {
                cycle[++ncycle]=u; incycle[u]=1;
                dis[ncycle]=sum; sum+=trc[u];
                u=tr[u];
            }
            cycle[++ncycle]=u; incycle[u]=1;
            dis[ncycle]=sum; sum+=k;
            return ;
        }
    };
    for(int adj:a[u]) consider(adj);
    consider(nxt[u]);
}


/////////////////////////////////////////////////////////////////////////////
ll cal(int u,int p,ll &ma)
{
    ll res=0,ma0=0,ma1=0;
    auto consider=[&](int adj)
    {
        int v=v=(e[adj].u==u ? e[adj].v : e[adj].u);;
        if(v!=p && incycle[v]==0)
        {
            ll k=cal(v,u,ma)+e[adj].k;
            res=max(res,k);
            if(ma0<k) ma0=k;
            if(ma1<ma0) swap(ma1,ma0);
        }
    };
    for(int adj:a[u]) consider(adj);
    consider(nxt[u]);
    ma=max(ma,ma0+ma1);
    return res;
}
ll Work()
{
    int i,u,v;
    FT.Init(ncycle);
    ll res=0;
    for(i=1;i<=ncycle;i++)
    {
        u=cycle[i];
        c[i]=cal(u,0,res);
        FT.update(i,c[i]-dis[i]);
    }
    for(i=1;i<ncycle;i++)
    {
        int l=i,r=ncycle;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(sum-(dis[mid]-dis[i])>=dis[mid]-dis[i])
                l=mid;
            else r=mid-1;
        }
        res=max(res,c[i]+sum+dis[i]+FT.get(i+1,l));
    }

    FT.Init(ncycle);
    for(i=1;i<=ncycle;i++)
    {
        u=cycle[i];
        FT.update(i,c[i]+dis[i]);
    }
    for(i=1;i<ncycle;i++)
    {
        int l=i,r=ncycle;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(sum-(dis[mid]-dis[i])>=dis[mid]-dis[i])
                l=mid;
            else r=mid-1;
        }
        res=max(res,c[i]-dis[i]+FT.get(l+1,ncycle));
    }
    return res;
}
int main()
{
 //   freopen("islands.inp","r",stdin);
  //  freopen("islands.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>n;
    for(u=1;u<=n;u++)
    {
        cin>>v>>k;
        nxt[u]=u; a[v].push_back(u);
        e[u]={ u,v,k };
    }
    for(u=1;u<=n;u++)
        if(visited[u]==0)
        {
            dfs(u);
            tr[u]=-1;
            ncycle=0;
            Find_cycle(u,0);
            res+=Work();
        }
    cout<<res;
}

Compilation message

islands.cpp: In lambda function:
islands.cpp:67:16: warning: operation on 'v' may be undefined [-Wsequence-point]
   67 |         int v=v=(e[adj].u==u ? e[adj].v : e[adj].u),k=e[adj].k;
      |               ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
islands.cpp: In lambda function:
islands.cpp:98:16: warning: operation on 'v' may be undefined [-Wsequence-point]
   98 |         int v=v=(e[adj].u==u ? e[adj].v : e[adj].u);;
      |               ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
islands.cpp: In function 'long long int Work()':
islands.cpp:114:13: warning: unused variable 'v' [-Wunused-variable]
  114 |     int i,u,v;
      |             ^
islands.cpp: In function 'int main()':
islands.cpp:167:16: warning: narrowing conversion of 'u' from 'long long int' to 'int' [-Wnarrowing]
  167 |         e[u]={ u,v,k };
      |                ^
islands.cpp:167:18: warning: narrowing conversion of 'v' from 'long long int' to 'int' [-Wnarrowing]
  167 |         e[u]={ u,v,k };
      |                  ^
islands.cpp:167:20: warning: narrowing conversion of 'k' from 'long long int' to 'int' [-Wnarrowing]
  167 |         e[u]={ u,v,k };
      |                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 40280 KB Output is correct
2 Correct 7 ms 40028 KB Output is correct
3 Correct 7 ms 39900 KB Output is correct
4 Correct 7 ms 40028 KB Output is correct
5 Correct 7 ms 40028 KB Output is correct
6 Correct 7 ms 40024 KB Output is correct
7 Correct 7 ms 40028 KB Output is correct
8 Correct 7 ms 40028 KB Output is correct
9 Correct 7 ms 40028 KB Output is correct
10 Correct 7 ms 39904 KB Output is correct
11 Correct 8 ms 40028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 40028 KB Output is correct
2 Correct 8 ms 40028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 40024 KB Output is correct
2 Correct 8 ms 40284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 40540 KB Output is correct
2 Correct 21 ms 42096 KB Output is correct
3 Correct 14 ms 40488 KB Output is correct
4 Correct 10 ms 40028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 42844 KB Output is correct
2 Correct 37 ms 43864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 47596 KB Output is correct
2 Correct 91 ms 60172 KB Output is correct
3 Correct 105 ms 65104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 113 ms 58184 KB Output is correct
2 Correct 171 ms 79276 KB Output is correct
3 Correct 277 ms 100672 KB Output is correct
4 Correct 277 ms 110464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 157 ms 58400 KB Output is correct
2 Correct 755 ms 131072 KB Output is correct
3 Correct 210 ms 70284 KB Output is correct
4 Correct 329 ms 107032 KB Output is correct
5 Correct 326 ms 104532 KB Output is correct
6 Correct 1136 ms 73532 KB Output is correct
7 Runtime error 252 ms 131072 KB Execution killed with signal 9
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 239 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -