답안 #262206

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
262206 2020-08-12T13:28:01 Z dantoh000 Islands (IOI08_islands) C++14
3 / 100
491 ms 127564 KB
#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;
int n;
vector<ii> G[1000005];
ll P[1000005];
int D[1000005];
int Pct;
bitset<1000005> vis[3];
ll ANS;
ll ans;
int cyclehead;
void dfs(int u, int p){
    vis[u][1] = 1;
    int join =0;
    int ct = 0;
    for (auto v: G[u]){
        if (vis[v.fi][1]){
            if (v.fi == p){
                if (ct == 1){
                    cyclehead = p;
                    //printf("%d %d repeated\n",u,v.fi);
                    vis[u][2] = 1;
                    join = v.se;
                }
                else {
                    ct++;
                }
                continue;
            }
            cyclehead = v.fi;
            vis[u][2] = 1;
            join = v.se;
            continue;
        }
        if (vis[v.fi][0] || vis[v.fi][1]) continue;
        dfs(v.fi,u);
        if (vis[v.fi][2] && v.fi != cyclehead) {
            vis[u][2] = 1;
            join = v.se;
            continue;
        }
    }
    if (vis[u][2]){
        //printf("at %d : %d\n",u,join);
        P[Pct] = u;
        D[Pct] = join;
        Pct++;
    }
    vis[u][0] = 1;
    vis[u][1] = 0;
}
ll dfs2(int u, int p){
    ll d2 = 0;
    ll d = 0;
    for (auto v: G[u]){
        if (v.fi == p || vis[v.fi][2]) 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;
}
ll solve(int root){
    ans = Pct = 0;
    dfs(root,-1);
    //printf("at %d\n",root);
    reverse(P,P+Pct);
    reverse(D,D+Pct);
    for (int i = 0; i < Pct; i++){
        P[i] = dfs2(P[i],-1);
        //printf("%d %d\n",P[i],D[i]);
    }
    if (Pct == 2){
        return max(ans,P[0]+P[1]+max(D[0],D[1]));
    }
    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);
    return ans;
}
int main(){
    scanf("%d",&n);
    for (int i = 1; i <= n; i++){
        int j,L;
        scanf("%d%d",&j,&L);
        G[i].push_back({j,L});
        G[j].push_back({i,L});
    }
    for (int i = 1; i <= n; i++){
        if (vis[i][0] == 0){
            ANS += solve(i);
        }
    }
    printf("%lld",ANS);
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:107:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  107 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
islands.cpp:110:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  110 |         scanf("%d%d",&j,&L);
      |         ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 23784 KB Output isn't correct
2 Runtime error 43 ms 48128 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Runtime error 42 ms 48120 KB Execution killed with signal 11 (could be triggered by violating memory limits)
4 Correct 14 ms 23808 KB Output is correct
5 Incorrect 17 ms 23808 KB Output isn't correct
6 Runtime error 42 ms 48120 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 44 ms 48376 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Incorrect 16 ms 24192 KB Output isn't correct
9 Runtime error 43 ms 48120 KB Execution killed with signal 11 (could be triggered by violating memory limits)
10 Runtime error 45 ms 48888 KB Execution killed with signal 11 (could be triggered by violating memory limits)
11 Runtime error 46 ms 48892 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 46 ms 48120 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 45 ms 48280 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 49 ms 49112 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 82 ms 51832 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 135 ms 62992 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 242 ms 76920 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 465 ms 127564 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 491 ms 111408 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -