답안 #525663

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525663 2022-02-12T11:51:57 Z Urvuk3 꿈 (IOI13_dreaming) C++17
47 / 100
554 ms 47780 KB
#include "dreaming.h"
#include <bits/stdc++.h>

using namespace std;

#define ll long long
const ll INF=1e9,LINF=1e18;
#define fi first
#define se second
#define pii pair<int,int>
#define mid ((l+r)/2)
#define sz(a) (int((a).size()))
#define all(a) a.begin(),a.end()
#define endl "\n"
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
#define pb push_back
#define PRINTvec(arr) { cerr<<#arr<<"="; for(auto h:arr) cerr<<h<<" "; cerr<<endl; }

int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    vector<int> adj[N+1];
    map<pair<int,int>,ll> edge;
    for(int i=0;i<M;i++){
        int u=A[i],v=B[i],w=T[i];
        u++; v++;
        adj[u].pb(v);
        adj[v].pb(u);
        edge[{u,v}]=w;
        edge[{v,u}]=w;
    }
    vector<int> trees[N+1];
    vector<bool> vis(N+1,false);
    int idx=1;
    function<void(int,int)> dfs=[&](int node,int prev){
        vis[node]=true;
        trees[idx].pb(node);
        for(auto v:adj[node]) if(v!=prev) dfs(v,node);
    };
    for(int i=1;i<=N;i++){
        if(!vis[i]){
            dfs(i,-1);
            idx++;
        }
    }
    vector<ll> nadstablo_udaljenost(N+1),podstablo_udaljenost1(N+1),podstablo_udaljenost2(N+1);
    vector<ll> udaljenost(N+1),dubina(N+1);

    function<void(int,int)> dfs1=[&](int node,int prev){
        podstablo_udaljenost1[node]=0; podstablo_udaljenost2[node]=0;
        if(prev!=-1) dubina[node]=dubina[prev]+1;
        else dubina[node]=0;
        vector<ll> tmp; tmp.pb(0); tmp.pb(0);
        for(auto v:adj[node]){
            if(v!=prev){
                dfs1(v,node);
                tmp.pb(podstablo_udaljenost1[v]+edge[{node,v}]);
            }
        }
        sort(all(tmp),greater<int>());
        podstablo_udaljenost1[node]=tmp[0]; podstablo_udaljenost2[node]=tmp[1];
    };

    function<void(int,int)> dfs2=[&](int node,int prev){
        nadstablo_udaljenost[node]=0;
        if(prev!=-1){
            nadstablo_udaljenost[node]=nadstablo_udaljenost[prev]+edge[{prev,node}];
        }
        for(auto v:adj[prev]){
            if(dubina[v]>dubina[prev] && v!=node){
                nadstablo_udaljenost[node]=max(nadstablo_udaljenost[node],podstablo_udaljenost1[v]+edge[{v,prev}]+edge[{prev,node}]);
            }
        }
        for(auto v:adj[node]) if(v!=prev) dfs2(v,node);
        udaljenost[node]=max(nadstablo_udaljenost[node],podstablo_udaljenost1[node]);
    };


    vector<pii> all_trees;
    
    /*
    for(int i=1;i<idx;i++){
        int node=0;
        ll val=LINF;
        for(auto v:trees[i]){
            if(udaljenost[v]<=val){
                val=udaljenost[v]; node=v;
            }
        }
        all_trees.pb({val,node});
    }
    
    sort(all(all_trees),greater<pair<ll,ll>>());

    for(int i=1;i<sz(all_trees);i++){
        int node=all_trees[i].se;
        int first=all_trees[0].se;
        adj[node].pb(first); adj[first].pb(node);
        edge[{node,first}]=L; edge[{first,node}]=L;
    }
    */

    int node=0; ll val=LINF;
    for(int i=1;i<idx;i++){
        int root=trees[i][0];
        dfs1(root,-1);
        dfs2(root,-1);
        if(i==1){
            for(auto v:trees[i]){
                if(udaljenost[v]<=val){
                    val=udaljenost[v]; node=v;
                }
            }
        }
        else{
            int tmp_node=0,tmp_val=INF;
            for(auto v:trees[i]){
                if(udaljenost[v]<=tmp_val){
                    tmp_val=udaljenost[v]; tmp_node=v;
                }
            }
            adj[tmp_node].pb(node); adj[node].pb(tmp_node);
            edge[{tmp_node,node}]=L; edge[{node,tmp_node}]=L;
        }
    }

    /*
    for(int i=1;i<idx;i++){
        PRINTvec(trees[i]);
        for(auto v:trees[i]){
            cout<<"nadstablo_udaljenost["<<v<<"]="<<nadstablo_udaljenost[v]<<endl;
            cout<<"podstablo_udaljenost1["<<v<<"]="<<podstablo_udaljenost1[v]<<endl;
            cout<<"podstablo_udaljenost2["<<v<<"]="<<podstablo_udaljenost2[v]<<endl;
            cout<<"udaljenost["<<v<<"]="<<udaljenost[v]<<endl;
            cout<<"dubina["<<v<<"]="<<dubina[v]<<endl;
            cout<<endl;
        }
        cout<<endl<<endl;
    }
    */
    dfs1(1,-1);
    ll diametar=0;
    for(int node=1;node<=N;node++){
        diametar=max(diametar,podstablo_udaljenost1[node]+podstablo_udaljenost2[node]);
    }
    return diametar;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 314 ms 34104 KB Output is correct
