Submission #427209

# Submission time Handle Problem Language Result Execution time Memory
427209 2021-06-14T13:23:24 Z AmineWeslati Dreaming (IOI13_dreaming) C++14
47 / 100
1000 ms 14656 KB
#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int>vi; 
#define pb push_back
#define sz(v) (int)v.size()

typedef pair<int,int>pi;
#define fi first
#define se second
typedef vector<pi>vpi;
#define eb emplace_back

#define FOR(i,a,b) for(int i=a; i<b; i++)
#define ROF(i,a,b) for(int i=b-1; i>=a; i--)


void ckmax(int &x, int y){x=max(x,y);}
void ckmin(int &x, int y){x=min(x,y);}

const int MX=1e5+10;
//-----------------------------
int N,M; 
vpi adj[MX];
vi vis(MX,0);

vi dist(MX);
pi par[MX]; 
void dfs(int u){
    vis[u]=1;
    for(auto it: adj[u]){
        int v=it.fi,w=it.se;
        if(dist[v]==-1){
            dist[v]=dist[u]+w;
            par[v]={u,w};
            dfs(v);
        }
    } 
}

int u2,v2,m2,c,d;
void solve(int u){
    FOR(i,0,N) dist[i]=-1; dist[u]=0; 
    dfs(u);
    int v=u; 
    FOR(i,0,N) if(dist[i]!=-1 && dist[i]>dist[v]) v=i; 

    u=v; 

    FOR(i,0,N) dist[i]=-1; dist[u]=0;
    dfs(u);
    v=u; 
    FOR(i,0,N) if(dist[i]!=-1 && dist[i]>dist[v]) v=i; 

    int diam=dist[v];
    tie(u2,v2)={u,v};


    int cur=0,mn=2e9;

    m2=v; c=0; d=0;
    while(u!=v){
        cur+=par[v].se; 
        v=par[v].fi;

        if(max(diam-cur,cur)<mn){
            mn=max(diam-cur,cur); 
            m2=v; c=diam-cur; d=cur; 
        }
    }
}


void find_middle(int u, int v){
    FOR(i,0,N) dist[i]=-1; dist[u]=0;
    dfs(u);
    int diam=dist[v];

    int cur=0,mn=2e9;
    m2=v; c=0; d=0;
    while(u!=v){
        cur+=par[v].se; 
        v=par[v].fi;

        if(max(diam-cur,cur)<mn){
            mn=max(diam-cur,cur); 
            m2=v; c=diam-cur; d=cur; 
        }
    }
}

int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    ::N=N; ::M=M; 
    FOR(i,0,M){
        int u=A[i],v=B[i],w=T[i];
        adj[u].eb(v,w);
        adj[v].eb(u,w);
    }


    int u=-1,v,m,a,b; 
    FOR(i,0,N) if(!vis[i]){
        solve(i);

        //cout << i << endl;
        //cout << u2 << ' ' << v2 << ' ' << m2 << ' ' << c << ' ' << d << endl;
        
        if(u==-1){
            tie(u,v)={u2,v2};
            m=m2;
            tie(a,b)={c,d};  
        }
        else{
            if(c+d>=max(a+b, max(a,b)+max(c,d)+L) ){
                tie(u,v)={u2,v2};
                m=m2;
                tie(a,b)={c,d};  
            }
            else if(max(a,b)+max(c,d)+L>=max(a+b,c+d)){
                adj[m].eb(m2,L);
                adj[m2].eb(m,L);

                solve(m);
                tie(u,v)={u2,v2};
                m=m2;
                tie(a,b)={c,d}; 


                /*int nu,nv; 
                if(a>=b) nu=u; 
                else nu=v; 

                if(c>=d) nv=u2;
                else nv=v2;

                tie(u,v)={nu,nv};

                //cout << u << ' ' << v << endl;
                //cout << m << ' '<< m2 << endl;
                //cout << endl;

                find_middle(u,v);
                m=m2; a=c; b=d; 

                //cout << m << endl;*/
            }
        }
    }
    return a+b; 
}

/*
12
8
2
0 8 4
8 2 2 
2 7 4
5 11 3
5 1 7
1 3 1
1 9 5
10 6 3

18

*/

/*
12
10
5

0 1 2
0 2 3
0 3 1
1 4 4 
1 5 1
3 6 6
7 8 5
7 9 5
5 10 5
5 11 5

22
*/

Compilation message

dreaming.cpp: In function 'void solve(int)':
dreaming.cpp:16:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   16 | #define FOR(i,a,b) for(int i=a; i<b; i++)
      |                    ^~~
dreaming.cpp:45:5: note: in expansion of macro 'FOR'
   45 |     FOR(i,0,N) dist[i]=-1; dist[u]=0;
      |     ^~~
dreaming.cpp:45:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   45 |     FOR(i,0,N) dist[i]=-1; dist[u]=0;
      |                            ^~~~
dreaming.cpp:16:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   16 | #define FOR(i,a,b) for(int i=a; i<b; i++)
      |                    ^~~
dreaming.cpp:52:5: note: in expansion of macro 'FOR'
   52 |     FOR(i,0,N) dist[i]=-1; dist[u]=0;
      |     ^~~
dreaming.cpp:52:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   52 |     FOR(i,0,N) dist[i]=-1; dist[u]=0;
      |                            ^~~~
dreaming.cpp: In function 'void find_middle(int, int)':
dreaming.cpp:16:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   16 | #define FOR(i,a,b) for(int i=a; i<b; i++)
      |                    ^~~
dreaming.cpp:77:5: note: in expansion of macro 'FOR'
   77 |     FOR(i,0,N) dist[i]=-1; dist[u]=0;
      |     ^~~
dreaming.cpp:77:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   77 |     FOR(i,0,N) dist[i]=-1; dist[u]=0;
      |                            ^~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:151:14: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
  151 |     return a+b;
      |              ^
dreaming.cpp:151:14: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
# Verdict Execution time Memory Grader output
1 Correct 65 ms 13764 KB Output is correct
2 Correct 64 ms 13640 KB Output is correct
3 Correct 35 ms 10180 KB Output is correct
4 Correct 9 ms 4940 KB Output is correct
5 Correct 8 ms 4172 KB Output is correct
6 Correct 14 ms 5708 KB Output is correct
7 Correct 3 ms 3404 KB Output is correct
8 Correct 29 ms 6988 KB Output is correct
9 Correct 36 ms 8492 KB Output is correct
10 Correct 3 ms 3440 KB Output is correct
11 Correct 63 ms 9936 KB Output is correct
12 Correct 68 ms 11828 KB Output is correct
13 Correct 3 ms 3532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3404 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
3 Correct 2 ms 3404 KB Output is correct
4 Correct 2 ms 3404 KB Output is correct
5 Correct 4 ms 3404 KB Output is correct
6 Correct 2 ms 3404 KB Output is correct
7 Correct 3 ms 3404 KB Output is correct
8 Correct 2 ms 3404 KB Output is correct
9 Correct 3 ms 3404 KB Output is correct
10 Correct 2 ms 3404 KB Output is correct
11 Correct 2 ms 3404 KB Output is correct
12 Correct 3 ms 3404 KB Output is correct
13 Correct 2 ms 3404 KB Output is correct
14 Correct 3 ms 3404 KB Output is correct
15 Correct 2 ms 3404 KB Output is correct
16 Correct 3 ms 3404 KB Output is correct
17 Correct 4 ms 3456 KB Output is correct
18 Correct 3 ms 3404 KB Output is correct
19 Correct 3 ms 3404 KB Output is correct
20 Correct 3 ms 3404 KB Output is correct
21 Correct 3 ms 3404 KB Output is correct
22 Correct 3 ms 3404 KB Output is correct
23 Correct 3 ms 3404 KB Output is correct
24 Correct 3 ms 3404 KB Output is correct
25 Correct 2 ms 3404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 13764 KB Output is correct
2 Correct 64 ms 13640 KB Output is correct
3 Correct 35 ms 10180 KB Output is correct
4 Correct 9 ms 4940 KB Output is correct
5 Correct 8 ms 4172 KB Output is correct
6 Correct 14 ms 5708 KB Output is correct
7 Correct 3 ms 3404 KB Output is correct
8 Correct 29 ms 6988 KB Output is correct
9 Correct 36 ms 8492 KB Output is correct
10 Correct 3 ms 3440 KB Output is correct
11 Correct 63 ms 9936 KB Output is correct
12 Correct 68 ms 11828 KB Output is correct
13 Correct 3 ms 3532 KB Output is correct
14 Correct 3 ms 3404 KB Output is correct
15 Correct 2 ms 3404 KB Output is correct
16 Correct 2 ms 3404 KB Output is correct
17 Correct 2 ms 3404 KB Output is correct
18 Correct 4 ms 3404 KB Output is correct
19 Correct 2 ms 3404 KB Output is correct
20 Correct 3 ms 3404 KB Output is correct
21 Correct 2 ms 3404 KB Output is correct
22 Correct 3 ms 3404 KB Output is correct
23 Correct 2 ms 3404 KB Output is correct
24 Correct 2 ms 3404 KB Output is correct
25 Correct 3 ms 3404 KB Output is correct
26 Correct 2 ms 3404 KB Output is correct
27 Correct 3 ms 3404 KB Output is correct
28 Correct 2 ms 3404 KB Output is correct
29 Correct 3 ms 3404 KB Output is correct
30 Correct 4 ms 3456 KB Output is correct
31 Correct 3 ms 3404 KB Output is correct
32 Correct 3 ms 3404 KB Output is correct
33 Correct 3 ms 3404 KB Output is correct
34 Correct 3 ms 3404 KB Output is correct
35 Correct 3 ms 3404 KB Output is correct
36 Correct 3 ms 3404 KB Output is correct
37 Correct 3 ms 3404 KB Output is correct
38 Correct 2 ms 3404 KB Output is correct
39 Correct 61 ms 13660 KB Output is correct
40 Correct 64 ms 13752 KB Output is correct
41 Correct 52 ms 10176 KB Output is correct
42 Correct 10 ms 4852 KB Output is correct
43 Correct 3 ms 3404 KB Output is correct
44 Correct 3 ms 3404 KB Output is correct
45 Correct 3 ms 3404 KB Output is correct
46 Correct 2 ms 3404 KB Output is correct
47 Correct 2 ms 3404 KB Output is correct
48 Correct 3 ms 3404 KB Output is correct
49 Correct 3 ms 3404 KB Output is correct
50 Correct 3 ms 3404 KB Output is correct
51 Correct 2 ms 3404 KB Output is correct
52 Correct 2 ms 3404 KB Output is correct
53 Correct 3 ms 3404 KB Output is correct
54 Correct 2 ms 3404 KB Output is correct
55 Correct 3 ms 3404 KB Output is correct
56 Correct 3 ms 3404 KB Output is correct
57 Correct 110 ms 9328 KB Output is correct
58 Correct 91 ms 9376 KB Output is correct
59 Correct 101 ms 9196 KB Output is correct
60 Correct 125 ms 9252 KB Output is correct
61 Correct 81 ms 9156 KB Output is correct
62 Correct 87 ms 9144 KB Output is correct
63 Correct 113 ms 9032 KB Output is correct
64 Correct 137 ms 9180 KB Output is correct
65 Correct 84 ms 9000 KB Output is correct
66 Correct 102 ms 9024 KB Output is correct
67 Correct 131 ms 9200 KB Output is correct
68 Correct 106 ms 9284 KB Output is correct
69 Correct 119 ms 9256 KB Output is correct
70 Correct 100 ms 9296 KB Output is correct
71 Correct 3 ms 3404 KB Output is correct
72 Correct 4 ms 3532 KB Output is correct
73 Correct 5 ms 3532 KB Output is correct
74 Correct 4 ms 3532 KB Output is correct
75 Correct 4 ms 3532 KB Output is correct
76 Correct 4 ms 3532 KB Output is correct
77 Correct 4 ms 3532 KB Output is correct
78 Correct 4 ms 3636 KB Output is correct
79 Correct 5 ms 3532 KB Output is correct
80 Correct 96 ms 9232 KB Output is correct
81 Correct 120 ms 9176 KB Output is correct
82 Correct 106 ms 9204 KB Output is correct
83 Correct 92 ms 9132 KB Output is correct
84 Correct 4 ms 3532 KB Output is correct
85 Correct 4 ms 3532 KB Output is correct
86 Correct 4 ms 3532 KB Output is correct
87 Correct 4 ms 3532 KB Output is correct
88 Correct 4 ms 3532 KB Output is correct
89 Correct 4 ms 3532 KB Output is correct
90 Correct 4 ms 3532 KB Output is correct
91 Correct 6 ms 3532 KB Output is correct
92 Correct 4 ms 3532 KB Output is correct
93 Correct 5 ms 3572 KB Output is correct
94 Correct 3 ms 3404 KB Output is correct
95 Correct 3 ms 3404 KB Output is correct
96 Correct 3 ms 3404 KB Output is correct
97 Correct 3 ms 3404 KB Output is correct
98 Correct 3 ms 3404 KB Output is correct
99 Correct 3 ms 3404 KB Output is correct
100 Correct 3 ms 3404 KB Output is correct
101 Correct 3 ms 3404 KB Output is correct
102 Correct 3 ms 3404 KB Output is correct
103 Correct 3 ms 3404 KB Output is correct
104 Correct 8 ms 4232 KB Output is correct
105 Correct 14 ms 5672 KB Output is correct
106 Correct 3 ms 3404 KB Output is correct
107 Correct 27 ms 6920 KB Output is correct
108 Correct 41 ms 8464 KB Output is correct
109 Correct 3 ms 3404 KB Output is correct
110 Correct 69 ms 9968 KB Output is correct
111 Correct 76 ms 11820 KB Output is correct
112 Correct 3 ms 3532 KB Output is correct
113 Correct 99 ms 14656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1078 ms 6488 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3404 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
3 Correct 2 ms 3404 KB Output is correct
4 Correct 2 ms 3404 KB Output is correct
5 Correct 4 ms 3404 KB Output is correct
6 Correct 2 ms 3404 KB Output is correct
7 Correct 3 ms 3404 KB Output is correct
8 Correct 2 ms 3404 KB Output is correct
9 Correct 3 ms 3404 KB Output is correct
10 Correct 2 ms 3404 KB Output is correct
11 Correct 2 ms 3404 KB Output is correct
12 Correct 3 ms 3404 KB Output is correct
13 Correct 2 ms 3404 KB Output is correct
14 Correct 3 ms 3404 KB Output is correct
15 Correct 2 ms 3404 KB Output is correct
16 Correct 3 ms 3404 KB Output is correct
17 Correct 4 ms 3456 KB Output is correct
18 Correct 3 ms 3404 KB Output is correct
19 Correct 3 ms 3404 KB Output is correct
20 Correct 3 ms 3404 KB Output is correct
21 Correct 3 ms 3404 KB Output is correct
22 Correct 3 ms 3404 KB Output is correct
23 Correct 3 ms 3404 KB Output is correct
24 Correct 3 ms 3404 KB Output is correct
25 Correct 2 ms 3404 KB Output is correct
26 Correct 4 ms 3404 KB Output is correct
27 Correct 8 ms 3560 KB Output is correct
28 Correct 12 ms 3616 KB Output is correct
29 Correct 3 ms 3404 KB Output is correct
30 Correct 6 ms 3532 KB Output is correct
31 Correct 8 ms 3624 KB Output is correct
32 Correct 3 ms 3404 KB Output is correct
33 Incorrect 5 ms 3532 KB Output isn't correct
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 65 ms 13764 KB Output is correct
2 Correct 64 ms 13640 KB Output is correct
3 Correct 35 ms 10180 KB Output is correct
4 Correct 9 ms 4940 KB Output is correct
5 Correct 8 ms 4172 KB Output is correct
6 Correct 14 ms 5708 KB Output is correct
7 Correct 3 ms 3404 KB Output is correct
8 Correct 29 ms 6988 KB Output is correct
9 Correct 36 ms 8492 KB Output is correct
10 Correct 3 ms 3440 KB Output is correct
11 Correct 63 ms 9936 KB Output is correct
12 Correct 68 ms 11828 KB Output is correct
13 Correct 3 ms 3532 KB Output is correct
14 Correct 3 ms 3404 KB Output is correct
15 Correct 2 ms 3404 KB Output is correct
16 Correct 2 ms 3404 KB Output is correct
17 Correct 2 ms 3404 KB Output is correct
18 Correct 4 ms 3404 KB Output is correct
19 Correct 2 ms 3404 KB Output is correct
20 Correct 3 ms 3404 KB Output is correct
21 Correct 2 ms 3404 KB Output is correct
22 Correct 3 ms 3404 KB Output is correct
23 Correct 2 ms 3404 KB Output is correct
24 Correct 2 ms 3404 KB Output is correct
25 Correct 3 ms 3404 KB Output is correct
26 Correct 2 ms 3404 KB Output is correct
27 Correct 3 ms 3404 KB Output is correct
28 Correct 2 ms 3404 KB Output is correct
29 Correct 3 ms 3404 KB Output is correct
30 Correct 4 ms 3456 KB Output is correct
31 Correct 3 ms 3404 KB Output is correct
32 Correct 3 ms 3404 KB Output is correct
33 Correct 3 ms 3404 KB Output is correct
34 Correct 3 ms 3404 KB Output is correct
35 Correct 3 ms 3404 KB Output is correct
36 Correct 3 ms 3404 KB Output is correct
37 Correct 3 ms 3404 KB Output is correct
38 Correct 2 ms 3404 KB Output is correct
39 Correct 61 ms 13660 KB Output is correct
40 Correct 64 ms 13752 KB Output is correct
41 Correct 52 ms 10176 KB Output is correct
42 Correct 10 ms 4852 KB Output is correct
43 Correct 3 ms 3404 KB Output is correct
44 Correct 3 ms 3404 KB Output is correct
45 Correct 3 ms 3404 KB Output is correct
46 Correct 2 ms 3404 KB Output is correct
47 Correct 2 ms 3404 KB Output is correct
48 Correct 3 ms 3404 KB Output is correct
49 Correct 3 ms 3404 KB Output is correct
50 Correct 3 ms 3404 KB Output is correct
51 Correct 2 ms 3404 KB Output is correct
52 Correct 2 ms 3404 KB Output is correct
53 Correct 3 ms 3404 KB Output is correct
54 Correct 2 ms 3404 KB Output is correct
55 Correct 3 ms 3404 KB Output is correct
56 Correct 3 ms 3404 KB Output is correct
57 Correct 110 ms 9328 KB Output is correct
58 Correct 91 ms 9376 KB Output is correct
59 Correct 101 ms 9196 KB Output is correct
60 Correct 125 ms 9252 KB Output is correct
61 Correct 81 ms 9156 KB Output is correct
62 Correct 87 ms 9144 KB Output is correct
63 Correct 113 ms 9032 KB Output is correct
64 Correct 137 ms 9180 KB Output is correct
65 Correct 84 ms 9000 KB Output is correct
66 Correct 102 ms 9024 KB Output is correct
67 Correct 131 ms 9200 KB Output is correct
68 Correct 106 ms 9284 KB Output is correct
69 Correct 119 ms 9256 KB Output is correct
70 Correct 100 ms 9296 KB Output is correct
71 Correct 3 ms 3404 KB Output is correct
72 Correct 4 ms 3532 KB Output is correct
73 Correct 5 ms 3532 KB Output is correct
74 Correct 4 ms 3532 KB Output is correct
75 Correct 4 ms 3532 KB Output is correct
76 Correct 4 ms 3532 KB Output is correct
77 Correct 4 ms 3532 KB Output is correct
78 Correct 4 ms 3636 KB Output is correct
79 Correct 5 ms 3532 KB Output is correct
80 Correct 96 ms 9232 KB Output is correct
81 Correct 120 ms 9176 KB Output is correct
82 Correct 106 ms 9204 KB Output is correct
83 Correct 92 ms 9132 KB Output is correct
84 Correct 4 ms 3532 KB Output is correct
85 Correct 4 ms 3532 KB Output is correct
86 Correct 4 ms 3532 KB Output is correct
87 Correct 4 ms 3532 KB Output is correct
88 Correct 4 ms 3532 KB Output is correct
89 Correct 4 ms 3532 KB Output is correct
90 Correct 4 ms 3532 KB Output is correct
91 Correct 6 ms 3532 KB Output is correct
92 Correct 4 ms 3532 KB Output is correct
93 Correct 5 ms 3572 KB Output is correct
94 Correct 3 ms 3404 KB Output is correct
95 Correct 3 ms 3404 KB Output is correct
96 Correct 3 ms 3404 KB Output is correct
97 Correct 3 ms 3404 KB Output is correct
98 Correct 3 ms 3404 KB Output is correct
99 Correct 3 ms 3404 KB Output is correct
100 Correct 3 ms 3404 KB Output is correct
101 Correct 3 ms 3404 KB Output is correct
102 Correct 3 ms 3404 KB Output is correct
103 Correct 3 ms 3404 KB Output is correct
104 Correct 8 ms 4232 KB Output is correct
105 Correct 14 ms 5672 KB Output is correct
106 Correct 3 ms 3404 KB Output is correct
107 Correct 27 ms 6920 KB Output is correct
108 Correct 41 ms 8464 KB Output is correct
109 Correct 3 ms 3404 KB Output is correct
110 Correct 69 ms 9968 KB Output is correct
111 Correct 76 ms 11820 KB Output is correct
112 Correct 3 ms 3532 KB Output is correct
113 Correct 99 ms 14656 KB Output is correct
114 Execution timed out 1078 ms 6488 KB Time limit exceeded
115 Halted 0 ms 0 KB -