답안 #561118

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
561118 2022-05-12T09:38:54 Z Zanite 꿈 (IOI13_dreaming) C++17
47 / 100
134 ms 23156 KB
#include "dreaming.h"

#include <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>
#define fi first
#define se second

const int INF = 2e9;
const int maxN = 1e5 + 5;

void chmax(int &a, int b) {a = max(a, b);}
void chmin(int &a, int b) {a = min(a, b);}

struct Node {
    int child, lg;
    Node(int _child, int _lg) :
        child(_child),
        lg(_lg)
    {}

    bool operator < (const Node &other) const {
        return lg > other.lg;
    }
};

int N, M, L, ans;
vector<pii> adj[maxN];
vector<Node> children[maxN];
int idx[maxN], longest[maxN], newLongest[maxN];

vector<int> curComponent, components;
void dfs(int cur, int par, int id) {
    idx[cur] = id;
	curComponent.push_back(cur);

    for (auto a : adj[cur]) {
        int nxt, w;
        tie(nxt, w) = a;
        if (nxt == par) continue;

        dfs(nxt, cur, id);
        chmax(longest[cur], longest[nxt] + w);
        children[cur].push_back(Node(nxt, longest[nxt] + w));
    }
}

void reroot(int cur, int par, int downwardsEdge) {
    if (par != -1) {
        // push "parent" as cur's child
        if (children[par][0].child == cur) {
            if (children[par].size() > 1) {
                children[cur].push_back(Node(par, children[par][1].lg + downwardsEdge));
            } else {
                children[cur].push_back(Node(par, downwardsEdge));
            }
            
        } else {
            children[cur].push_back(Node(par, children[par][0].lg + downwardsEdge));
        }
    }

	sort(children[cur].begin(), children[cur].end());

	// update newLongest
	for (auto c : children[cur]) {
		chmax(newLongest[cur], c.lg);
	}

	// go to children
	for (auto a : adj[cur]) {
		int nxt, w;
		tie(nxt, w) = a;
		if (nxt == par) continue;

		reroot(nxt, cur, w);
	}
}

void calculateTree(int root, int id) {
    // gather data about longest and children's longest for each node
	curComponent.clear();
    dfs(root, -1, id);

    // do reroot
    reroot(root, -1, -1);

	// find out the minimum newLongest of the component
	int curMin = INF;
	for (auto i : curComponent) {
		chmax(ans, newLongest[i]);
		chmin(curMin, newLongest[i]);
	}
	components.push_back(curMin);
}

int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    ::N = N, ::M = M, ::L = L;

    for (int i = 0; i < M; i++) {
        adj[A[i]].push_back({B[i], T[i]});
        adj[B[i]].push_back({A[i], T[i]});
    }

    for (int i = 0, cur = 1; i < N; i++) {
        if (idx[i] == 0) {
            calculateTree(i, cur);
            cur++;
        }
    }

	/*
	for (int i = 0; i < N; i++) {
		printf("%d: %d %d\n", i, idx[i], newLongest[i]);
	}
	*/

	sort(components.begin(), components.end());
	int s = components.size();

	if (components.size() == 2) {
		chmax(ans, components[s-1] + components[s-2] + L);
	} else {
		int pos[3];
		pos[0] = max(
			components[s-1] + components[s-2] + L,
			components[s-2] + components[s-3] + 2*L
		);
		pos[1] = max(
			components[s-1] + components[s-2] + L,
			components[s-1] + components[s-3] + 2*L
		);
		pos[2] = max(
			components[s-1] + components[s-3] + L,
			components[s-1] + components[s-2] + 2*L
		);
		chmax(ans, min({pos[0], pos[1], pos[2]}));
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 17904 KB Output is correct
2 Correct 78 ms 17588 KB Output is correct
3 Correct 60 ms 15120 KB Output is correct
4 Correct 9 ms 6868 KB Output is correct
5 Correct 7 ms 6092 KB Output is correct
6 Correct 14 ms 7844 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 28 ms 10320 KB Output is correct
9 Correct 37 ms 13056 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 63 ms 14300 KB Output is correct
12 Correct 95 ms 16044 KB Output is correct
13 Correct 5 ms 5148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5028 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4976 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 4 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
22 Correct 2 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 4 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 17904 KB Output is correct
2 Correct 78 ms 17588 KB Output is correct
3 Correct 60 ms 15120 KB Output is correct
4 Correct 9 ms 6868 KB Output is correct
5 Correct 7 ms 6092 KB Output is correct
6 Correct 14 ms 7844 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 28 ms 10320 KB Output is correct
9 Correct 37 ms 13056 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 63 ms 14300 KB Output is correct
12 Correct 95 ms 16044 KB Output is correct
13 Correct 5 ms 5148 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 5028 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4976 KB Output is correct
22 Correct 2 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 2 ms 4948 KB Output is correct
27 Correct 2 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 3 ms 4948 KB Output is correct
30 Correct 3 ms 4948 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 4 ms 4948 KB Output is correct
33 Correct 3 ms 4948 KB Output is correct
34 Correct 2 ms 4948 KB Output is correct
35 Correct 2 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 4 ms 4948 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Correct 67 ms 17916 KB Output is correct
40 Correct 70 ms 17580 KB Output is correct
41 Correct 49 ms 15088 KB Output is correct
42 Correct 13 ms 6868 KB Output is correct
43 Correct 3 ms 4948 KB Output is correct
44 Correct 3 ms 4948 KB Output is correct
45 Correct 3 ms 4948 KB Output is correct
46 Correct 3 ms 4948 KB Output is correct
47 Correct 3 ms 4948 KB Output is correct
48 Correct 2 ms 4948 KB Output is correct
49 Correct 3 ms 4948 KB Output is correct
50 Correct 3 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 2 ms 4948 KB Output is correct
55 Correct 3 ms 4948 KB Output is correct
56 Correct 3 ms 4948 KB Output is correct
57 Correct 90 ms 14824 KB Output is correct
58 Correct 92 ms 14636 KB Output is correct
59 Correct 134 ms 14956 KB Output is correct
60 Correct 102 ms 14768 KB Output is correct
61 Correct 90 ms 15032 KB Output is correct
62 Correct 96 ms 14892 KB Output is correct
63 Correct 82 ms 15028 KB Output is correct
64 Correct 102 ms 15012 KB Output is correct
65 Correct 86 ms 15060 KB Output is correct
66 Correct 93 ms 14992 KB Output is correct
67 Correct 105 ms 14852 KB Output is correct
68 Correct 114 ms 14900 KB Output is correct
69 Correct 96 ms 14768 KB Output is correct
70 Correct 97 ms 14784 KB Output is correct
71 Correct 3 ms 4948 KB Output is correct
72 Correct 4 ms 5204 KB Output is correct
73 Correct 4 ms 5204 KB Output is correct
74 Correct 5 ms 5332 KB Output is correct
75 Correct 4 ms 5204 KB Output is correct
76 Correct 5 ms 5204 KB Output is correct
77 Correct 6 ms 5204 KB Output is correct
78 Correct 5 ms 5332 KB Output is correct
79 Correct 5 ms 5332 KB Output is correct
80 Correct 105 ms 14740 KB Output is correct
81 Correct 90 ms 14776 KB Output is correct
82 Correct 92 ms 15028 KB Output is correct
83 Correct 105 ms 15100 KB Output is correct
84 Correct 4 ms 5204 KB Output is correct
85 Correct 4 ms 5204 KB Output is correct
86 Correct 5 ms 5204 KB Output is correct
87 Correct 4 ms 5204 KB Output is correct
88 Correct 4 ms 5204 KB Output is correct
89 Correct 4 ms 5280 KB Output is correct
90 Correct 4 ms 5204 KB Output is correct
91 Correct 4 ms 5204 KB Output is correct
92 Correct 4 ms 5204 KB Output is correct
93 Correct 5 ms 5204 KB Output is correct
94 Correct 3 ms 4948 KB Output is correct
95 Correct 3 ms 4948 KB Output is correct
96 Correct 3 ms 4948 KB Output is correct
97 Correct 3 ms 4948 KB Output is correct
98 Correct 3 ms 4948 KB Output is correct
99 Correct 3 ms 4948 KB Output is correct
100 Correct 3 ms 4948 KB Output is correct
101 Correct 3 ms 4948 KB Output is correct
102 Correct 3 ms 4948 KB Output is correct
103 Correct 3 ms 4948 KB Output is correct
104 Correct 9 ms 6100 KB Output is correct
105 Correct 16 ms 7764 KB Output is correct
106 Correct 3 ms 4948 KB Output is correct
107 Correct 32 ms 10288 KB Output is correct
108 Correct 39 ms 13092 KB Output is correct
109 Correct 3 ms 5076 KB Output is correct
110 Correct 60 ms 14240 KB Output is correct
111 Correct 74 ms 16004 KB Output is correct
112 Correct 3 ms 5076 KB Output is correct
113 Correct 96 ms 23156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 10536 KB Output is correct
2 Correct 35 ms 10620 KB Output is correct
3 Correct 34 ms 10584 KB Output is correct
4 Correct 29 ms 10628 KB Output is correct
5 Correct 29 ms 10596 KB Output is correct
6 Correct 31 ms 11208 KB Output is correct
7 Correct 33 ms 10816 KB Output is correct
8 Correct 29 ms 10448 KB Output is correct
9 Correct 28 ms 10500 KB Output is correct
10 Correct 30 ms 10856 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 7 ms 6620 KB Output is correct
13 Correct 7 ms 6736 KB Output is correct
14 Correct 8 ms 6608 KB Output is correct
15 Correct 9 ms 6616 KB Output is correct
16 Correct 7 ms 6608 KB Output is correct
17 Correct 9 ms 6224 KB Output is correct
18 Correct 9 ms 6736 KB Output is correct
19 Correct 6 ms 6608 KB Output is correct
20 Incorrect 2 ms 4948 KB Output isn't correct
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5028 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4976 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 4 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
22 Correct 2 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 4 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5204 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 4 ms 5204 KB Output is correct
31 Correct 4 ms 5204 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 4 ms 5204 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 3 ms 4948 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Correct 4 ms 4948 KB Output is correct
40 Correct 3 ms 4948 KB Output is correct
41 Correct 3 ms 4948 KB Output is correct
42 Correct 3 ms 4948 KB Output is correct
43 Correct 3 ms 4948 KB Output is correct
44 Correct 3 ms 4948 KB Output is correct
45 Correct 3 ms 4948 KB Output is correct
46 Correct 3 ms 4948 KB Output is correct
47 Correct 2 ms 4948 KB Output is correct
48 Correct 4 ms 4948 KB Output is correct
49 Correct 3 ms 4936 KB Output is correct
50 Correct 3 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Incorrect 3 ms 4948 KB Output isn't correct
53 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 17904 KB Output is correct
2 Correct 78 ms 17588 KB Output is correct
3 Correct 60 ms 15120 KB Output is correct
4 Correct 9 ms 6868 KB Output is correct
5 Correct 7 ms 6092 KB Output is correct
6 Correct 14 ms 7844 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 28 ms 10320 KB Output is correct
9 Correct 37 ms 13056 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 63 ms 14300 KB Output is correct
12 Correct 95 ms 16044 KB Output is correct
13 Correct 5 ms 5148 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 5028 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4976 KB Output is correct
22 Correct 2 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 2 ms 4948 KB Output is correct
27 Correct 2 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 3 ms 4948 KB Output is correct
30 Correct 3 ms 4948 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 4 ms 4948 KB Output is correct
33 Correct 3 ms 4948 KB Output is correct
34 Correct 2 ms 4948 KB Output is correct
35 Correct 2 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 4 ms 4948 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Correct 67 ms 17916 KB Output is correct
40 Correct 70 ms 17580 KB Output is correct
41 Correct 49 ms 15088 KB Output is correct
42 Correct 13 ms 6868 KB Output is correct
43 Correct 3 ms 4948 KB Output is correct
44 Correct 3 ms 4948 KB Output is correct
45 Correct 3 ms 4948 KB Output is correct
46 Correct 3 ms 4948 KB Output is correct
47 Correct 3 ms 4948 KB Output is correct
48 Correct 2 ms 4948 KB Output is correct
49 Correct 3 ms 4948 KB Output is correct
50 Correct 3 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 2 ms 4948 KB Output is correct
55 Correct 3 ms 4948 KB Output is correct
56 Correct 3 ms 4948 KB Output is correct
57 Correct 90 ms 14824 KB Output is correct
58 Correct 92 ms 14636 KB Output is correct
59 Correct 134 ms 14956 KB Output is correct
60 Correct 102 ms 14768 KB Output is correct
61 Correct 90 ms 15032 KB Output is correct
62 Correct 96 ms 14892 KB Output is correct
63 Correct 82 ms 15028 KB Output is correct
64 Correct 102 ms 15012 KB Output is correct
65 Correct 86 ms 15060 KB Output is correct
66 Correct 93 ms 14992 KB Output is correct
67 Correct 105 ms 14852 KB Output is correct
68 Correct 114 ms 14900 KB Output is correct
69 Correct 96 ms 14768 KB Output is correct
70 Correct 97 ms 14784 KB Output is correct
71 Correct 3 ms 4948 KB Output is correct
72 Correct 4 ms 5204 KB Output is correct
73 Correct 4 ms 5204 KB Output is correct
74 Correct 5 ms 5332 KB Output is correct
75 Correct 4 ms 5204 KB Output is correct
76 Correct 5 ms 5204 KB Output is correct
77 Correct 6 ms 5204 KB Output is correct
78 Correct 5 ms 5332 KB Output is correct
79 Correct 5 ms 5332 KB Output is correct
80 Correct 105 ms 14740 KB Output is correct
81 Correct 90 ms 14776 KB Output is correct
82 Correct 92 ms 15028 KB Output is correct
83 Correct 105 ms 15100 KB Output is correct
84 Correct 4 ms 5204 KB Output is correct
85 Correct 4 ms 5204 KB Output is correct
86 Correct 5 ms 5204 KB Output is correct
87 Correct 4 ms 5204 KB Output is correct
88 Correct 4 ms 5204 KB Output is correct
89 Correct 4 ms 5280 KB Output is correct
90 Correct 4 ms 5204 KB Output is correct
91 Correct 4 ms 5204 KB Output is correct
92 Correct 4 ms 5204 KB Output is correct
93 Correct 5 ms 5204 KB Output is correct
94 Correct 3 ms 4948 KB Output is correct
95 Correct 3 ms 4948 KB Output is correct
96 Correct 3 ms 4948 KB Output is correct
97 Correct 3 ms 4948 KB Output is correct
98 Correct 3 ms 4948 KB Output is correct
99 Correct 3 ms 4948 KB Output is correct
100 Correct 3 ms 4948 KB Output is correct
101 Correct 3 ms 4948 KB Output is correct
102 Correct 3 ms 4948 KB Output is correct
103 Correct 3 ms 4948 KB Output is correct
104 Correct 9 ms 6100 KB Output is correct
105 Correct 16 ms 7764 KB Output is correct
106 Correct 3 ms 4948 KB Output is correct
107 Correct 32 ms 10288 KB Output is correct
108 Correct 39 ms 13092 KB Output is correct
109 Correct 3 ms 5076 KB Output is correct
110 Correct 60 ms 14240 KB Output is correct
111 Correct 74 ms 16004 KB Output is correct
112 Correct 3 ms 5076 KB Output is correct
113 Correct 96 ms 23156 KB Output is correct
114 Correct 35 ms 10536 KB Output is correct
115 Correct 35 ms 10620 KB Output is correct
116 Correct 34 ms 10584 KB Output is correct
117 Correct 29 ms 10628 KB Output is correct
118 Correct 29 ms 10596 KB Output is correct
119 Correct 31 ms 11208 KB Output is correct
120 Correct 33 ms 10816 KB Output is correct
121 Correct 29 ms 10448 KB Output is correct
122 Correct 28 ms 10500 KB Output is correct
123 Correct 30 ms 10856 KB Output is correct
124 Correct 2 ms 4948 KB Output is correct
125 Correct 7 ms 6620 KB Output is correct
126 Correct 7 ms 6736 KB Output is correct
127 Correct 8 ms 6608 KB Output is correct
128 Correct 9 ms 6616 KB Output is correct
129 Correct 7 ms 6608 KB Output is correct
130 Correct 9 ms 6224 KB Output is correct
131 Correct 9 ms 6736 KB Output is correct
132 Correct 6 ms 6608 KB Output is correct
133 Incorrect 2 ms 4948 KB Output isn't correct
134 Halted 0 ms 0 KB -