Submission #897834

# Submission time Handle Problem Language Result Execution time Memory
897834 2024-01-03T18:46:19 Z Malix Dreaming (IOI13_dreaming) C++14
32 / 100
55 ms 16480 KB
#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define MP make_pair

vi leaf,leafVal;

int findLeaf(int a,vector<vector<pi>> &c,vi &p,vi &toLeaf,int &i,vi &up){
    if(toLeaf[a]!=-1)return toLeaf[a];
    up[a]=i;
    if(c[a].size()==0){
        toLeaf[a]=0;
        return 0;
    }
    if(c[a].size()<2&&p[a]!=a){
        toLeaf[a]=0;
        return 0;
    }
    for(auto u:c[a]){
        if(u.F==p[a])continue;
        p[u.F]=a;
        toLeaf[a]=max(toLeaf[a],findLeaf(u.F,c,p,toLeaf,i,up)+u.S);
        if(toLeaf[a]==toLeaf[u.F]+u.S){
            leaf[a]=u.F;
            leafVal[a]=u.S;
        }
    } 
    return toLeaf[a];
}

int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    vi p(N,-1);
    vi toLeaf(N,-1);
    vi maxLength(N,0);
    vi up(N);
    vi parents;
    leaf.resize(N,-1);
    leafVal.resize(N,-1);

    vector<vector<pi>> c(N);
    REP(i,0,M){
        c[A[i]].PB({B[i],T[i]});
        c[B[i]].PB({A[i],T[i]});
    }

    REP(i,0,N){
        if(toLeaf[i]!=-1)continue;
        p[i]=i;parents.PB(i);up[i]=i;
        findLeaf(i,c,p,toLeaf,i,up);
    }
   // cout<<"lol";
    REP(i,0,N){
       // cout<<i<<" "<<c[i].size()<<" ";
       if(c[i].size()==0){
            maxLength[i]=0;
            continue;
       }
       if(c[i].size()==1){
        maxLength[i]=toLeaf[i];
        continue;
       }
        if(c[i].size()<3&&p[i]!=i)continue;
        priority_queue<int> pq;
        for(auto u:c[i]){
            if(p[i]==u.F)continue;
          // if(toLeaf[u]==-1)pq.push(grid[i][u]);
            else pq.push(toLeaf[u.F]+u.S);
        }
        maxLength[i]+=pq.top();
        pq.pop();;
        maxLength[i]+=pq.top();
    }
    
    map<int,pi> m;
    REP(i,0,N){
        if(m.count(up[i])){
            if(m[up[i]].F<maxLength[i]){
                 m[up[i]]=make_pair(maxLength[i],i);
            }
        }
        else{
            m[up[i]]=make_pair(maxLength[i],i);
        }
    }
    
    priority_queue<tuple<int,int,int>> pq2;
    priority_queue<int> maxVals;
    for(auto z:parents){
        int u=m[z].S;int u2=u;
        vi tempVal1,tempVal2;
        if(c[u].size()==0){
            maxVals.push(0);
        }
        else if(c[u].size()==1){
            while(leaf[u]!=-1){
                tempVal1.PB(leafVal[u]);
                u=leaf[u];
            }
           // cout<<"lol\n";
            int sum=0;int pos=0;
            while(sum+tempVal1[pos]<=maxLength[u2]/2){
                sum+=tempVal1[pos];
                pos++;
            }
            int sum2=0;
            int ss=tempVal1.size();
           // reverse(tempVal1.begin(),tempVal1.end());
            if(pos<ss){
                sum2=sum+tempVal1[pos];
            }
            //cout<<sum<<" "<<sum2<<"\n";
            sum=min(max(sum,maxLength[u2]-sum),max(sum2,maxLength[u2]-sum2));

            maxVals.push(sum);
            
        }
        else{
            for(auto g:c[u]){
                if(g.F==p[u])continue;
                pq2.push({toLeaf[g.F]+g.S,g.F,g.S});
            }
            int x1=get<1>(pq2.top());
            int xx1=get<2>(pq2.top());
            pq2.pop();
            int x2=get<1>(pq2.top());
            int xx2=get<2>(pq2.top());
            tempVal1.PB(xx1);
            tempVal2.PB(xx2);
            while(leaf[x1]!=-1){
                tempVal1.PB(leafVal[x1]);
                x1=leaf[x1];
            }
            while(leaf[x2]!=-1){
                tempVal2.PB(leafVal[x2]);
                x2=leaf[x2];
            }
            reverse(tempVal1.begin(),tempVal1.end());
            for(auto y:tempVal2)tempVal1.PB(y);
            int sum=0;int pos=0;
            while(sum+tempVal1[pos]<=maxLength[u2]/2){
                sum+=tempVal1[pos];
                pos++;
            }
            int sum2=0;
            int ss=tempVal1.size();
           // reverse(tempVal1.begin(),tempVal1.end());
            if(pos<ss){
                sum2=sum+tempVal1[pos];
            }
            //cout<<sum<<" "<<sum2<<"\n";
            sum=min(max(sum,maxLength[u2]-sum),max(sum2,maxLength[u2]-sum2));
            //cout<<sum<<" "<<sum2<<"\n";
            //for(auto tt:tempVal1)cout<<tt<<" ";
           // cout<<"\n";
            maxVals.push(sum);
        }
    }
    //sort(toLeaf.begin(),toLeaf.end());
   // REP(i,0,N)cout<<toLeaf[i]<<" ";
   //cout<<"\n";
    //REP(i,0,N)cout<<maxLength[i]<<" ";
    //cout<<"\n";
    //for(auto g1:maxVals)cout<<g1<<" ";
   // cout<<"\n";
    
    int r1=maxVals.top();
    maxVals.pop();
    int ans=r1;
    if(!maxVals.empty()){
        int r2=maxVals.top();
        maxVals.pop();
        ans=max(ans,r1+r2+L);
        if(!maxVals.empty()){
            int r3=maxVals.top();
            ans=max(ans,r2+r3+2*L);
        } 
    } 
    
    
   // cout<<r1<<" "<<r2<<" "<<r3<<"\n";
    
    for(auto z:parents){
        int k1=m[z].F;
        ans=max(ans,k1);
    }
    return ans;

    return 42;
}
# Verdict Execution time Memory Grader output
1 Correct 38 ms 16480 KB Output is correct
2 Correct 36 ms 15832 KB Output is correct
3 Correct 24 ms 13392 KB Output is correct
4 Correct 7 ms 2652 KB Output is correct
5 Correct 4 ms 1884 KB Output is correct
6 Correct 8 ms 4172 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 16 ms 7320 KB Output is correct
9 Correct 22 ms 10892 KB Output is correct
10 Correct 1 ms 444 KB Output is correct
11 Correct 33 ms 12372 KB Output is correct
12 Correct 37 ms 14552 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 16480 KB Output is correct
2 Correct 36 ms 15832 KB Output is correct
3 Correct 24 ms 13392 KB Output is correct
4 Correct 7 ms 2652 KB Output is correct
5 Correct 4 ms 1884 KB Output is correct
6 Correct 8 ms 4172 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 16 ms 7320 KB Output is correct
9 Correct 22 ms 10892 KB Output is correct
10 Correct 1 ms 444 KB Output is correct
11 Correct 33 ms 12372 KB Output is correct
12 Correct 37 ms 14552 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Incorrect 0 ms 348 KB Output isn't correct
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 52 ms 12068 KB Output is correct
2 Correct 52 ms 12112 KB Output is correct
3 Correct 52 ms 11988 KB Output is correct
4 Correct 50 ms 12112 KB Output is correct
5 Correct 50 ms 11972 KB Output is correct
6 Correct 55 ms 13188 KB Output is correct
7 Correct 52 ms 12624 KB Output is correct
8 Correct 52 ms 11820 KB Output is correct
9 Correct 48 ms 11808 KB Output is correct
10 Correct 51 ms 12472 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 45 ms 12356 KB Output is correct
13 Correct 44 ms 12572 KB Output is correct
14 Correct 43 ms 12500 KB Output is correct
15 Correct 42 ms 12488 KB Output is correct
16 Correct 44 ms 12500 KB Output is correct
17 Correct 44 ms 12344 KB Output is correct
18 Correct 42 ms 12348 KB Output is correct
19 Correct 43 ms 12492 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 42 ms 12444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 16480 KB Output is correct
2 Correct 36 ms 15832 KB Output is correct
3 Correct 24 ms 13392 KB Output is correct
4 Correct 7 ms 2652 KB Output is correct
5 Correct 4 ms 1884 KB Output is correct
6 Correct 8 ms 4172 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 16 ms 7320 KB Output is correct
9 Correct 22 ms 10892 KB Output is correct
10 Correct 1 ms 444 KB Output is correct
11 Correct 33 ms 12372 KB Output is correct
12 Correct 37 ms 14552 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Incorrect 0 ms 348 KB Output isn't correct
18 Halted 0 ms 0 KB -