답안 #262177

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
262177 2020-08-12T12:51:09 Z dantoh000 Islands (IOI08_islands) C++14
90 / 100
1235 ms 131072 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;
typedef pair<ll, int> li;
int n;
vector<ii> G[1000005];
li P[1000005];
int Pct;
int vis[1000005];
ll ANS;
ll ans;
int cyclehead;
void dfs(int u, int p){
    vis[u] = 2;
    int join =0;

    int ct = 0;
    for (auto v: G[u]){

        if (vis[v.fi] & 2){
            if (v.fi == p){
                if (ct == 1){
                    cyclehead = p;
                    //printf("%d %d repeated\n",u,v.fi);
                    vis[u] |= 4;
                    join = v.se;
                }
                else {
                    ct++;
                }
                continue;
            }
            cyclehead = v.fi;
            vis[u] |= 4;
            join = v.se;
            continue;
        }
        if (vis[v.fi]) continue;
        dfs(v.fi,u);
        if ((vis[v.fi]&4) && v.fi != cyclehead) {
            vis[u] |= 4;
            join = v.se;
            continue;
        }
    }
    if (vis[u] & 4){
        //printf("at %d : %d\n",u,join);
        P[Pct++] = {u,join};
    }
    vis[u] ^= 3;
}
ll dfs2(int u, int p){
    ll d2 = 0;
    ll d = 0;
    for (auto v: G[u]){
        if (v.fi == p || (vis[v.fi]&4)) 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);
    for (int i = 0; i < Pct; i++){
        P[i].fi = dfs2(P[i].fi,-1);
        //printf("%d: %d %d\n",x.fi,d[x.fi],x.se);
    }
    if (Pct == 2){
        return max(ans,P[0].fi+P[1].fi+max(P[0].se,P[1].se));
    }
    ll S = 0;
    ll Sl = 0;
    for (int i = 0; i < Pct; i++){
        S += P[i].se;
    }
    ll MX = P[0].fi;
    ll MX2 = P[0].fi;
    for (int i = 1; i < Pct; i++){
        ll x = P[i].fi;
        //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 += P[i-1].se;
        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){
            ANS += solve(i);
        }
    }
    printf("%lld",ANS);
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:105:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  105 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
islands.cpp:108:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  108 |         scanf("%d%d",&j,&L);
      |         ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 15 ms 23800 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 15 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 15 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23936 KB Output is correct
2 Correct 17 ms 23920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23936 KB Output is correct
2 Correct 18 ms 24192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 24568 KB Output is correct
2 Correct 44 ms 26744 KB Output is correct
3 Correct 29 ms 24704 KB Output is correct
4 Correct 21 ms 24192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 27768 KB Output is correct
2 Correct 59 ms 29432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 33912 KB Output is correct
2 Correct 120 ms 39164 KB Output is correct
3 Correct 147 ms 48248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 41080 KB Output is correct
2 Correct 237 ms 63608 KB Output is correct
3 Correct 277 ms 69496 KB Output is correct
4 Correct 362 ms 86776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 450 ms 66896 KB Output is correct
2 Correct 1076 ms 112364 KB Output is correct
3 Correct 369 ms 57848 KB Output is correct
4 Correct 507 ms 93308 KB Output is correct
5 Correct 484 ms 93816 KB Output is correct
6 Correct 1235 ms 62956 KB Output is correct
7 Correct 547 ms 129844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 505 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -