제출 #262259

#제출 시각아이디문제언어결과실행 시간메모리
262259dantoh000Islands (IOI08_islands)C++14
21 / 100
784 ms131076 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
///Input format is not always nice, first nodes may not be in the keychain ring
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll, int> li;
int n;
vector<ii> G[1000005];
ll P[1000005];
int ord[1000005];
int a[1000005], L[1000005];
int D[1000005];
int Pct;
bool vis[1000005];
bool incyc[1000005];
ll ANS;
ll ans;
int cyclehead;
void dfs(int root){
    int h = root, t = root;
    do{
        //printf("%d %d\n",h,t);
        h = a[a[h]];
        t = a[t];
    }
    while (h != t);
    do{
        //printf("%d ",t);
        ord[Pct++] = t;
        incyc[t] = 1;
        t = a[t];

    }
    while (t != h);
}
ll dfs2(int u, int p){
    vis[u] = 1;
    ll d2 = 0;
    ll d = 0;
    for (auto v: G[u]){
        if (v.fi == p || incyc[v.fi]) continue;
        ll nd = dfs2(v.fi,u)+v.se;
        if (d <= nd){
            d2 = d;
            d = nd;
        }
        else d2 = max(d2,nd);
    }
    ans = max(ans,d2+d);
    return d;
}
void solve(int root){
    ans = Pct = 0;
    dfs(root);
    //printf("at %d\n",root);
    reverse(ord,ord+Pct);
    for (int i = 0; i < Pct; i++){
        P[i] = dfs2(ord[i],-1);
        D[i] = L[ord[i]];
        //printf("%d: %d %d\n",x.fi,d[x.fi],x.se);
    }
    if (Pct == 2){
        ANS += max(ans,P[0]+P[1]+max(D[0],D[1]));
        return ;
    }
    ll S = 0;
    ll Sl = 0;
    for (int i = 0; i < Pct; i++){
        S += D[i];
    }
    ll MX = P[0];
    ll MX2 = P[0];
    for (int i = 1; i < Pct; i++){
        ll x = P[i];
        //printf("at %lld %lld\n",d[x],Sl[x]);
        //printf("MX: %lld + %lld\n",MX,d[x] + Sl[x]);
        //printf("MX2: %lld + %lld\n",MX2,S + d[x] - Sl[x]);
        Sl += D[i-1];
        ans = max(ans, x + Sl + MX);
        ans = max(ans, S + x - Sl + MX2);
        MX = max(MX, x - Sl);
        MX2 = max(MX2, x + Sl);
    }
    //printf("%lld max\n",ans);
    ANS += ans;
    return;
}
int main(){
    scanf("%d",&n);
    int j,_L;
    for (int i = 1; i <= n; i++){
        scanf("%d%d",&j,&_L);
        a[i] = j;
        L[i] = _L;
        G[i].push_back({j,_L});
        G[j].push_back({i,_L});
    }
    for (int i = 1; i <= n; i++){
        if (vis[i] == 0){
            solve(i);
        }
    }
    printf("%lld",ANS);
}

컴파일 시 표준 에러 (stderr) 메시지

islands.cpp: In function 'int main()':
islands.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   91 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
islands.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   94 |         scanf("%d%d",&j,&_L);
      |         ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...