2 Correct 323 ms 34536 KB Output is correct
3 Correct 174 ms 27136 KB Output is correct
4 Correct 24 ms 5396 KB Output is correct
5 Correct 27 ms 3532 KB Output is correct
6 Correct 55 ms 8004 KB Output is correct
7 Correct 1 ms 376 KB Output is correct
8 Correct 157 ms 15156 KB Output is correct
9 Correct 197 ms 21792 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 352 ms 26216 KB Output is correct
12 Correct 355 ms 30488 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 296 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 314 ms 34104 KB Output is correct
2 Correct 323 ms 34536 KB Output is correct
3 Correct 174 ms 27136 KB Output is correct
4 Correct 24 ms 5396 KB Output is correct
5 Correct 27 ms 3532 KB Output is correct
6 Correct 55 ms 8004 KB Output is correct
7 Correct 1 ms 376 KB Output is correct
8 Correct 157 ms 15156 KB Output is correct
9 Correct 197 ms 21792 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 352 ms 26216 KB Output is correct
12 Correct 355 ms 30488 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 296 KB Output is correct
35 Correct 1 ms 296 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 347 ms 35392 KB Output is correct
40 Correct 328 ms 34456 KB Output is correct
41 Correct 177 ms 27068 KB Output is correct
42 Correct 23 ms 5324 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 304 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 296 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 300 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 332 KB Output is correct
53 Correct 1 ms 296 KB Output is correct
54 Correct 1 ms 300 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 539 ms 27620 KB Output is correct
58 Correct 477 ms 27588 KB Output is correct
59 Correct 481 ms 27832 KB Output is correct
60 Correct 502 ms 27844 KB Output is correct
61 Correct 541 ms 27796 KB Output is correct
62 Correct 493 ms 27964 KB Output is correct
63 Correct 499 ms 27716 KB Output is correct
64 Correct 545 ms 27632 KB Output is correct
65 Correct 514 ms 27980 KB Output is correct
66 Correct 502 ms 27992 KB Output is correct
67 Correct 518 ms 27868 KB Output is correct
68 Correct 554 ms 27932 KB Output is correct
69 Correct 530 ms 28252 KB Output is correct
70 Correct 490 ms 28260 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 6 ms 1100 KB Output is correct
73 Correct 6 ms 1100 KB Output is correct
74 Correct 7 ms 1100 KB Output is correct
75 Correct 6 ms 1100 KB Output is correct
76 Correct 5 ms 1100 KB Output is correct
77 Correct 6 ms 1100 KB Output is correct
78 Correct 6 ms 1100 KB Output is correct
79 Correct 6 ms 1100 KB Output is correct
80 Correct 510 ms 27824 KB Output is correct
81 Correct 506 ms 27824 KB Output is correct
82 Correct 491 ms 27908 KB Output is correct
83 Correct 502 ms 27960 KB Output is correct
84 Correct 31 ms 844 KB Output is correct
85 Correct 28 ms 884 KB Output is correct
86 Correct 18 ms 972 KB Output is correct
87 Correct 6 ms 972 KB Output is correct
88 Correct 25 ms 948 KB Output is correct
89 Correct 19 ms 972 KB Output is correct
90 Correct 5 ms 972 KB Output is correct
91 Correct 6 ms 1100 KB Output is correct
92 Correct 6 ms 972 KB Output is correct
93 Correct 14 ms 1040 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 1 ms 332 KB Output is correct
98 Correct 1 ms 332 KB Output is correct
99 Correct 1 ms 332 KB Output is correct
100 Correct 1 ms 332 KB Output is correct
101 Correct 1 ms 332 KB Output is correct
102 Correct 1 ms 332 KB Output is correct
103 Correct 1 ms 332 KB Output is correct
104 Correct 21 ms 3604 KB Output is correct
105 Correct 45 ms 7984 KB Output is correct
106 Correct 1 ms 332 KB Output is correct
107 Correct 126 ms 15140 KB Output is correct
108 Correct 202 ms 21812 KB Output is correct
109 Correct 1 ms 588 KB Output is correct
110 Correct 363 ms 26168 KB Output is correct
111 Correct 360 ms 30616 KB Output is correct
112 Correct 2 ms 588 KB Output is correct
113 Correct 491 ms 47780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 233 ms 26304 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 296 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 3 ms 564 KB Output is correct
27 Correct 8 ms 844 KB Output is correct
28 Correct 16 ms 1100 KB Output is correct
29 Incorrect 4 ms 460 KB Output isn't correct
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 314 ms 34104 KB Output is correct
2 Correct 323 ms 34536 KB Output is correct
3 Correct 174 ms 27136 KB Output is correct
4 Correct 24 ms 5396 KB Output is correct
5 Correct 27 ms 3532 KB Output is correct
6 Correct 55 ms 8004 KB Output is correct
7 Correct 1 ms 376 KB Output is correct
8 Correct 157 ms 15156 KB Output is correct
9 Correct 197 ms 21792 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 352 ms 26216 KB Output is correct
12 Correct 355 ms 30488 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 296 KB Output is correct
35 Correct 1 ms 296 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 347 ms 35392 KB Output is correct
40 Correct 328 ms 34456 KB Output is correct
41 Correct 177 ms 27068 KB Output is correct
42 Correct 23 ms 5324 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 304 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 296 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 300 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 332 KB Output is correct
53 Correct 1 ms 296 KB Output is correct
54 Correct 1 ms 300 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 539 ms 27620 KB Output is correct
58 Correct 477 ms 27588 KB Output is correct
59 Correct 481 ms 27832 KB Output is correct
60 Correct 502 ms 27844 KB Output is correct
61 Correct 541 ms 27796 KB Output is correct
62 Correct 493 ms 27964 KB Output is correct
63 Correct 499 ms 27716 KB Output is correct
64 Correct 545 ms 27632 KB Output is correct
65 Correct 514 ms 27980 KB Output is correct
66 Correct 502 ms 27992 KB Output is correct
67 Correct 518 ms 27868 KB Output is correct
68 Correct 554 ms 27932 KB Output is correct
69 Correct 530 ms 28252 KB Output is correct
70 Correct 490 ms 28260 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 6 ms 1100 KB Output is correct
73 Correct 6 ms 1100 KB Output is correct
74 Correct 7 ms 1100 KB Output is correct
75 Correct 6 ms 1100 KB Output is correct
76 Correct 5 ms 1100 KB Output is correct
77 Correct 6 ms 1100 KB Output is correct
78 Correct 6 ms 1100 KB Output is correct
79 Correct 6 ms 1100 KB Output is correct
80 Correct 510 ms 27824 KB Output is correct
81 Correct 506 ms 27824 KB Output is correct
82 Correct 491 ms 27908 KB Output is correct
83 Correct 502 ms 27960 KB Output is correct
84 Correct 31 ms 844 KB Output is correct
85 Correct 28 ms 884 KB Output is correct
86 Correct 18 ms 972 KB Output is correct
87 Correct 6 ms 972 KB Output is correct
88 Correct 25 ms 948 KB Output is correct
89 Correct 19 ms 972 KB Output is correct
90 Correct 5 ms 972 KB Output is correct
91 Correct 6 ms 1100 KB Output is correct
92 Correct 6 ms 972 KB Output is correct
93 Correct 14 ms 1040 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 1 ms 332 KB Output is correct
98 Correct 1 ms 332 KB Output is correct
99 Correct 1 ms 332 KB Output is correct
100 Correct 1 ms 332 KB Output is correct
101 Correct 1 ms 332 KB Output is correct
102 Correct 1 ms 332 KB Output is correct
103 Correct 1 ms 332 KB Output is correct
104 Correct 21 ms 3604 KB Output is correct
105 Correct 45 ms 7984 KB Output is correct
106 Correct 1 ms 332 KB Output is correct
107 Correct 126 ms 15140 KB Output is correct
108 Correct 202 ms 21812 KB Output is correct
109 Correct 1 ms 588 KB Output is correct
110 Correct 363 ms 26168 KB Output is correct
111 Correct 360 ms 30616 KB Output is correct
112 Correct 2 ms 588 KB Output is correct
113 Correct 491 ms 47780 KB Output is correct
114 Incorrect 233 ms 26304 KB Output isn't correct
115 Halted 0 ms 0 KB -