답안 #839017

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
839017 2023-08-28T13:34:17 Z Cookie Islands (IOI08_islands) C++14
64 / 100
2000 ms 131072 KB
#include<bits/stdc++.h>
using namespace std;
ifstream fin("susss.txt");
ofstream fout("res.txt");
#define forr(i, a, b) for(int i = a; i < b; i++)
#define pb push_back
#define vt vector
#define fi first
#define se second
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define sz(v) (int)v.size()
const int mxn = 1e6, inf = 1e9, mod = 1e9 + 7, mxm = 1e5 + 5;
int n;
vt<int>adj[mxn + 1];
 
bool vis[mxn + 1], in[mxn + 1];
ll mxd[mxn + 1], dis[mxn + 1];
int cost[mxn + 1];
int to[mxn + 1];
ll mx, id;
vt<int>comp;
void dfs(int s, int pre, int root, ll dep){
    mxd[root] = max(mxd[root], dep);
    comp.pb(s);
    for(int ii = 0; ii < sz(adj[s]); ii++){
        
        int i = adj[s][ii]; ll w;
        if(i == to[s])w = cost[s];
        else w = cost[i];
        if(i != pre && !in[i])dfs(i, s, root, dep + w);
        
    }
}
void dfs2(int s, int pre, int root, ll dep){
    queue<int>q;
    q.push(s); 
    while(!q.empty()){
        int nw = q.front(); q.pop();
        if(dis[nw] > mx){
            mx = dis[nw]; id = nw;
        }
    for(auto i: adj[s]){
        ll w;
        if(i == to[s])w = cost[s];
        else w = cost[i];
        if(dis[i] == 0 && (i == root || !in[i])){
            dis[i] = dis[nw] + w;
            q.push(i);
        }
    }
    }
    return;
}
void get_comp(int s){
    vis[s] = 1; 
    for(int idd = 0; idd < sz(adj[s]); idd++){
        int i = adj[s][idd];
        if(!vis[i]){
            get_comp(i);
        }
    }
}
 
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        int v, w; cin >> v >> w;
        to[i] = v; cost[i] = w;
        adj[i].pb(v); adj[v].pb(i);
    }
  vt<int>cyc; vt<ll>pref;
    ll ans = 0;
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            ll cand = 0;
            get_comp(i);
            int a = to[i],  b = to[to[i]];
            while(a != b){
                a = to[a]; b = to[to[b]];
            }
            a = i;
            while(a != b){
                a = to[a]; b = to[b];
            }
            ll pp = 0;
          cyc.clear(); pref.clear();
            cyc.pb(a); in[a] = 1;  pp += cost[a]; a = to[a]; 
            pref.pb(0);
            while(a != b){
                in[a] = 1;
                cyc.pb(a); pref.pb(pp); pp += cost[a]; a = to[a];
            }
            for(int ii = 0; ii < sz(cyc); ii++){
                comp.clear();
                int j = cyc[ii];
                dfs(j, -1, j, 0);
                mx = id = -1;
                dfs2(j, -1, j, 0);
                for(auto cool: comp)dis[cool] = 0;
                dfs2(id, -1, j, 0);
                cand = max(cand, mx);
            }
            
            ll pmx = mxd[cyc[0]];
            for(int j = 1; j < sz(cyc); j++){
                cand = max(cand, pref[j] + mxd[cyc[j]] + pmx);
                pmx = max(pmx, -pref[j] + mxd[cyc[j]]);
            }
            
            
            pmx = mxd[cyc[0]];
            for(int j = 1; j < sz(cyc); j++){
                cand = max(cand, -pref[j] + mxd[cyc[j]] + pmx + pp);
                pmx = max(pmx, pref[j] + mxd[cyc[j]]);
            }
            ans += cand;
            
            
        }
    }
    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Incorrect 10 ms 23764 KB Output isn't correct
3 Correct 10 ms 23908 KB Output is correct
4 Correct 10 ms 23764 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 10 ms 23764 KB Output is correct
7 Correct 10 ms 23764 KB Output is correct
8 Correct 14 ms 24020 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Incorrect 10 ms 23764 KB Output isn't correct
11 Correct 11 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23892 KB Output is correct
2 Correct 11 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23892 KB Output is correct
2 Correct 12 ms 24148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 24788 KB Output is correct
2 Correct 25 ms 26836 KB Output is correct
3 Correct 18 ms 25172 KB Output is correct
4 Incorrect 14 ms 24404 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 27984 KB Output is correct
2 Correct 40 ms 30728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 36672 KB Output is correct
2 Correct 81 ms 40788 KB Output is correct
3 Correct 99 ms 46960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 47756 KB Output is correct
2 Correct 154 ms 64144 KB Output is correct
3 Correct 196 ms 72648 KB Output is correct
4 Correct 215 ms 82852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2045 ms 83684 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 273 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -