# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
145061 |
2019-08-18T16:11:31 Z |
MladenP |
Hard route (IZhO17_road) |
C++17 |
|
1121 ms |
62456 KB |
#include<bits/stdc++.h>
#define STIZE(x) fprintf(stderr, "STIZE%d\n", x);
#define PRINT(x) fprintf(stderr, "%s = %d\n", #x, x);
#define NL(x) printf("%c", " \n"[(x)]);
#define lld long long
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define endl '\n'
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 500010
int N, A, B, X, Y, dist1[MAXN], dist2[MAXN], len[MAXN], cnt[MAXN], L[MAXN];
lld C[MAXN];
vector<int> adj[MAXN];
void dfsMerge(int node, int prev) {
lld maxi1 = 0, maxi2 = 0;
for(auto x : adj[node]) {
if(x != prev) {
dfsMerge(x, node);
if(len[x]+1 >= maxi1) maxi2 = maxi1, maxi1 = len[x]+1;
else if(len[x]+1 >= maxi2) maxi2 = len[x]+1;
}
}
if(maxi2 == 0) return;
L[node] = maxi1+maxi2;
if(maxi1 == maxi2) {
lld sum = 0;
for(auto x : adj[node]) {
if(x != prev) {
if(len[x]+1 == maxi1) C[node] -= cnt[x]*cnt[x], sum += cnt[x];
}
}
C[node] += sum*sum;
C[node] /= 2;
} else {
lld cnt1 = 0, cnt2 = 0;
for(auto x : adj[node]) {
if(x != prev) {
if(len[x]+1 == maxi1) cnt1 += cnt[x];
if(len[x]+1 == maxi2) cnt2 += cnt[x];
}
}
C[node] = cnt1*cnt2;
}
}
void dfsDist(int node, int prev, int dist[]) {
dist[node] = dist[prev]+1;
for(auto x : adj[node]) {
if(x != prev) dfsDist(x, node, dist);
}
}
pii dfsMaxPut(int node, int prev) {
cnt[node] = 1;
for(auto x : adj[node]) {
if(x != prev) {
pii cur = dfsMaxPut(x, node);
if(cur.fi+1 > len[node]) {
len[node] = cur.fi+1;
cnt[node] = cur.se;
} else if(cur.fi+1 == len[node]) cnt[node] += cur.se;
}
}
return {len[node], cnt[node]};
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin >> N;
for(int i = 1; i < N; i++) {
int x, y; cin >> x >> y;
adj[x].pb(y); adj[y].pb(x);
}
dist1[1] = -1; dfsDist(1, 1, dist1);
for(int i = 1; i <= N; i++) if(dist1[i] > dist1[A]) A = i;
dist1[A] = -1; dfsDist(A, A, dist1);
for(int i = 1; i <= N; i++) if(dist1[i] > dist1[B]) B = i;
dist2[B] = -1; dfsDist(B, B, dist2);
int rast = INF, prec = 0;
for(int i = 1; i <= N; i++) {
int eks = max(dist1[i], dist2[i]);
prec = max(prec, dist1[i]);
if(eks == rast) Y = i;
else if(eks < rast) X = i, Y = 0, rast = eks;
}
if(prec == N-1) {
cout << 0 << ' ' << 1 << endl;
return 0;
}
lld score = 0, Cnt = 0;
if(Y == 0) {
dfsMaxPut(X, X);
dfsMerge(X, X);
for(int i = 1; i <= N; i++) {
if(i != X) {
lld cur = (lld)(prec-len[i])*(L[i]);
if(cur > score) {
Cnt = C[i];
score = cur;
} else if(cur == score) Cnt += C[i];
}
}
///PUTEVI KOJI PROLAZE KROZ CENTAR
if(adj[X].size() > 2) {
lld maxi1 = 0, maxi2 = 0, br = 0, cnt1 = 0, cnt2 = 0;
for(auto x : adj[X]) {
if(len[x]+1 > maxi1) maxi2 = maxi1, maxi1 = len[x]+1;
else if(len[x]+1 > maxi2) maxi2 = len[x]+1;
}
for(auto x : adj[X]) {
if(len[x]+1 == maxi1) br++, cnt1 += cnt[x];
if(len[x]+1 == maxi2) cnt2 += cnt[x];
}
if(br <= 2) {
if(maxi1*(maxi1+maxi2) > score) score = maxi1*(maxi1+maxi2), Cnt = cnt1*cnt2;
else if(maxi1*(maxi1+maxi2) == score) Cnt += cnt1*cnt2;
} else {
lld curS = cnt1*cnt1;
for(auto x : adj[X]) {
if(len[x]+1 == maxi1) curS -= cnt[x]*cnt[x];
}
curS /= 2;
if(2*maxi1*maxi1 > score) score = 2*maxi1*maxi1, Cnt = curS;
else if(2*maxi1*maxi1 == score) Cnt += curS;
}
}
///
} else {
dfsMaxPut(X, Y); dfsMaxPut(Y, X);
dfsMerge(X, Y); dfsMerge(Y, X);
for(int i = 1; i <= N; i++) {
lld cur = (lld)(prec-len[i])*(L[i]);
if(cur > score) {
Cnt = C[i];
score = cur;
} else if(cur == score) Cnt += C[i];
}
}
cout << score << ' ' << Cnt << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
15 ms |
12024 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
13 ms |
12152 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
13 ms |
12152 KB |
Output is correct |
8 |
Correct |
13 ms |
12152 KB |
Output is correct |
9 |
Correct |
13 ms |
12152 KB |
Output is correct |
10 |
Correct |
13 ms |
12168 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12152 KB |
Output is correct |
14 |
Correct |
16 ms |
12148 KB |
Output is correct |
15 |
Correct |
16 ms |
12152 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
12 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
13 ms |
12148 KB |
Output is correct |
20 |
Correct |
13 ms |
12156 KB |
Output is correct |
21 |
Correct |
13 ms |
12152 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
17 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
15 ms |
12024 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
13 ms |
12152 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
13 ms |
12152 KB |
Output is correct |
8 |
Correct |
13 ms |
12152 KB |
Output is correct |
9 |
Correct |
13 ms |
12152 KB |
Output is correct |
10 |
Correct |
13 ms |
12168 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12152 KB |
Output is correct |
14 |
Correct |
16 ms |
12148 KB |
Output is correct |
15 |
Correct |
16 ms |
12152 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
12 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
13 ms |
12148 KB |
Output is correct |
20 |
Correct |
13 ms |
12156 KB |
Output is correct |
21 |
Correct |
13 ms |
12152 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
17 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
25 |
Correct |
15 ms |
12664 KB |
Output is correct |
26 |
Correct |
15 ms |
12664 KB |
Output is correct |
27 |
Correct |
15 ms |
12664 KB |
Output is correct |
28 |
Correct |
16 ms |
12536 KB |
Output is correct |
29 |
Correct |
15 ms |
12536 KB |
Output is correct |
30 |
Correct |
15 ms |
12540 KB |
Output is correct |
31 |
Correct |
16 ms |
12540 KB |
Output is correct |
32 |
Correct |
15 ms |
12536 KB |
Output is correct |
33 |
Correct |
15 ms |
12604 KB |
Output is correct |
34 |
Correct |
15 ms |
12664 KB |
Output is correct |
35 |
Correct |
15 ms |
12536 KB |
Output is correct |
36 |
Correct |
15 ms |
12640 KB |
Output is correct |
37 |
Correct |
15 ms |
12536 KB |
Output is correct |
38 |
Correct |
16 ms |
12536 KB |
Output is correct |
39 |
Correct |
17 ms |
12536 KB |
Output is correct |
40 |
Correct |
17 ms |
12408 KB |
Output is correct |
41 |
Correct |
15 ms |
12408 KB |
Output is correct |
42 |
Correct |
16 ms |
12408 KB |
Output is correct |
43 |
Correct |
15 ms |
12408 KB |
Output is correct |
44 |
Correct |
16 ms |
12436 KB |
Output is correct |
45 |
Correct |
15 ms |
12380 KB |
Output is correct |
46 |
Correct |
15 ms |
12280 KB |
Output is correct |
47 |
Correct |
15 ms |
12408 KB |
Output is correct |
48 |
Correct |
15 ms |
12408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
15 ms |
12024 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
13 ms |
12152 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
13 ms |
12152 KB |
Output is correct |
8 |
Correct |
13 ms |
12152 KB |
Output is correct |
9 |
Correct |
13 ms |
12152 KB |
Output is correct |
10 |
Correct |
13 ms |
12168 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12152 KB |
Output is correct |
14 |
Correct |
16 ms |
12148 KB |
Output is correct |
15 |
Correct |
16 ms |
12152 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
12 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
13 ms |
12148 KB |
Output is correct |
20 |
Correct |
13 ms |
12156 KB |
Output is correct |
21 |
Correct |
13 ms |
12152 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
17 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
25 |
Correct |
15 ms |
12664 KB |
Output is correct |
26 |
Correct |
15 ms |
12664 KB |
Output is correct |
27 |
Correct |
15 ms |
12664 KB |
Output is correct |
28 |
Correct |
16 ms |
12536 KB |
Output is correct |
29 |
Correct |
15 ms |
12536 KB |
Output is correct |
30 |
Correct |
15 ms |
12540 KB |
Output is correct |
31 |
Correct |
16 ms |
12540 KB |
Output is correct |
32 |
Correct |
15 ms |
12536 KB |
Output is correct |
33 |
Correct |
15 ms |
12604 KB |
Output is correct |
34 |
Correct |
15 ms |
12664 KB |
Output is correct |
35 |
Correct |
15 ms |
12536 KB |
Output is correct |
36 |
Correct |
15 ms |
12640 KB |
Output is correct |
37 |
Correct |
15 ms |
12536 KB |
Output is correct |
38 |
Correct |
16 ms |
12536 KB |
Output is correct |
39 |
Correct |
17 ms |
12536 KB |
Output is correct |
40 |
Correct |
17 ms |
12408 KB |
Output is correct |
41 |
Correct |
15 ms |
12408 KB |
Output is correct |
42 |
Correct |
16 ms |
12408 KB |
Output is correct |
43 |
Correct |
15 ms |
12408 KB |
Output is correct |
44 |
Correct |
16 ms |
12436 KB |
Output is correct |
45 |
Correct |
15 ms |
12380 KB |
Output is correct |
46 |
Correct |
15 ms |
12280 KB |
Output is correct |
47 |
Correct |
15 ms |
12408 KB |
Output is correct |
48 |
Correct |
15 ms |
12408 KB |
Output is correct |
49 |
Correct |
719 ms |
60668 KB |
Output is correct |
50 |
Correct |
754 ms |
60680 KB |
Output is correct |
51 |
Correct |
731 ms |
60620 KB |
Output is correct |
52 |
Correct |
716 ms |
60668 KB |
Output is correct |
53 |
Correct |
558 ms |
60340 KB |
Output is correct |
54 |
Correct |
599 ms |
60408 KB |
Output is correct |
55 |
Correct |
588 ms |
60508 KB |
Output is correct |
56 |
Correct |
606 ms |
60388 KB |
Output is correct |
57 |
Correct |
776 ms |
60636 KB |
Output is correct |
58 |
Correct |
754 ms |
60664 KB |
Output is correct |
59 |
Correct |
734 ms |
60540 KB |
Output is correct |
60 |
Correct |
711 ms |
60408 KB |
Output is correct |
61 |
Correct |
810 ms |
62456 KB |
Output is correct |
62 |
Correct |
831 ms |
62416 KB |
Output is correct |
63 |
Correct |
1086 ms |
52088 KB |
Output is correct |
64 |
Correct |
1078 ms |
47608 KB |
Output is correct |
65 |
Correct |
1071 ms |
44960 KB |
Output is correct |
66 |
Correct |
1121 ms |
43744 KB |
Output is correct |
67 |
Correct |
1050 ms |
43256 KB |
Output is correct |
68 |
Correct |
1109 ms |
42996 KB |
Output is correct |
69 |
Correct |
1038 ms |
42872 KB |
Output is correct |
70 |
Correct |
1035 ms |
42860 KB |
Output is correct |
71 |
Correct |
1044 ms |
42744 KB |
Output is correct |
72 |
Correct |
1089 ms |
42872 KB |
Output is correct |
73 |
Correct |
1095 ms |
42892 KB |
Output is correct |
74 |
Correct |
1045 ms |
42948 KB |
Output is correct |
75 |
Correct |
1032 ms |
42996 KB |
Output is correct |
76 |
Correct |
999 ms |
43004 KB |
Output is correct |
77 |
Correct |
696 ms |
43556 KB |
Output is correct |
78 |
Correct |
438 ms |
42852 KB |
Output is correct |