Submission #64788089
Source Code Expand
Copy
def all_direction_tree_dp(n, edges):G = [[] for _ in range(n)]for u, v in edges:G[u].append(v)G[v].append(u)dp = [0] * nans = [0] * ndef dfs1(v, parent):for w in G[v]:if w == parent:continuedfs1(w, v)dp[v] = max(dp[v], dp[w] + 1)def dfs2(v, parent, up):ans[v] = max(dp[v], up)children = [w for w in G[v] if w != parent]m = len(children)cand = [dp[w] + 2 for w in children]prefix = [0] * (m + 1)suffix = [0] * (m + 1)for i in range(m):
def all_direction_tree_dp(n, edges):
G = [[] for _ in range(n)]
for u, v in edges:
G[u].append(v)
G[v].append(u)
dp = [0] * n
ans = [0] * n
def dfs1(v, parent):
for w in G[v]:
if w == parent:
continue
dfs1(w, v)
dp[v] = max(dp[v], dp[w] + 1)
def dfs2(v, parent, up):
ans[v] = max(dp[v], up)
children = [w for w in G[v] if w != parent]
m = len(children)
cand = [dp[w] + 2 for w in children]
prefix = [0] * (m + 1)
suffix = [0] * (m + 1)
for i in range(m):
prefix[i+1] = max(prefix[i], cand[i])
for i in range(m-1, -1, -1):
suffix[i] = max(suffix[i+1], cand[i])
for i, w in enumerate(children):
siblings_max = max(prefix[i], suffix[i+1])
new_up = max(up + 1, siblings_max)
dfs2(w, v, new_up)
dfs1(0, -1)
dfs2(0, -1, 0)
return ans
ans=0
n1=int(input())
g1=[]
for _ in range(n1-1):
u,v=map(int,input().split())
u-=1
v-=1
g1.append((u,v))
n2=int(input())
g2=[]
for _ in range(n2-1):
u,v=map(int,input().split())
u-=1
v-=1
g2.append((u,v))
ans=n1*n2
for c in all_direction_tree_dp(n1,g1):
ans+=n2*c
for c in all_direction_tree_dp(n2,g2):
ans+=n1*c
print(ans)
Submission Info
| Submission Time | |
|---|---|
| Task | F - Add One Edge 3 |
| User | juntenbanana |
| Language | Python (PyPy 3.10-v7.3.12) |
| Score | 0 |
| Code Size | 1421 Byte |
| Status | RE |
| Exec Time | 796 ms |
| Memory | 176456 KB |
Judge Result
| Set Name | Sample | All | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 500 | ||||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 55 ms | 76536 KB |
| 00_sample_01.txt | AC | 55 ms | 76600 KB |
| 01_random_00.txt | AC | 787 ms | 159988 KB |
| 01_random_01.txt | AC | 347 ms | 103236 KB |
| 01_random_02.txt | AC | 267 ms | 96376 KB |
| 01_random_03.txt | WA | 147 ms | 86888 KB |
| 01_random_04.txt | AC | 765 ms | 152852 KB |
| 01_random_05.txt | WA | 537 ms | 128948 KB |
| 01_random_06.txt | WA | 443 ms | 118784 KB |
| 01_random_07.txt | WA | 437 ms | 114592 KB |
| 01_random_08.txt | AC | 482 ms | 119960 KB |
| 01_random_09.txt | WA | 411 ms | 113836 KB |
| 01_random_10.txt | AC | 551 ms | 176456 KB |
| 01_random_11.txt | AC | 283 ms | 125200 KB |
| 01_random_12.txt | AC | 428 ms | 154876 KB |
| 01_random_13.txt | AC | 191 ms | 101132 KB |
| 01_random_14.txt | AC | 435 ms | 152804 KB |
| 01_random_15.txt | AC | 222 ms | 109308 KB |
| 01_random_16.txt | AC | 352 ms | 139704 KB |
| 01_random_17.txt | AC | 270 ms | 118536 KB |
| 01_random_18.txt | AC | 534 ms | 175472 KB |
| 01_random_19.txt | AC | 418 ms | 156156 KB |
| 01_random_20.txt | AC | 796 ms | 159284 KB |
| 01_random_21.txt | WA | 602 ms | 132776 KB |
| 01_random_22.txt | WA | 348 ms | 106036 KB |
| 01_random_23.txt | WA | 338 ms | 104780 KB |
| 01_random_24.txt | AC | 478 ms | 122288 KB |
| 01_random_25.txt | WA | 518 ms | 126588 KB |
| 01_random_26.txt | WA | 548 ms | 130344 KB |
| 01_random_27.txt | WA | 505 ms | 125104 KB |
| 01_random_28.txt | WA | 475 ms | 121180 KB |
| 01_random_29.txt | AC | 361 ms | 105368 KB |
| 01_random_30.txt | RE | 322 ms | 136236 KB |
| 01_random_31.txt | RE | 316 ms | 135740 KB |
| 01_random_32.txt | RE | 259 ms | 114896 KB |
| 01_random_33.txt | RE | 280 ms | 119620 KB |
| 01_random_34.txt | RE | 246 ms | 113972 KB |
| 01_random_35.txt | RE | 196 ms | 99696 KB |
| 01_random_36.txt | RE | 218 ms | 100592 KB |
| 01_random_37.txt | RE | 211 ms | 99300 KB |
| 01_random_38.txt | RE | 202 ms | 94624 KB |
| 01_random_39.txt | RE | 217 ms | 105344 KB |
| 01_random_40.txt | RE | 321 ms | 135972 KB |
| 01_random_41.txt | RE | 322 ms | 136056 KB |
| 01_random_42.txt | RE | 332 ms | 147364 KB |
| 01_random_43.txt | RE | 331 ms | 138008 KB |
| 01_random_44.txt | RE | 330 ms | 135100 KB |
| 01_random_45.txt | AC | 55 ms | 76408 KB |
| 01_random_46.txt | RE | 266 ms | 126068 KB |