답안 #262161

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
262161 2020-08-12T12:33:09 Z dantoh000 Islands (IOI08_islands) C++14
80 / 100
945 ms 131076 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];
vector<ii> P;
int incyc[1000005];
int vis[1000005];
ll d[1000005];
ll Sl[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);
                    incyc[u] = 1;
                    join = v.se;
                }
                else {
                    ct++;
                }
                continue;
            }
            cyclehead = v.fi;
            incyc[u] = 1;
            join = v.se;
            continue;
        }
        if (vis[v.fi]) continue;
        dfs(v.fi,u);
        if (incyc[v.fi] && v.fi != cyclehead) {
            incyc[u] = 1;
            join = v.se;
            continue;
        }
    }
    if (incyc[u]){
        //printf("at %d : %d\n",u,join);
        P.push_back({u,join});
    }
    vis[u] = 1;
}
void dfs2(int u, int p){
    ll d2 = 0;
    for (auto v: G[u]){
        if (v.fi == p || incyc[v.fi]) continue;
        dfs2(v.fi,u);
        ll nd = d[v.fi]+v.se;
        if (d[u] <= nd){
            d2 = d[u];
            d[u] = nd;
        }
        else d2 = max(d2,nd);
    }
    ans = max(ans,d2+d[u]);
}
ll solve(int root){
    ans = 0;
    P.clear();
    dfs(root,-1);
    //printf("at %d\n",root);
    reverse(P.begin(),P.end());
    for (auto x : P){
        dfs2(x.fi,-1);
        //printf("%d: %d %d\n",x.fi,d[x.fi],x.se);
    }
    if (P.size() == 2){
        return max(ans,d[P[0].fi]+d[P[1].fi]+max(P[0].se,P[1].se));
    }
    ll S = P[0].se;
    Sl[P[0].fi] = 0;
    for (int i = 1; i < P.size(); i++){
        Sl[P[i].fi] = Sl[P[i-1].fi]+P[i-1].se;
        S += P[i].se;
    }
    ll MX = d[P[0].fi];
    ll MX2 = d[P[0].fi];
    for (int i = 1; i < P.size(); i++){
        int 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]);
        ans = max(ans, d[x] + Sl[x] + MX);
        ans = max(ans, S + d[x] - Sl[x] + MX2);
        MX = max(MX, d[x] - Sl[x]);
        MX2 = max(MX2, d[x] + Sl[x]);
    }
    //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 'll solve(int)':
islands.cpp:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for (int i = 1; i < P.size(); i++){
      |                     ~~^~~~~~~~~~
islands.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     for (int i = 1; i < P.size(); i++){
      |                     ~~^~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  106 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
islands.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  109 |         scanf("%d%d",&j,&L);
      |         ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 17 ms 23936 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 17 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 18 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23936 KB Output is correct
2 Correct 19 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23936 KB Output is correct
2 Correct 21 ms 24320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 25216 KB Output is correct
2 Correct 43 ms 28280 KB Output is correct
3 Correct 29 ms 25472 KB Output is correct
4 Correct 30 ms 24576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 30068 KB Output is correct
2 Correct 62 ms 33004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 141 ms 40956 KB Output is correct
2 Correct 132 ms 45812 KB Output is correct
3 Correct 159 ms 60008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 207 ms 53628 KB Output is correct
2 Correct 283 ms 83308 KB Output is correct
3 Correct 283 ms 87580 KB Output is correct
4 Correct 405 ms 114532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 491 ms 72412 KB Output is correct
2 Runtime error 945 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 505 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -