답안 #427195

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
427195 2021-06-14T13:20:26 Z AmineWeslati 꿈 (IOI13_dreaming) C++14
47 / 100
1000 ms 16320 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);


                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:146:14: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
  146 |     return a+b;
      |              ^
dreaming.cpp:146:14: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 14916 KB Output is correct
2 Correct 61 ms 14984 KB Output is correct
3 Correct 36 ms 11136 KB Output is correct
4 Correct 9 ms 5176 KB Output is correct
5 Correct 10 ms 4300 KB Output is correct
6 Correct 17 ms 5984 KB Output is correct
7 Correct 3 ms 3456 KB Output is correct
8 Correct 29 ms 7496 KB Output is correct
9 Correct 52 ms 9108 KB Output is correct
10 Correct 3 ms 3404 KB Output is correct
11 Correct 65 ms 11076 KB Output is correct
12 Correct 79 ms 12844 KB Output is correct
13 Correct 3 ms 3532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3404 KB Output is correct
2 Correct 3 ms 3404 KB Output is correct
3 Correct 3 ms 3404 KB Output is correct
4 Correct 3 ms 3404 KB Output is correct
5 Correct 3 ms 3452 KB Output is correct
6 Correct 3 ms 3460 KB Output is correct
7 Correct 3 ms 3452 KB Output is correct
8 Correct 3 ms 3404 KB Output is correct
9 Correct 3 ms 3404 KB Output is correct
10 Correct 3 ms 3404 KB Output is correct
11 Correct 3 ms 3404 KB Output is correct
12 Correct 3 ms 3404 KB Output is correct
13 Correct 3 ms 3532 KB Output is correct
14 Correct 3 ms 3404 KB Output is correct
15 Correct 3 ms 3452 KB Output is correct
16 Correct 3 ms 3404 KB Output is correct
17 Correct 3 ms 3404 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 3456 KB Output is correct
22 Correct 3 ms 3404 KB Output is correct
23 Correct 3 ms 3456 KB Output is correct
24 Correct 3 ms 3448 KB Output is correct
25 Correct 3 ms 3404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 14916 KB Output is correct
2 Correct 61 ms 14984 KB Output is correct
3 Correct 36 ms 11136 KB Output is correct
4 Correct 9 ms 5176 KB Output is correct
5 Correct 10 ms 4300 KB Output is correct
6 Correct 17 ms 5984 KB Output is correct
7 Correct 3 ms 3456 KB Output is correct
8 Correct 29 ms 7496 KB Output is correct
9 Correct 52 ms 9108 KB Output is correct
10 Correct 3 ms 3404 KB Output is correct
11 Correct 65 ms 11076 KB Output is correct
12 Correct 79 ms 12844 KB Output is correct
13 Correct 3 ms 3532 KB Output is correct
14 Correct 3 ms 3404 KB Output is correct
15 Correct 3 ms 3404 KB Output is correct
16 Correct 3 ms 3404 KB Output is correct
17 Correct 3 ms 3404 KB Output is correct
18 Correct 3 ms 3452 KB Output is correct
19 Correct 3 ms 3460 KB Output is correct
20 Correct 3 ms 3452 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 3 ms 3404 KB Output is correct
26 Correct 3 ms 3532 KB Output is correct
27 Correct 3 ms 3404 KB Output is correct
28 Correct 3 ms 3452 KB Output is correct
29 Correct 3 ms 3404 KB Output is correct
30 Correct 3 ms 3404 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 3456 KB Output is correct
35 Correct 3 ms 3404 KB Output is correct
36 Correct 3 ms 3456 KB Output is correct
37 Correct 3 ms 3448 KB Output is correct
38 Correct 3 ms 3404 KB Output is correct
39 Correct 77 ms 14972 KB Output is correct
40 Correct 84 ms 15008 KB Output is correct
41 Correct 37 ms 11052 KB Output is correct
42 Correct 9 ms 5068 KB Output is correct
43 Correct 3 ms 3452 KB Output is correct
44 Correct 2 ms 3404 KB Output is correct
45 Correct 2 ms 3404 KB Output is correct
46 Correct 2 ms 3404 KB Output is correct
47 Correct 3 ms 3404 KB Output is correct
48 Correct 2 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 3 ms 3404 KB Output is correct
52 Correct 3 ms 3404 KB Output is correct
53 Correct 3 ms 3404 KB Output is correct
54 Correct 3 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 79 ms 10620 KB Output is correct
58 Correct 109 ms 10664 KB Output is correct
59 Correct 99 ms 10608 KB Output is correct
60 Correct 130 ms 10608 KB Output is correct
61 Correct 79 ms 10692 KB Output is correct
62 Correct 100 ms 10728 KB Output is correct
63 Correct 138 ms 10412 KB Output is correct
64 Correct 118 ms 10416 KB Output is correct
65 Correct 90 ms 10664 KB Output is correct
66 Correct 71 ms 10648 KB Output is correct
67 Correct 115 ms 10560 KB Output is correct
68 Correct 141 ms 10540 KB Output is correct
69 Correct 151 ms 10948 KB Output is correct
70 Correct 93 ms 10908 KB Output is correct
71 Correct 2 ms 3460 KB Output is correct
72 Correct 4 ms 3660 KB Output is correct
73 Correct 4 ms 3660 KB Output is correct
74 Correct 4 ms 3660 KB Output is correct
75 Correct 5 ms 3660 KB Output is correct
76 Correct 4 ms 3660 KB Output is correct
77 Correct 4 ms 3660 KB Output is correct
78 Correct 4 ms 3592 KB Output is correct
79 Correct 4 ms 3660 KB Output is correct
80 Correct 110 ms 10532 KB Output is correct
81 Correct 128 ms 10592 KB Output is correct
82 Correct 94 ms 10736 KB Output is correct
83 Correct 93 ms 10652 KB Output is correct
84 Correct 4 ms 3532 KB Output is correct
85 Correct 4 ms 3660 KB Output is correct
86 Correct 4 ms 3588 KB Output is correct
87 Correct 4 ms 3636 KB Output is correct
88 Correct 5 ms 3596 KB Output is correct
89 Correct 4 ms 3536 KB Output is correct
90 Correct 5 ms 3632 KB Output is correct
91 Correct 4 ms 3660 KB Output is correct
92 Correct 4 ms 3532 KB Output is correct
93 Correct 4 ms 3532 KB Output is correct
94 Correct 3 ms 3452 KB Output is correct
95 Correct 2 ms 3452 KB Output is correct
96 Correct 3 ms 3456 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 3448 KB Output is correct
102 Correct 3 ms 3404 KB Output is correct
103 Correct 3 ms 3424 KB Output is correct
104 Correct 11 ms 4348 KB Output is correct
105 Correct 17 ms 5944 KB Output is correct
106 Correct 2 ms 3404 KB Output is correct
107 Correct 37 ms 7408 KB Output is correct
108 Correct 36 ms 9100 KB Output is correct
109 Correct 3 ms 3552 KB Output is correct
110 Correct 75 ms 11076 KB Output is correct
111 Correct 96 ms 12932 KB Output is correct
112 Correct 3 ms 3532 KB Output is correct
113 Correct 117 ms 16320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1075 ms 6904 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3404 KB Output is correct
2 Correct 3 ms 3404 KB Output is correct
3 Correct 3 ms 3404 KB Output is correct
4 Correct 3 ms 3404 KB Output is correct
5 Correct 3 ms 3452 KB Output is correct
6 Correct 3 ms 3460 KB Output is correct
7 Correct 3 ms 3452 KB Output is correct
8 Correct 3 ms 3404 KB Output is correct
9 Correct 3 ms 3404 KB Output is correct
10 Correct 3 ms 3404 KB Output is correct
11 Correct 3 ms 3404 KB Output is correct
12 Correct 3 ms 3404 KB Output is correct
13 Correct 3 ms 3532 KB Output is correct
14 Correct 3 ms 3404 KB Output is correct
15 Correct 3 ms 3452 KB Output is correct
16 Correct 3 ms 3404 KB Output is correct
17 Correct 3 ms 3404 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 3456 KB Output is correct
22 Correct 3 ms 3404 KB Output is correct
23 Correct 3 ms 3456 KB Output is correct
24 Correct 3 ms 3448 KB Output is correct
25 Correct 3 ms 3404 KB Output is correct
26 Correct 4 ms 3532 KB Output is correct
27 Correct 6 ms 3532 KB Output is correct
28 Correct 9 ms 3532 KB Output is correct
29 Correct 4 ms 3404 KB Output is correct
30 Correct 9 ms 3532 KB Output is correct
31 Correct 12 ms 3708 KB Output is correct
32 Correct 5 ms 3468 KB Output is correct
33 Incorrect 6 ms 3532 KB Output isn't correct
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 14916 KB Output is correct
2 Correct 61 ms 14984 KB Output is correct
3 Correct 36 ms 11136 KB Output is correct
4 Correct 9 ms 5176 KB Output is correct
5 Correct 10 ms 4300 KB Output is correct
6 Correct 17 ms 5984 KB Output is correct
7 Correct 3 ms 3456 KB Output is correct
8 Correct 29 ms 7496 KB Output is correct
9 Correct 52 ms 9108 KB Output is correct
10 Correct 3 ms 3404 KB Output is correct
11 Correct 65 ms 11076 KB Output is correct
12 Correct 79 ms 12844 KB Output is correct
13 Correct 3 ms 3532 KB Output is correct
14 Correct 3 ms 3404 KB Output is correct
15 Correct 3 ms 3404 KB Output is correct
16 Correct 3 ms 3404 KB Output is correct
17 Correct 3 ms 3404 KB Output is correct
18 Correct 3 ms 3452 KB Output is correct
19 Correct 3 ms 3460 KB Output is correct
20 Correct 3 ms 3452 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 3 ms 3404 KB Output is correct
26 Correct 3 ms 3532 KB Output is correct
27 Correct 3 ms 3404 KB Output is correct
28 Correct 3 ms 3452 KB Output is correct
29 Correct 3 ms 3404 KB Output is correct
30 Correct 3 ms 3404 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 3456 KB Output is correct
35 Correct 3 ms 3404 KB Output is correct
36 Correct 3 ms 3456 KB Output is correct
37 Correct 3 ms 3448 KB Output is correct
38 Correct 3 ms 3404 KB Output is correct
39 Correct 77 ms 14972 KB Output is correct
40 Correct 84 ms 15008 KB Output is correct
41 Correct 37 ms 11052 KB Output is correct
42 Correct 9 ms 5068 KB Output is correct
43 Correct 3 ms 3452 KB Output is correct
44 Correct 2 ms 3404 KB Output is correct
45 Correct 2 ms 3404 KB Output is correct
46 Correct 2 ms 3404 KB Output is correct
47 Correct 3 ms 3404 KB Output is correct
48 Correct 2 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 3 ms 3404 KB Output is correct
52 Correct 3 ms 3404 KB Output is correct
53 Correct 3 ms 3404 KB Output is correct
54 Correct 3 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 79 ms 10620 KB Output is correct
58 Correct 109 ms 10664 KB Output is correct
59 Correct 99 ms 10608 KB Output is correct
60 Correct 130 ms 10608 KB Output is correct
61 Correct 79 ms 10692 KB Output is correct
62 Correct 100 ms 10728 KB Output is correct
63 Correct 138 ms 10412 KB Output is correct
64 Correct 118 ms 10416 KB Output is correct
65 Correct 90 ms 10664 KB Output is correct
66 Correct 71 ms 10648 KB Output is correct
67 Correct 115 ms 10560 KB Output is correct
68 Correct 141 ms 10540 KB Output is correct
69 Correct 151 ms 10948 KB Output is correct
70 Correct 93 ms 10908 KB Output is correct
71 Correct 2 ms 3460 KB Output is correct
72 Correct 4 ms 3660 KB Output is correct
73 Correct 4 ms 3660 KB Output is correct
74 Correct 4 ms 3660 KB Output is correct
75 Correct 5 ms 3660 KB Output is correct
76 Correct 4 ms 3660 KB Output is correct
77 Correct 4 ms 3660 KB Output is correct
78 Correct 4 ms 3592 KB Output is correct
79 Correct 4 ms 3660 KB Output is correct
80 Correct 110 ms 10532 KB Output is correct
81 Correct 128 ms 10592 KB Output is correct
82 Correct 94 ms 10736 KB Output is correct
83 Correct 93 ms 10652 KB Output is correct
84 Correct 4 ms 3532 KB Output is correct
85 Correct 4 ms 3660 KB Output is correct
86 Correct 4 ms 3588 KB Output is correct
87 Correct 4 ms 3636 KB Output is correct
88 Correct 5 ms 3596 KB Output is correct
89 Correct 4 ms 3536 KB Output is correct
90 Correct 5 ms 3632 KB Output is correct
91 Correct 4 ms 3660 KB Output is correct
92 Correct 4 ms 3532 KB Output is correct
93 Correct 4 ms 3532 KB Output is correct
94 Correct 3 ms 3452 KB Output is correct
95 Correct 2 ms 3452 KB Output is correct
96 Correct 3 ms 3456 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 3448 KB Output is correct
102 Correct 3 ms 3404 KB Output is correct
103 Correct 3 ms 3424 KB Output is correct
104 Correct 11 ms 4348 KB Output is correct
105 Correct 17 ms 5944 KB Output is correct
106 Correct 2 ms 3404 KB Output is correct
107 Correct 37 ms 7408 KB Output is correct
108 Correct 36 ms 9100 KB Output is correct
109 Correct 3 ms 3552 KB Output is correct
110 Correct 75 ms 11076 KB Output is correct
111 Correct 96 ms 12932 KB Output is correct
112 Correct 3 ms 3532 KB Output is correct
113 Correct 117 ms 16320 KB Output is correct
114 Execution timed out 1075 ms 6904 KB Time limit exceeded
115 Halted 0 ms 0 KB -