Submission #935101

# Submission time Handle Problem Language Result Execution time Memory
935101 2024-02-28T15:39:28 Z anton Dreaming (IOI13_dreaming) C++17
47 / 100
83 ms 37020 KB
#include<bits/stdc++.h>
#include "dreaming.h"

using namespace std;
#define pii pair<int, int>


struct Tree{
    vector<vector<pii>> adj;
    vector<pair<pii, pii>> longest;
    vector<int> furthest;
    int min_len = 1e9;
    int diameter= 0;
};
const int MAX_N= 1e5;
vector<pii> madj[MAX_N+2];
bool vis[MAX_N+2];
vector<Tree> forest;


Tree sTree;

void get_tree(int id, int anc){
    //cout<<id<<endl;
    vis[id] = true;
    int tree_id = sTree.adj.size();
    sTree.adj.push_back(vector<pii>(0));
    for(auto e: madj[id]){
        if(e.first!=anc){
            int future_id = sTree.adj.size();
            get_tree(e.first, id);
            sTree.adj[tree_id].push_back(pii(future_id, e.second));
            sTree.adj[future_id].push_back(pii(tree_id, e.second));
        }
    }
    //cout<<id<<" done "<<endl;
}

pii get_len(int id, int anc, Tree& tr){
    vector<pii> ch;
    ch.push_back({0, -1});
    ch.push_back({0, -1});

    int max_diam = 0;

    for(auto e: tr.adj[id]){
        if(e.first!=anc){
            pii ch_ans = get_len(e.first, id, tr);
            ch.push_back({ch_ans.first+e.second, e.first});
            max_diam = max(max_diam, ch_ans.second);
        }
    }

    sort(ch.begin(), ch.end());
    tr.longest[id] = {ch[ch.size()-1], ch[ch.size()-2]};
    return pii(ch[ch.size()-1].first, max(max_diam, ch[ch.size()-1].first+ch[ch.size()-2].first));
}

void dp_reroot(int id, int anc, int from_above, Tree& tr){
    //cout<<id<<endl;
    tr.furthest[id] = max(from_above, tr.longest[id].first.first);
    //cout<<id<<" "<<tr.furthest[id]<<endl;
    vector<pii> cands;
    cands.push_back(tr.longest[id].first);
    cands.push_back(tr.longest[id].second);
    cands.push_back({from_above, -1});
    sort(cands.begin(), cands.end());
    reverse(cands.begin(), cands.end());

    for(auto e: tr.adj[id]){
        if(e.first!=anc){
            if(e.first == cands[0].second){
                dp_reroot(e.first, id, cands[1].first + e.second, tr);
            }
            else{
                dp_reroot(e.first, id, cands[0].first + e.second, tr);
            }
        }
    }
}

pii find_center(Tree& tr){
    dp_reroot(0, -1, 0, tr);
    int res_id = 0;
    int res_dist = 1e9;
    for(int i = 0; i<tr.adj.size(); i++){
        if(tr.furthest[i]<res_dist){
            res_id =i;
            res_dist = tr.furthest[i];
        }
    }
    return {res_id, res_dist};
}



signed travelTime(signed N,signed M, signed L,signed A[], signed B[], signed T[]) {
    for(int i =0; i<M; i++){
        madj[A[i]].push_back(pii(B[i], T[i]));
        madj[B[i]].push_back(pii(A[i], T[i]));
    }

    fill(&vis[0], &vis[MAX_N], false);
    for(int i = 0; i<N; i++){
        /*for(auto e: madj[i]){
            cout<<e.first<<" ";
        }
        cout<<endl;*/
        if(!vis[i]){
            
            //cerr<<i<<endl;
            sTree.adj.clear();
            get_tree(i, -1);
            forest.push_back(sTree);
        }
    }

    //cout<<forest.size()<<endl;
    
    for(int j = 0; j<forest.size(); j++){
        forest[j].longest.resize(forest[j].adj.size());
        forest[j].furthest.resize(forest[j].adj.size());

        
        pii d =  get_len(0, -1, forest[j]);

        
        forest[j].diameter = d.second;
        forest[j].min_len= find_center(forest[j]).second;
        //cout<<forest[j].diameter<<endl;
    }
    if(forest.size()==1){
        return forest[0].diameter;
    }

    return max(forest[0].min_len + forest[1].min_len + L, max(forest[0].diameter, forest[1].diameter));
}

Compilation message

dreaming.cpp: In function 'std::pair<int, int> find_center(Tree&)':
dreaming.cpp:86:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for(int i = 0; i<tr.adj.size(); i++){
      |                    ~^~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:120:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Tree>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |     for(int j = 0; j<forest.size(); j++){
      |                    ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 61 ms 26304 KB Output is correct
2 Correct 60 ms 25536 KB Output is correct
3 Correct 44 ms 22220 KB Output is correct
4 Correct 10 ms 7000 KB Output is correct
5 Correct 8 ms 5724 KB Output is correct
6 Correct 15 ms 8800 KB Output is correct
7 Correct 1 ms 3932 KB Output is correct
8 Correct 31 ms 13516 KB Output is correct
9 Correct 39 ms 18164 KB Output is correct
10 Correct 2 ms 3932 KB Output is correct
11 Correct 52 ms 19368 KB Output is correct
12 Correct 59 ms 22656 KB Output is correct
13 Correct 2 ms 3932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 1 ms 3676 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 1 ms 3676 KB Output is correct
8 Correct 1 ms 3676 KB Output is correct
9 Correct 1 ms 3676 KB Output is correct
10 Correct 1 ms 3676 KB Output is correct
11 Correct 1 ms 3676 KB Output is correct
12 Correct 1 ms 4128 KB Output is correct
13 Correct 2 ms 3676 KB Output is correct
14 Correct 1 ms 3768 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
17 Correct 1 ms 3676 KB Output is correct
18 Correct 2 ms 3676 KB Output is correct
19 Correct 1 ms 3672 KB Output is correct
20 Correct 1 ms 3672 KB Output is correct
21 Correct 1 ms 3676 KB Output is correct
22 Correct 1 ms 3676 KB Output is correct
23 Correct 1 ms 3676 KB Output is correct
24 Correct 1 ms 3672 KB Output is correct
25 Correct 1 ms 3672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 26304 KB Output is correct
2 Correct 60 ms 25536 KB Output is correct
3 Correct 44 ms 22220 KB Output is correct
4 Correct 10 ms 7000 KB Output is correct
5 Correct 8 ms 5724 KB Output is correct
6 Correct 15 ms 8800 KB Output is correct
7 Correct 1 ms 3932 KB Output is correct
8 Correct 31 ms 13516 KB Output is correct
9 Correct 39 ms 18164 KB Output is correct
10 Correct 2 ms 3932 KB Output is correct
11 Correct 52 ms 19368 KB Output is correct
12 Correct 59 ms 22656 KB Output is correct
13 Correct 2 ms 3932 KB Output is correct
14 Correct 2 ms 3676 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
17 Correct 2 ms 3676 KB Output is correct
18 Correct 1 ms 3676 KB Output is correct
19 Correct 1 ms 3676 KB Output is correct
20 Correct 1 ms 3676 KB Output is correct
21 Correct 1 ms 3676 KB Output is correct
22 Correct 1 ms 3676 KB Output is correct
23 Correct 1 ms 3676 KB Output is correct
24 Correct 1 ms 3676 KB Output is correct
25 Correct 1 ms 4128 KB Output is correct
26 Correct 2 ms 3676 KB Output is correct
27 Correct 1 ms 3768 KB Output is correct
28 Correct 1 ms 3676 KB Output is correct
29 Correct 1 ms 3676 KB Output is correct
30 Correct 1 ms 3676 KB Output is correct
31 Correct 2 ms 3676 KB Output is correct
32 Correct 1 ms 3672 KB Output is correct
33 Correct 1 ms 3672 KB Output is correct
34 Correct 1 ms 3676 KB Output is correct
35 Correct 1 ms 3676 KB Output is correct
36 Correct 1 ms 3676 KB Output is correct
37 Correct 1 ms 3672 KB Output is correct
38 Correct 1 ms 3672 KB Output is correct
39 Correct 64 ms 26048 KB Output is correct
40 Correct 63 ms 25540 KB Output is correct
41 Correct 45 ms 22468 KB Output is correct
42 Correct 12 ms 7128 KB Output is correct
43 Correct 1 ms 3676 KB Output is correct
44 Correct 1 ms 3676 KB Output is correct
45 Correct 1 ms 3676 KB Output is correct
46 Correct 1 ms 3676 KB Output is correct
47 Correct 1 ms 3676 KB Output is correct
48 Correct 1 ms 3892 KB Output is correct
49 Correct 1 ms 3676 KB Output is correct
50 Correct 1 ms 3676 KB Output is correct
51 Correct 1 ms 3672 KB Output is correct
52 Correct 1 ms 3676 KB Output is correct
53 Correct 1 ms 3676 KB Output is correct
54 Correct 1 ms 3672 KB Output is correct
55 Correct 1 ms 3928 KB Output is correct
56 Correct 1 ms 3672 KB Output is correct
57 Correct 74 ms 19184 KB Output is correct
58 Correct 66 ms 18144 KB Output is correct
59 Correct 83 ms 18136 KB Output is correct
60 Correct 67 ms 18564 KB Output is correct
61 Correct 81 ms 20224 KB Output is correct
62 Correct 67 ms 19704 KB Output is correct
63 Correct 67 ms 21536 KB Output is correct
64 Correct 69 ms 21760 KB Output is correct
65 Correct 68 ms 21516 KB Output is correct
66 Correct 77 ms 20116 KB Output is correct
67 Correct 69 ms 18348 KB Output is correct
68 Correct 70 ms 18976 KB Output is correct
69 Correct 72 ms 19180 KB Output is correct
70 Correct 68 ms 18400 KB Output is correct
71 Correct 1 ms 3672 KB Output is correct
72 Correct 3 ms 4188 KB Output is correct
73 Correct 3 ms 4188 KB Output is correct
74 Correct 3 ms 4416 KB Output is correct
75 Correct 3 ms 4188 KB Output is correct
76 Correct 3 ms 4156 KB Output is correct
77 Correct 3 ms 4188 KB Output is correct
78 Correct 3 ms 4188 KB Output is correct
79 Correct 3 ms 4188 KB Output is correct
80 Correct 76 ms 19944 KB Output is correct
81 Correct 71 ms 19268 KB Output is correct
82 Correct 70 ms 19400 KB Output is correct
83 Correct 74 ms 21488 KB Output is correct
84 Correct 2 ms 4184 KB Output is correct
85 Correct 2 ms 4188 KB Output is correct
86 Correct 3 ms 4188 KB Output is correct
87 Correct 3 ms 4188 KB Output is correct
88 Correct 2 ms 4188 KB Output is correct
89 Correct 3 ms 4188 KB Output is correct
90 Correct 3 ms 4188 KB Output is correct
91 Correct 3 ms 4188 KB Output is correct
92 Correct 3 ms 4188 KB Output is correct
93 Correct 3 ms 4160 KB Output is correct
94 Correct 1 ms 3676 KB Output is correct
95 Correct 2 ms 3676 KB Output is correct
96 Correct 1 ms 3832 KB Output is correct
97 Correct 1 ms 3676 KB Output is correct
98 Correct 1 ms 3676 KB Output is correct
99 Correct 1 ms 3676 KB Output is correct
100 Correct 1 ms 3676 KB Output is correct
101 Correct 1 ms 3672 KB Output is correct
102 Correct 1 ms 3672 KB Output is correct
103 Correct 1 ms 3676 KB Output is correct
104 Correct 7 ms 5720 KB Output is correct
105 Correct 16 ms 8840 KB Output is correct
106 Correct 1 ms 3932 KB Output is correct
107 Correct 28 ms 13560 KB Output is correct
108 Correct 39 ms 18116 KB Output is correct
109 Correct 4 ms 3932 KB Output is correct
110 Correct 52 ms 19656 KB Output is correct
111 Correct 59 ms 22808 KB Output is correct
112 Correct 2 ms 3928 KB Output is correct
113 Correct 81 ms 37020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 42 ms 19936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 1 ms 3676 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 1 ms 3676 KB Output is correct
8 Correct 1 ms 3676 KB Output is correct
9 Correct 1 ms 3676 KB Output is correct
10 Correct 1 ms 3676 KB Output is correct
11 Correct 1 ms 3676 KB Output is correct
12 Correct 1 ms 4128 KB Output is correct
13 Correct 2 ms 3676 KB Output is correct
14 Correct 1 ms 3768 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
17 Correct 1 ms 3676 KB Output is correct
18 Correct 2 ms 3676 KB Output is correct
19 Correct 1 ms 3672 KB Output is correct
20 Correct 1 ms 3672 KB Output is correct
21 Correct 1 ms 3676 KB Output is correct
22 Correct 1 ms 3676 KB Output is correct
23 Correct 1 ms 3676 KB Output is correct
24 Correct 1 ms 3672 KB Output is correct
25 Correct 1 ms 3672 KB Output is correct
26 Incorrect 2 ms 3928 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 61 ms 26304 KB Output is correct
2 Correct 60 ms 25536 KB Output is correct
3 Correct 44 ms 22220 KB Output is correct
4 Correct 10 ms 7000 KB Output is correct
5 Correct 8 ms 5724 KB Output is correct
6 Correct 15 ms 8800 KB Output is correct
7 Correct 1 ms 3932 KB Output is correct
8 Correct 31 ms 13516 KB Output is correct
9 Correct 39 ms 18164 KB Output is correct
10 Correct 2 ms 3932 KB Output is correct
11 Correct 52 ms 19368 KB Output is correct
12 Correct 59 ms 22656 KB Output is correct
13 Correct 2 ms 3932 KB Output is correct
14 Correct 2 ms 3676 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
17 Correct 2 ms 3676 KB Output is correct
18 Correct 1 ms 3676 KB Output is correct
19 Correct 1 ms 3676 KB Output is correct
20 Correct 1 ms 3676 KB Output is correct
21 Correct 1 ms 3676 KB Output is correct
22 Correct 1 ms 3676 KB Output is correct
23 Correct 1 ms 3676 KB Output is correct
24 Correct 1 ms 3676 KB Output is correct
25 Correct 1 ms 4128 KB Output is correct
26 Correct 2 ms 3676 KB Output is correct
27 Correct 1 ms 3768 KB Output is correct
28 Correct 1 ms 3676 KB Output is correct
29 Correct 1 ms 3676 KB Output is correct
30 Correct 1 ms 3676 KB Output is correct
31 Correct 2 ms 3676 KB Output is correct
32 Correct 1 ms 3672 KB Output is correct
33 Correct 1 ms 3672 KB Output is correct
34 Correct 1 ms 3676 KB Output is correct
35 Correct 1 ms 3676 KB Output is correct
36 Correct 1 ms 3676 KB Output is correct
37 Correct 1 ms 3672 KB Output is correct
38 Correct 1 ms 3672 KB Output is correct
39 Correct 64 ms 26048 KB Output is correct
40 Correct 63 ms 25540 KB Output is correct
41 Correct 45 ms 22468 KB Output is correct
42 Correct 12 ms 7128 KB Output is correct
43 Correct 1 ms 3676 KB Output is correct
44 Correct 1 ms 3676 KB Output is correct
45 Correct 1 ms 3676 KB Output is correct
46 Correct 1 ms 3676 KB Output is correct
47 Correct 1 ms 3676 KB Output is correct
48 Correct 1 ms 3892 KB Output is correct
49 Correct 1 ms 3676 KB Output is correct
50 Correct 1 ms 3676 KB Output is correct
51 Correct 1 ms 3672 KB Output is correct
52 Correct 1 ms 3676 KB Output is correct
53 Correct 1 ms 3676 KB Output is correct
54 Correct 1 ms 3672 KB Output is correct
55 Correct 1 ms 3928 KB Output is correct
56 Correct 1 ms 3672 KB Output is correct
57 Correct 74 ms 19184 KB Output is correct
58 Correct 66 ms 18144 KB Output is correct
59 Correct 83 ms 18136 KB Output is correct
60 Correct 67 ms 18564 KB Output is correct
61 Correct 81 ms 20224 KB Output is correct
62 Correct 67 ms 19704 KB Output is correct
63 Correct 67 ms 21536 KB Output is correct
64 Correct 69 ms 21760 KB Output is correct
65 Correct 68 ms 21516 KB Output is correct
66 Correct 77 ms 20116 KB Output is correct
67 Correct 69 ms 18348 KB Output is correct
68 Correct 70 ms 18976 KB Output is correct
69 Correct 72 ms 19180 KB Output is correct
70 Correct 68 ms 18400 KB Output is correct
71 Correct 1 ms 3672 KB Output is correct
72 Correct 3 ms 4188 KB Output is correct
73 Correct 3 ms 4188 KB Output is correct
74 Correct 3 ms 4416 KB Output is correct
75 Correct 3 ms 4188 KB Output is correct
76 Correct 3 ms 4156 KB Output is correct
77 Correct 3 ms 4188 KB Output is correct
78 Correct 3 ms 4188 KB Output is correct
79 Correct 3 ms 4188 KB Output is correct
80 Correct 76 ms 19944 KB Output is correct
81 Correct 71 ms 19268 KB Output is correct
82 Correct 70 ms 19400 KB Output is correct
83 Correct 74 ms 21488 KB Output is correct
84 Correct 2 ms 4184 KB Output is correct
85 Correct 2 ms 4188 KB Output is correct
86 Correct 3 ms 4188 KB Output is correct
87 Correct 3 ms 4188 KB Output is correct
88 Correct 2 ms 4188 KB Output is correct
89 Correct 3 ms 4188 KB Output is correct
90 Correct 3 ms 4188 KB Output is correct
91 Correct 3 ms 4188 KB Output is correct
92 Correct 3 ms 4188 KB Output is correct
93 Correct 3 ms 4160 KB Output is correct
94 Correct 1 ms 3676 KB Output is correct
95 Correct 2 ms 3676 KB Output is correct
96 Correct 1 ms 3832 KB Output is correct
97 Correct 1 ms 3676 KB Output is correct
98 Correct 1 ms 3676 KB Output is correct
99 Correct 1 ms 3676 KB Output is correct
100 Correct 1 ms 3676 KB Output is correct
101 Correct 1 ms 3672 KB Output is correct
102 Correct 1 ms 3672 KB Output is correct
103 Correct 1 ms 3676 KB Output is correct
104 Correct 7 ms 5720 KB Output is correct
105 Correct 16 ms 8840 KB Output is correct
106 Correct 1 ms 3932 KB Output is correct
107 Correct 28 ms 13560 KB Output is correct
108 Correct 39 ms 18116 KB Output is correct
109 Correct 4 ms 3932 KB Output is correct
110 Correct 52 ms 19656 KB Output is correct
111 Correct 59 ms 22808 KB Output is correct
112 Correct 2 ms 3928 KB Output is correct
113 Correct 81 ms 37020 KB Output is correct
114 Incorrect 42 ms 19936 KB Output isn't correct
115 Halted 0 ms 0 KB -