Submission #59540

# Submission time Handle Problem Language Result Execution time Memory
59540 2018-07-22T09:28:31 Z TAMREF Islands (IOI08_islands) C++11
80 / 100
1519 ms 132096 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;

const int mx = 1e6 + 5;

struct Edg{
    int u, v, w;
    inline int oppo(int x){
        return u + v - x;
    }
} E[mx];

vector<int> G[mx];
vector<bool> vis(mx);
vector<bool> vis2(mx);
vector<bool> dE(mx);
int num_cyc = 1; //greater than 1
int n;


vector<int> cv; //cycle vertex
vector<int> ce; //cycle edge weight
vector<ll> Vei, Wei;

int OP;
int dfs(int x){
    int cyc = 0, tcyc;
    vis[x] = 1;
    for(int &e : G[x]){
        if(dE[e]) continue;
        OP = E[e].oppo(x);
        if(vis[OP]){
            //printf("cycle detected - (%d->%d)\n",x,u);
            cyc = OP;
            dE[e] = 1;
            ce.push_back(E[e].w);
        }else{
            dE[e] = 1;
            tcyc = dfs(OP);
            cyc |= tcyc;
            if(tcyc){
                ce.push_back(E[e].w);
            }
        }
    }
    if(cyc){
        //printf("push %d in cv\n",x);
        cv.push_back(x);
    }
    if(cyc == x){
        //printf("stopping cycle in %d\n",x);
        cyc = 0;
    }
    return cyc;
}

ll lans;
ll dfs_st(int x){ //dfs for each subtrees in cycle
    vis2[x] = 1;
    ll ans = 0, ans2 = 0, tmp;
    for(int &e : G[x]){
        OP = E[e].oppo(x);
        if(!vis2[OP]){
            tmp = dfs_st(OP) + E[e].w;
            if(tmp > ans) ans2 = ans, ans = tmp;
            else ans2 = max(ans2, tmp);
        }
    }
    lans = max(lans, ans + ans2); //local answer in subtree
    return ans;
}

inline void BackInsert(deque<pli>& d, pli p){
    while(!d.empty() && d.back() <= p) d.pop_back();
    d.push_back(p);
}

inline void OldPop(deque<pli>& d, int k){
    while(!d.empty() && d.front().second < k) d.pop_front();
}

ll calc(vector<ll>& Vw, vector<int>& E, vector<ll>& sE){
    deque<pli> dq;
    int m = E.size();
    ll lsum = E[0];
    ll ret = 0;

    for(int i = 1; i < m; i++){
        BackInsert(dq, pli(Vw[i] + sE[i], i));
        lsum += E[i];
    }
    for(int i = 0; i < m; i++){
        OldPop(dq, i+1);
        ret = max(ret, Vw[i] - sE[i] + dq.front().first);
        BackInsert(dq, pli(Vw[i] + lsum + sE[i], i+m));
    }
    dq.clear();
    dq.shrink_to_fit();
    return ret;
}

ll dp_on_cycle(){
    int C = cv.size();
    ll lsum = 0;
    ll ans = 0;
    //give cycle-vertices their own number
    rotate(ce.begin(),ce.begin()+1,ce.end()); //f**k
    for(int i = 0; i < C; i++){
        vis2[cv[i]] = 1;
        //printf("vertex : %d, clockwise - edge : %d\n",cv[i],ce[i]);
    }
    //clockwise
    for(int i = 0; i < C; i++){
        Vei.push_back( dfs_st(cv[i]) );
        Wei.push_back( lsum );
        lsum += ce[i];
    }
    ans = max(ans, calc(Vei, ce, Wei));

    //inclockwise - reversing all the vectors
    lsum = 0;
    reverse(cv.begin(),cv.end());
    reverse(Vei.begin(),Vei.end());

    reverse(ce.begin(), ce.end() - 1);

    for(int i = 0; i < C; i++){
        //printf("vertex : %d, Inc ew : %d\n",cv[i],ce[i]);
        Wei[i] = lsum;
        lsum += ce[i];
    }

    ans = max(ans, calc(Vei, ce, Wei));
    ans = max(ans, lans);
    //printf("dp result : %lld\n",ans);
    return ans;
}

void input(){
    cin >> n;
    cv.reserve(n);
    ce.reserve(n);
    Vei.reserve(n);
    Wei.reserve(n);
    for(int i = 1; i <= n; i++){
        cin >> E[i].v >> E[i].w;
        E[i].u = i;
        G[i].push_back(i);
        G[E[i].v].push_back(i);
    }
}

void debug(const char* S){
    /*
    for(int i = 1; i <= n; i++){
        if(G[i].size() > 1){
            //printf("deg[%d] = %d\n",i,G[i].size());
            for(int &e : G[i]){
                if(E[e].w > 90000000)
                    //printf("(%d, %d)\n",E[e].oppo(i),E[e].w);
            }
        }
    }
    */
    FILE *fp = fopen(S,"rt");
    ll correct;
    fscanf(fp,"%lld",&correct);
    printf("answer = %lld\n",correct);
}

int main(){
    //freopen("isl10.in","rt",stdin);
    ios_base::sync_with_stdio(false);cin.tie(0);
    ll ans = 0;
    input();
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            cv.clear();
            ce.clear();
            Wei.clear();
            Vei.clear();
            dfs(i);
            lans = 0;
            ans += dp_on_cycle();
        }
    }
    cout << ans << '\n';
    //debug("isl10.out");
}

Compilation message

islands.cpp: In function 'void debug(const char*)':
islands.cpp:171:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fp,"%lld",&correct);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 28 ms 24312 KB Output is correct
2 Correct 25 ms 24400 KB Output is correct
3 Correct 23 ms 24400 KB Output is correct
4 Correct 28 ms 24400 KB Output is correct
5 Correct 24 ms 24472 KB Output is correct
6 Correct 24 ms 24544 KB Output is correct
7 Correct 26 ms 24544 KB Output is correct
8 Correct 28 ms 24544 KB Output is correct
9 Correct 28 ms 24560 KB Output is correct
10 Correct 25 ms 24564 KB Output is correct
11 Correct 26 ms 24564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24708 KB Output is correct
2 Correct 31 ms 24744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 24744 KB Output is correct
2 Correct 33 ms 24920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 25616 KB Output is correct
2 Correct 62 ms 27996 KB Output is correct
3 Correct 45 ms 27996 KB Output is correct
4 Correct 31 ms 27996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 30104 KB Output is correct
2 Correct 79 ms 33444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 40724 KB Output is correct
2 Correct 183 ms 48712 KB Output is correct
3 Correct 208 ms 59576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 213 ms 59984 KB Output is correct
2 Correct 371 ms 85764 KB Output is correct
3 Correct 356 ms 100796 KB Output is correct
4 Correct 450 ms 122848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 505 ms 124564 KB Output is correct
2 Runtime error 1519 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 593 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
2 Halted 0 ms 0 KB -