Submission #651712

#TimeUsernameProblemLanguageResultExecution timeMemory
651712beaconmcPower Plant (JOI20_power)Pypy 3
0 / 100
37 ms18176 KiB
import sys input = lambda: sys.stdin.readline().strip() sys.setrecursionlimit(1000000000) n = int(input()) if n==1: print(1) quit() edges = [[] for i in range(n+1)] par = [-1 for i in range(n+1)] visited = [False for i in range(n+1)] for i in range(n-1): a,b = map(int, input().split()) edges[a].append(b) edges[b].append(a) sus = input() root = 1 flag = False def dfs(a): for i in edges[a]: if not visited[i]: par[i] = a visited[i] = True dfs(i) visited[root] = True dfs(root) dps = [-1 for i in range(n+1)] used = [0 for i in range(n+1)] ans = -1 def dp(a): global ans if dps[a] != -1: return dps[a] sum = 0 maxi = 0 for i in edges[a]: if i == par[a]: continue sum += dps[i] maxi = max(maxi, dps[i]) if sus[a-1]=="1": ans = max(ans, maxi+1, sum-1) else: ans = max(ans,sum) dps[a] = max(sum-int(sus[a-1]), int(sus[a-1])) return dps[a] dp(root) print(ans)
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...