We need to solve: given a graph (undirected) with N nodes (up to 1e5) and M edges (M up to 1e5, also at least N-1). Each node has weight w_i (positive). Over N days, on day i we "inspect" node i, meaning node i is removed from the graph, along with all its incident edges. The remaining graph may break into several connected components. Among these components, we operate the one with the maximum total weight (sum of w in that component). If there is a tie? Not specified; but we output the total performance value of the cluster operated. Likely if multiple have same max, we still output that max value (since it's the highest overall performance value, there could be multiple, but they have same total performance; any is fine? However we need output the value; it's just the max total weight among the components). So for each i, we need to compute max component weight after removing node i.
We need to compute for all i efficiently.
Observations: The graph is arbitrary, but M is up to 1e5, N up to 1e5. Removing a node can cause the graph to split into up to deg(i) components (one for each neighbor's subtree if we think of biconnected? Actually removing a cut vertex). So we need to compute, for each node, the maximum component size (by weight) among the connected components formed when that node is removed.
This is reminiscent of "finding articulation points and compute sizes of subtrees when removed". But the graph may have cycles, not a tree. However we can think of building a DFS tree and using lowlink to identify articulation points, but we need to compute the sizes of components after removal of a node (including those that are not trees). For any node v, after removal, the graph splits into components corresponding to each "child" subtree that is not connected back to ancestors (i.e., via back edges), plus possibly an "upward" component containing the rest of the graph (the part connected to the parent). For non-articulation points, removal doesn't split; the graph remains connected except for v itself, so there is only one component (the rest of the graph), and its total weight = total_weight_all - w_v. That component is operated, so answer = total_sum - w_i. BUT if the removed node is isolated? Actually after removal, the remaining nodes may still be connected? If v is not articulation, then the graph (excluding v) is still connected, so the only component is all other nodes. So answer = total_sum - w_i. However note: if the graph originally is not connected? But constraints: they say "A large-scale cluster ... consists of N computers and M communication channels that can communicate with each other." Usually cluster implies whole system is connected? But not explicitly; M can be as low as N-1, which could form a tree, which is connected. But they don't guarantee connectivity? They say "cluster" is a set of nodes connected by physical communication paths. The description says: "consists of N computers (nodes) and M physical communication channels that can communicate with each other." That phrasing might imply the whole system is connected? Actually they say "that can communicate with each other" possibly meaning each channel allows communication between its two nodes, but not that the whole graph is connected. However typical interpretation: the cluster is a system that combines computers into a single unit, usually it's connected. Also the sample: first sample graph seems connected (9 nodes, 10 edges). Second sample: 16 nodes, 19 edges, appears connected. So likely the input graph is connected. But we should handle possibly disconnected? The problem statement: "the entire cluster of SAKURA is divided into one or more clusters, and one of them is operated in place of SAKURA." So if originally it's disconnected? Actually if the original cluster is not connected, then it's already multiple components; but they said "cluster ... consists of N computers and M communication channels that can communicate with each other." This might imply they are all interconnected; so I'll assume the original graph is connected. But we need not rely heavily, could check. However M >= N-1 suggests maybe connectivity because minimal edges to connect N nodes is N-1. But it's not guaranteed; you could have N=3, M=2, which is N-1, but if edges are 1-2 and 2-3, it's connected. If edges are 1-2 and 1-2? No duplicates. So M >= N-1 likely implies graph is connected? Not necessarily: you could have a graph with N=4, M=3: could be 1-2,1-3,1-4 (connected) or 1-2,2-3,1-3 (triangle) and node 4 isolated? But then edges are 3, N-1=3, but node 4 isolated means total edges 3, but M is total edges, includes those connecting isolated? Actually isolated node has degree 0, but M = N-1, if one node isolated, the rest must form a tree on N-1 nodes with N-2 edges, total edges N-2, not N-1. So to have an isolated node, total edges would be <= N-2. Because with N nodes, to have at least one isolated node and the rest connected, max edges is (N-1 choose 2) if rest complete? But constraints M >= N-1 ensures at least N-1 edges, which is the minimum for connectivity. So if M >= N-1, it's possible to be disconnected only if there are multiple components and each component has at least some edges; but the total number of edges needed to have k components each a tree is N - k. Since M >= N-1, we have N - k <= M, so k <= 1? Actually M >= N-1 implies N-1 <= M, but if there are k components, the minimum edges to connect each component as a tree is N - k. So we require N - k <= M, which is always true for M >= N-1? Because N - k <= N - 1 implies k >= 1. So k could be >1 if M is larger, e.g., N=4, M=4, could be two components: a triangle (3 edges) and an isolated node (0 edges) total 3 edges not 4. Actually to have two components, each component could have extra edges, but total edges could be >= N-1 even if graph disconnected. For example N=4, M=4: component1: nodes 1-2-3 forming triangle (3 edges), component2: node 4 isolated (0 edges), total edges=3 not 4. To get 4 edges with two components, you'd need both components to have cycles, but max edges for 3 nodes is 3, for 1 node is 0, total 3. So with N=4, maximum edges in disconnected graph with at least 2 components is less than N*(N-1)/2 but could be up to? If split into 2 and 2, max edges = 1+1=2? Actually complete graph on 2 nodes has 1 edge; two such components total 2 edges. So not >=3? For N=4, M=3 could be triangle+isolated, but that's 3 edges exactly, so M=N-1=3 gives a triangle+isolated? That's possible: nodes 1,2,3 in triangle (3 edges) and node 4 isolated; total edges=3, M=3, N=4, M=N-1 holds. So indeed M = N-1 can correspond to a connected tree (N-1 edges) or a graph with an isolated node and a complete graph on N-1 nodes? But complete graph on N-1 nodes would have (N-1)(N-2)/2 edges, which is >> N-1 for N>3. So to have M = N-1, if there is an isolated node, the other N-1 nodes must form a tree, which has exactly N-2 edges, not N-1. Because tree on N-1 nodes has (N-1)-1 = N-2 edges. So total edges = N-2, not N-1. So to have total M = N-1, if one isolated node, the rest must have N-1 edges, but N-1 nodes can't have N-1 edges and be connected? For N-1 nodes, minimum edges to be connected is (N-2), maximum is (N-1)(N-2)/2. So N-1 edges is possible if the component on N-1 nodes has N-1 edges, which is one more than tree, i.e., it has one cycle. So example: N=5, M=4. If one isolated node, other 4 nodes can have 4 edges, e.g., a cycle of 4 nodes (4 edges) or a triangle with an extra leaf? Actually triangle (3 edges) plus a leaf attached (1 edge) total 4 edges, but that leaf makes it a tree plus an extra edge? Let's count: nodes 1,2,3,4. Edges: 1-2,2-3,3-1 (triangle) and 3-4 (leaf) => 4 edges, connected. So yes, possible. So M = N-1 does not guarantee connectivity. But the problem statement likely intends the cluster to be a connected system. The phrase "that can communicate with each other" might be ambiguous. The sample inputs are connected. I think it's safe to assume the initial graph is connected. However to be robust, we could compute for each node, the maximum component after removal, which might be relevant even if initial graph not connected, but we need to output total performance value of the cluster operated. If the initial graph has multiple components, the cluster is the whole set? Actually they say "the entire cluster of SAKURA is divided into one or more clusters". Initially, before any inspection, the whole system is presumably one cluster (the entire SAKURA). If it's disconnected initially, then it's already multiple clusters? But they say "consists of N computers and M physical communication channels that can communicate with each other." That suggests they are interconnected. So it's connected. So I'll assume connectivity.
Thus for non-articulation nodes, answer = total_sum - w_i.
For articulation nodes, we need to compute the maximum total weight among the components formed by removing v (including the component containing the rest of graph beyond v's children in DFS). However careful: when v is removed, components correspond to each "block" that becomes disconnected. In an undirected graph, removing a vertex v splits the graph into components equal to the number of "children subtrees" that are not connected to ancestors via back edges, plus possibly an "upward" component (the part of the graph not in any of those subtrees). However if v is the root of DFS, there is no upward component; instead the components are just the subtrees of each child that is not linked to ancestors (since root has no ancestors). Actually for root, if it has k children in the DFS tree that are not connected via back edges across, then removal yields exactly k components (each child's subtree). But note that if there are back edges between different child subtrees? In undirected graph, back edges only go to ancestors, not across subtrees (since edges are undirected, any edge between two different subtrees would be a cross edge, but in undirected DFS, any non-tree edge connects a node to an ancestor or descendant, not to a node in a different subtree unless it's an ancestor. Actually in undirected DFS, there are no cross edges; every edge is either tree edge or back edge (to ancestor). So there is no connection between different subtrees other than through the root. So removal of root yields components exactly corresponding to each child subtree if each child's subtree has no back edge to root's ancestors? root has no ancestors; so each child subtree is isolated from others if there is no edge connecting them except via root. Since only back edges are to ancestors, and root has no ancestors, any back edge from a node in a child subtree must go to an ancestor of that node. The only ancestors are root or deeper. Could it go to a node in another child subtree? That would be a cross edge, but in undirected DFS it can't because when exploring, if there is an edge to a node already visited that is not the parent, it's a back edge to an ancestor (which could be root or higher). But if that visited node is in a different subtree, it would have been visited earlier but not an ancestor; but in undirected DFS, when you encounter an already visited node, it's either the parent (tree edge) or some node that was visited earlier. However that earlier node could be in a different subtree only if that edge connects two nodes that are not ancestor/descendant? But because the graph is undirected, if you have an edge (u,v) where both have been visited, one must have been visited before the other. Suppose we traverse root, then go to child A subtree, then later to child B subtree. If there is an edge between a node in A subtree and a node in B subtree, then when exploring A's subtree, we would see a neighbor that is not visited yet? Actually B's nodes are not visited when A is being explored (since B comes later). So edge from A's node to B's node would be discovered as a tree edge when we first encounter B's node from A's node? But the DFS order: starting at root, we go to child A and fully explore its subtree before backtracking to root and then going to child B. So when exploring A's subtree, any neighbor that hasn't been visited will be visited as part of that subtree, but if that neighbor is in B's subtree (which hasn't been visited), then DFS would actually go from A's node to that neighbor and explore it, making it part of A's subtree, not B. So the structure ensures that there cannot be an edge connecting two nodes in different subtrees of the root without being discovered as a tree edge, which would merge them into the same subtree. Therefore, in undirected DFS, the subtrees of children are indeed disconnected when root is removed. So removal of root yields components each being the set of nodes in each child's DFS subtree (including that child). But careful: back edges within a subtree might connect to ancestors (including root). Those back edges don't connect to other subtrees. So yes, components are exactly those subtrees.
For a non-root articulation point v, removal splits the graph into components: for each child c of v in DFS tree such that there is no back edge from the subtree of c to an ancestor of v (i.e., low[c] >= disc[v]), then that subtree becomes a separate component. Additionally, the rest of the graph (the "parent" component) which includes all nodes not in those subtrees (including ancestors and also child subtrees that have a back edge to ancestor, i.e., low[c] < disc[v]) stays connected. So the sizes (weights) of components are: for each child c where low[c] >= disc[v], the weight of subtree rooted at c (including c). Also the component containing the parent side: total_weight_all - w_v - sum_of_weights_of_separated_subtrees. That includes ancestors and also any child subtrees that are not separated.
Thus we can compute for each node v: Let total_sum = sum w_i.
We need to compute, for each node, the maximum of:
- For each child c in DFS tree such that low[c] >= disc[v] (i.e., v is articulation for that child), the weight of subtree rooted at c (call it sub_weight[c]).
- The weight of the "rest" component: total_sum - w_v - sum_{c: low[c] >= disc[v]} sub_weight[c].
Also possibly there is the component consisting of v alone? No, v is removed, so not considered.
If v is not articulation, then there is no child with low[c] >= disc[v] (except possibly root? Actually for non-articulation, either it's not cut vertex: for all children, low[c] < disc[v], and also if root, it's articulation if it has >1 children. So non-articulation root? Root is articulation if children >1, else not). For non-articulation nodes, the set of separated subtrees is empty, so the only component after removal is the whole graph minus v: weight = total_sum - w_v. So answer = total_sum - w_v.
Thus algorithm: Do a DFS (or any traversal) to compute articulation points using Tarjan. During DFS, also compute subtree weights (sum of w of nodes in the DFS subtree rooted at each node, including the node). Also compute low values.
But careful: The DFS tree must be defined with a root (say 1). For each node v (non-root), we need to identify which children cause separation.
Standard condition for articulation point:
- For root: if it has >1 children in DFS tree, then it's articulation.
- For non-root: if there exists a child c such that low[c] >= disc[v], then v is articulation.
When v is articulation, the components are those child subtrees where low[c] >= disc[v] (each becomes a separate component) and the rest.
But we also need to consider the case where the "rest" component might be empty? For root with >1 children, the rest component after removal? Removing root yields components for each child subtree, but there is no "rest" because root's ancestors don't exist. So the only components are those child subtrees. So the maximum among them. For non-root, the rest component includes the parent side, which is not empty because at least the parent exists (and possibly other parts). So we need to compute its weight.
So algorithm outline:
- Compute total_sum.
- Build adjacency list.
- Perform DFS from node 1 (assuming graph connected) to compute:
- disc (dfs order time)
- low
- subtree_weight[v] = w_v + sum subtree_weight[children] (children in DFS tree)
- For each node v, maintain a list or compute max component weight when v removed.
- For root (node 1):
- Count number of children (in DFS tree) that are not visited from v? Actually children count from DFS.
- If root has k children, then after removing root, the components are each child subtree. So the candidate weights are subtree_weight[child] for each child.
- The maximum is max over those subtree_weights.
- (If root has only one child, then root is not articulation; answer = total_sum - w_root.)
- For non-root v:
- During DFS, after processing children, we can compute:
- Let separated_sum = 0
- max_component = 0
- For each child c:
- If low[c] >= disc[v]: then subtree c becomes separate component of weight subtree_weight[c].
separated_sum += subtree_weight[c]
max_component = max(max_component, subtree_weight[c])
- else: then subtree c remains attached to parent side; its weight is included in the rest.
- The rest component weight = total_sum - w_v - separated_sum.
- max_component = max(max_component, rest_component)
- answer[v] = max_component
- Note: If there is no child with low[c] >= disc[v], then separated_sum = 0, rest_component = total_sum - w_v, and max_component = total_sum - w_v, so answer[v] = total_sum - w_v (consistent with non-articulation).
- But careful: In case v has a child c where low[c] >= disc[v] but also there might be a back edge from c's subtree to v? Actually low[c] is the minimum discovery time reachable from subtree of c via back edges. Since v is parent, low[c] >= disc[v] means no back edge from c's subtree to any ancestor of v (including v itself?). Actually low[c] is min of disc of nodes reachable via back edges from any node in subtree of c (including c). If there is a back edge from c or its descendant to v, then disc[v] is less than low? Wait, disc[v] is the discovery time of v. low[c] is the minimum disc among nodes reachable via back edges. If there is an edge from subtree of c to v (which is the parent), then that edge is a tree edge? Actually v-c is a tree edge, so it's not a back edge. But could there be an edge from a descendant of c to v? That would be a back edge to an ancestor (v). In that case, low[c] would be at most disc[v]. Since disc[v] < disc[c] (as v discovered earlier), if such a back edge exists, low[c] = min(low[c], disc[v]) => low[c] <= disc[v]. But we want low[c] >= disc[v] to be a separating child. Actually condition low[c] >= disc[v] means there is no back edge from subtree of c to any ancestor of v (including v). If there is a back edge to v itself, then low[c] would be <= disc[v], thus low[c] < disc[v]? Possibly equal if disc[v] is the min? Actually disc[v] is a specific integer. If there is a direct back edge from some node in subtree of c to v, then low[c] = min(..., disc[v]) <= disc[v]. Since disc[v] is less than disc of any node in subtree (except maybe if there is an edge to itself? not). So low[c] <= disc[v]. For it to be >= disc[v], there must be no back edge to any ancestor, thus low[c] equals disc[c]? Actually low[c] is at least disc[v]? Wait, low[c] is the minimum discovery time reachable, which will be at most disc[c] (since c itself). If no back edges, low[c] = disc[c] (which is > disc[v] because v is parent). So low[c] > disc[v]. So condition low[c] >= disc[v] is typical for articulation: if low[c] >= disc[v], then removing v disconnects subtree of c from the rest. Yes.
But what about a child c where low[c] = disc[v]? That would happen if there is a back edge from subtree of c to v (or to an ancestor with same disc? Actually disc[v] is smallest among ancestors). If there is a back edge directly to v, then low[c] could be disc[v] which is less than disc[v]? Actually disc[v] is equal, so low[c] = disc[v]. Then low[c] >= disc[v] is true (since equal). But typical articulation condition uses low[c] >= disc[v] for non-root; that includes equality? In many definitions, if there is a back edge from subtree to v, then removal of v does NOT disconnect that subtree because it still can reach v? Wait, v is being removed, so the back edge to v becomes useless because v is gone. Actually consider v and child c; if there is a back edge from subtree of c to v itself, after removing v, the back edge is gone, so that subtree loses connection to ancestors via v; but maybe there is also a back edge to some ancestor above v? If there is a back edge to v only, then after removing v, the subtree c cannot reach above v because that back edge goes to v which is removed, and the only path to rest is through v (tree edge). So indeed it becomes disconnected. So low[c] = disc[v] (due to back edge to v) should still count as a separating child? Let's analyze: Suppose v is a node, c is child. There is an edge from some node in c's subtree to v (other than the tree edge). Then in the graph, there is a cycle: v - ... - that node - v. When we remove v, the subtree c is still connected to v? No, v is gone, so the edge to v is lost. So the subtree c might still be connected to other parts if there is a path from that node to some ancestor of v via other nodes? But the back edge is directly to v, not to an ancestor above v. So after removal, subtree c is isolated? However, there might be another path: from c's subtree, through some other child of v? That would require a cross edge between subtrees, which doesn't exist in undirected DFS (but could exist via back edges to ancestors above v?). Let's think: In undirected graph, an edge between a node in c's subtree and v is a tree edge? Actually v is parent, so edge v-c is tree. Additional edge from a node x in c's subtree to v is a back edge (since v is ancestor). That creates a cycle. When v is removed, x loses connection to v, but x still is in subtree c, and there is still the tree edge from c to v? But v removed, so c loses that connection. So all nodes in subtree c may still be connected among themselves, but they have no connection to nodes outside subtree c (because any path to outside would have to go through v or through some other edge from subtree c to outside). Could there be an edge from subtree c to some node outside that is not v? That would be a back edge to an ancestor of v (like v's parent) or a cross edge to another subtree. But cross edge doesn't exist in undirected DFS; any edge from subtree c to a node not in its subtree and not an ancestor would have been discovered as tree edge when exploring c, making that node part of c's subtree. So any such edge would be to an ancestor (including v or higher). So if there is a back edge to v's parent (u), then low[c] would be <= disc[u] which is < disc[v] (since parent discovered earlier). Then low[c] < disc[v], so condition low[c] >= disc[v] fails. In that case, after removal of v, subtree c can still reach u via that back edge, so it remains connected to the rest (through u). So only when low[c] >= disc[v] (including equality) does the subtree c have no back edge to any ancestor strictly above v? Actually equality low[c] = disc[v] indicates the highest back edge reaches v itself (or maybe some node with same disc as v? But disc[v] is unique). So indeed low[c] >= disc[v] means that the highest (minimum disc) reachable from subtree c is v or later (i.e., no connection to ancestors of v). But if it reaches v exactly, that still does not provide an alternative route when v is removed. So that subtree becomes separate. So condition should be low[c] >= disc[v] (including equality) for the child to be separated. That's standard articulation point condition (for non-root) uses low[c] >= disc[v]. I've seen both > and >=; typical is low[c] >= disc[v] (including equality) because if there's a back edge to v, v's removal still isolates that subtree. Actually many textbooks use low[v] >= disc[u] for edge (u,v) to be a bridge; for articulation point, condition is low[child] >= disc[node]. This includes equality. So we should use >=.
Thus for each child c where low[c] >= disc[v], that subtree is a separate component.
Now we also need to be careful about the root: For root, condition is number of children in DFS tree > 1. Then components are subtree_weights of each child. So answer = max(child subtree_weights).
Now we also need to compute subtree_weight correctly: subtree_weight includes the sum of w of all nodes in the DFS tree rooted at v (including v). But when we are removing v, we consider components formed by subtrees of children; each such component's weight is subtree_weight[child] because child's subtree is still connected after v removal (since edges within subtree remain). But what about nodes that are not in any child's subtree but are ancestors? For non-root, the rest component includes all nodes not in v's subtree plus those child subtrees that are not separated. However, our DFS tree is rooted at 1. For a node v, its subtree includes v and all descendants. The rest of the graph (nodes not in subtree) are ancestors and possibly nodes in other branches that are not descendants. However, if there is a back edge from a node in v's subtree to some ancestor above v, that could connect that subtree to the rest even after v removal? Actually the separation condition is based on low[c] for each child. For a child c where low[c] < disc[v], that subtree is still connected to ancestors (via that back edge), so it becomes part of the rest component. But careful: For that child's subtree, its nodes are in v's subtree. So when we compute the rest component weight as total_sum - w_v - sum_{separated} subtree_weight[child], that automatically includes those non-separated child subtrees as part of the rest? Actually total_sum - w_v includes all nodes except v. Then we subtract separated_subtrees, leaving the rest which includes all nodes not in v's subtree? Wait, nodes not in v's subtree are not in any child subtree, they are outside v's entire subtree. Those are definitely in the rest component. For a child subtree that is not separated (low[c] < disc[v]), its nodes are part of the rest component because they can reach ancestors via back edges, so they should not be subtracted. So yes, we subtract only separated subtrees. So rest = total_sum - w_v - sum_{c separated} subtree_weight[c].
But is it possible that some nodes in v's subtree are not in any child subtree? That is just v itself, which we removed. So correct.
But is it always that the rest component is connected? In a non-articulation scenario, yes. In articulation scenario, the rest component includes the parent side and also the non-separated child subtrees; these are all connected because the non-separated child subtrees have back edges to ancestors (above v) that keep them connected. The ancestors and those subtrees are connected via those back edges even without v. Also any non-separated child subtrees might be connected to each other via the ancestor region? Possibly they may also have interconnections among themselves? But as argued, there are no cross edges between subtrees; but they could have back edges to common ancestors, thus they are all connected through the ancestor region. So the rest component is a single connected component.
So answer = max( max_{separated child} subtree_weight[child], total_sum - w_v - sum_{separated child} subtree_weight[child] ).
Edge Cases: If the rest component is empty? For non-root v, can it be empty? That would mean that after removing v, all other nodes are in separated child subtrees. That would happen if v is a root? For non-root, there is at least the parent node (and possibly ancestors) which are not in v's subtree, so rest component includes those. Unless v's parent is only node in rest and it might be empty only if the graph has only v? But N>=2. So rest component is non-empty. For root with >1 children, there is no rest component; we only consider child subtrees. But note: For root, if it has k>1 children, the rest component (the part not in any child subtree) is none; but also there could be ancestors? No. So answer is max of child subtree weights.
But what about root with 1 child? Then root is not articulation, answer = total_sum - w_root.
Now we need to compute subtree_weight for each node. Standard DFS.
Implementation steps:
-
Read N, M.
-
Read array w[1..N].
-
Build adjacency list.
-
total_sum = sum w_i.
-
Initialize arrays:
- disc[N+1] (discovery time, set to 0 initially)
- low[N+1]
- subtree_weight[N+1]
- ans[N+1] (to store result for each node)
- maybe parent[N+1] to track parent in DFS tree.
- time counter.
-
Perform DFS from node 1 (or any start, assuming graph connected). Use iterative stack or recursion with caution for recursion depth up to 1e5; in Python recursion limit may be insufficient; use iterative DFS or set recursion limit high. But Python recursion can handle 1e5 with sys.setrecursionlimit(300000). However there is risk of stack overflow, but typical CP environment may allow. Safer to implement iterative DFS with explicit stack. But we need to compute low and subtree_weight in post-order. So we could do recursive with increased limit, but might still cause recursion depth error if graph is a long chain (N=1e5). Python default recursion limit ~1000; we can set to 200000. Should be okay but sometimes recursion depth > 1000 causes RecursionError; but we can increase limit. However recursion in Python is slower and uses call stack; but for 1e5 it may be okay if not too deep. But I'm comfortable with recursive with sys.setrecursionlimit(300000). Many solutions do that. We'll use recursion.
Edge Cases: Graph may not be connected? If not, we need to handle. Let's think: If graph is not connected initially, the cluster is not a single unit? The problem might not consider that. But just in case, we can treat each connected component separately? But then when removing a node, the operation picks the cluster with highest total performance among all components formed. Initially there might be multiple components? Actually the whole SAKURA is a cluster; it should be one connected system. So I'll ignore.
Now compute DFS:
def dfs(v, parent):
nonlocal time
time += 1
disc[v] = low[v] = time
subtree_weight[v] = w[v]
child_count = 0
separated_sum = 0
max_comp = 0
for u in adj[v]:
if u == parent:
continue
if disc[u] == 0: # not visited
child_count += 1
dfs(u, v)
subtree_weight[v] += subtree_weight[u]
low[v] = min(low[v], low[u])
# After child processed, check separation condition
if low[u] >= disc[v]:
# subtree of u becomes separate component
separated_sum += subtree_weight[u]
max_comp = max(max_comp, subtree_weight[u])
# else: not separated, so part of rest
else:
# back edge to ancestor (since undirected, ignore parent)
low[v] = min(low[v], disc[u])
# After loop
if parent == 0: # root
if child_count > 1:
ans[v] = max_comp # max among separated children? Actually for root, all children are "separated" because there is no ancestor. But condition for root: each child subtree becomes a separate component. So we should treat all children as separated. However note: The condition low[u] >= disc[v] might not hold for root? For root, disc[root]=1, low[u] is at least disc[root]? Actually low[u] could be < disc[root]? disc[root] is smallest discovery time (1). low[u] cannot be less than 1. So low[u] >= 1 always, so low[u] >= disc[root] for all children. So in our DFS, for root, each child will satisfy low[u] >= disc[root] and thus be counted as separated. But careful: For root, there is no rest component; we don't want to subtract separated_sum from total_sum etc. So we need to treat root specially.
# For root, we need to compute max among all child subtree weights.
# Actually we can compute max_comp as max over all children (since all children are separated). But our loop already adds each child's subtree_weight to separated_sum and updates max_comp if low[u] >= disc[v]. For root, that condition holds for all children. So after loop, separated_sum = sum of subtree_weights of all children, and max_comp = max child subtree weight.
# There is no rest component, so answer is max_comp.
ans[v] = max_comp
else:
# root not articulation: only one child, graph remains connected after removal of root.
ans[v] = total_sum - w[v]
else:
# non-root
rest = total_sum - w[v] - separated_sum
max_comp = max(max_comp, rest)
ans[v] = max_comp
But there's a nuance: For root, what about the possibility that root is articulation but some children's low[u] might be < disc[root]? That cannot happen because disc[root] is smallest. So all children will satisfy condition. So separated_sum includes all child subtrees. But then rest = total_sum - w[root] - separated_sum = total_sum - w[root] - (total_sum - w[root])? Actually subtree_weight[root] includes all nodes, so separated_sum = sum of subtree_weight of all children = subtree_weight[root] - w[root] = total_sum - w[root]. Then rest = total_sum - w[root] - (total_sum - w[root]) = 0. So max_comp would be max among child subtrees, but also max(..., 0) = max_comp. That works. But the answer should be max among child subtrees. So we could unify: For any node, compute rest = total_sum - w[v] - separated_sum, then ans = max(max_comp, rest). For root, rest=0, which is fine as long as we don't incorrectly think rest=0 is a candidate. But note: If root has only one child, then separated_sum = subtree_weight[child] (since condition holds). Then rest = total_sum - w[root] - subtree_weight[child] = (w[root] + subtree_weight[child]) - w[root] - subtree_weight[child] = 0. So ans = max(subtree_weight[child], 0) = subtree_weight[child]. But correct answer for non-articulation root (single child) should be total_sum - w[root] = subtree_weight[child] (since total_sum = w[root] + subtree_weight[child]). That matches: subtree_weight[child] = total_sum - w[root]. So it's fine! Actually for root with one child, removal of root leaves the whole rest (the child's subtree) as a single component. So ans = total_sum - w[root] = subtree_weight[child] which equals max_comp. So the formula works without special casing root? But careful: In our algorithm for non-root, we assumed that rest component is total_sum - w[v] - separated_sum. For root, is that correct? Let's test: For root with >1 children, separated_sum = sum of all child subtrees = total_sum - w[root]. Then rest = 0. ans = max(max_comp, 0) = max_comp. That's correct. For root with 1 child, separated_sum = subtree_weight[child] (since condition holds). rest=0, ans=subtree_weight[child] which equals total_sum - w[root] (since only one child). So indeed the same formula holds for root? But is there any case where root has a child with low[child] < disc[root]? That cannot happen. So it seems we may not need to treat root specially. However, we must consider that for root, there is no "parent side", so the rest component as computed might be 0, which is fine because we don't operate a component of size 0 (they'd pick the max among positive components). The algorithm gives the max among separated child components. But wait, what if the root has a single child but that child is not an articulation condition? Actually low[child] >= disc[root] always true, so it will be counted as separated, and rest=0, ans = subtree_weight[child] which is total_sum - w[root]. That's correct. So the unified formula seems to work for all nodes if we correctly compute separated_sum as sum of subtree weights for children where low[child] >= disc[v] (including for root). Then answer = max( max_{separated child} subtree_weight[child], total_sum - w[v] - separated_sum ). However, for root, the "rest" component might be non-zero if there are nodes not in any child's subtree? But root's subtree includes all nodes, so all nodes are either root or in some child's subtree. So rest is 0. That's fine.
But there is a subtle issue: For non-root, what about children where low[child] >= disc[v] but there is also a back edge from that child's subtree to some node in another separated child's subtree? That would be a cross edge, which cannot happen in undirected DFS. So it's fine.
Thus we can compute for all nodes uniformly, without special root case. However we must ensure that separated_sum includes all children that become separate components. The condition low[child] >= disc[v] is correct.
But we must also consider the case where v is not an articulation point: Then there are no children with low[child] >= disc[v], so separated_sum = 0, rest = total_sum - w[v], ans = total_sum - w[v]. Good.
Now we need to compute subtree_weight correctly. But subtree_weight as defined includes the node and all descendants. However, in DFS tree, the subtree of a child includes all nodes that are in that child's DFS tree. For a child that is not separated (low[child] < disc[v]), the nodes in that subtree are still part of the rest component. That's fine.
Now we must be careful about the possibility of multiple edges? Input says no duplicate edges.
Also, graph may have up to 1e5 nodes and edges, recursion depth may be up to N if graph is a path. Python recursion depth can be set to 200000, but there is also risk of recursion limit and stack overflow due to Python's recursion overhead. Many CP solutions use recursion with sys.setrecursionlimit(10**6). I'll do that.
Time complexity: O(N+M) DFS.
Now let's test with given examples.
Example 1:
N=9, M=10
weights: 1..9
Edges: as given.
We need to compute answers: Output given.
We'll test manually maybe later.
But there is an additional nuance: When we remove node v, the component with maximum total weight is operated. Our computed answer is the maximum component weight. That seems correct.
Edge Cases: Suppose there is a node that, when removed, leaves multiple components with the same max weight? Output that max weight.
Now let's consider possible pitfalls.
-
The DFS tree might not be spanning if graph is not connected? But we assume connected.
-
When computing low[v], we need to consider back edges to visited nodes that are not the parent. In undirected graph, we must ignore the immediate parent edge because it's tree edge; but we also need to avoid revisiting parent via the same edge. So in DFS, when we see neighbor u that is already visited and u != parent, we update low[v] = min(low[v], disc[u]).
-
For subtree_weight accumulation: we add after DFS returns.
Now test with sample 1 quickly in mind or via code later.
But there is an additional complication: The "rest" component weight for a non-root node may include not only the ancestors but also other subtrees that are not separated. However, what about nodes that are not in v's subtree and also not ancestors? Actually all nodes not in v's subtree are ancestors or in other branches from ancestors that are not part of v's subtree. They are all connected via the DFS tree above v. So that's fine.
But is it guaranteed that the rest component computed as total_sum - w[v] - separated_sum is indeed the total weight of the component that contains the parent? For non-root v, consider the case where there is a back edge from a child subtree that is separated? No, separated children have low[child] >= disc[v], meaning they have no back edge to ancestors of v. However, they might have back edges to other subtrees of v? But as argued, not possible. So they are isolated after removal. The remaining nodes (including ancestors and non-separated child subtrees) are all connected because:
- Ancestors are connected among themselves via the DFS tree above v.
- Non-separated child subtrees each have a back edge to some ancestor of v (strictly above v), so they are connected to that ancestor, and thus to the ancestor region.
- Also non-separated child subtrees might connect to each other via those back edges to common ancestors.
Thus the rest is a single component.
Now we should also consider that for root, the "rest" component might be non-zero if the root has a back edge to somewhere? But root has no ancestors; any back edge from a child to root is already counted? Actually if there is a back edge from a child subtree to root, that does not create an alternative path because root is being removed. So child becomes separated if low[child] >= disc[root]; but low[child] could be disc[root] if there is a back edge to root, making it separated. So rest = 0. That matches: all nodes are in child subtrees.
Now confirm with sample 1.
I'll try to simulate quickly.
Sample 1:
N=9, weights 1..9.
Edges:
1-2
2-3
2-4
3-4
4-5
4-6
2-7
7-8
7-9
9-1
Graph: Let's draw. Nodes 1..9.
Edges: 1-2, 2-3,2-4,3-4 (so 3 and 4 connected),4-5,4-6,2-7,7-8,7-9,9-1. So it's like a cycle 1-2-... maybe. Let's analyze.
Better compute using code mentally? I can try to compute articulation points and component sizes.
But maybe trust the algorithm.
Nevertheless, we need to verify that output matches.
We'll implement and test with given examples.
But also note: The graph might have multiple edges? Not allowed.
Now consider if there is a node that is articulation but also has a child that is separated, but the maximum component might be the rest component or one of the separated subtrees. Our formula accounts.
Now we must be careful about integer overflow? N up to 1e5, w_i up to 1e6, total_sum up to 1e11, fits in 64-bit. Python int unlimited.
Now implement.
Potential pitfalls: The DFS order and low calculation. Ensure we use disc times incrementally.
Implementation details:
-
Use 0-index or 1-index? I'll use 0-index internally for convenience? But input uses 1..N. I'll keep 1-index.
-
adjacency list: list of lists of ints.
-
global variables: time = 0; disc = [0](N+1); low = [0](N+1); sub = [0](N+1); ans = [0](N+1); total_sum = sum(weights).
Function dfs(v, parent):
nonlocal time
time += 1
disc[v] = low[v] = time
sub[v] = w[v]
separated_sum = 0
max_sep = 0
for u in adj[v]:
if u == parent:
continue
if disc[u] == 0:
dfs(u, v)
sub[v] += sub[u]
low[v] = min(low[v], low[u])
if low[u] >= disc[v]:
separated_sum += sub[u]
max_sep = max(max_sep, sub[u])
else:
low[v] = min(low[v], disc[u])
# after loop
rest = total_sum - w[v] - separated_sum
ans[v] = max(max_sep, rest)
But wait: For root (parent=0), we treat parent specially? In our DFS call from root, we pass parent=0. The loop will skip u==0? Not needed. For root, after processing children, separated_sum includes all children that satisfy low[u] >= disc[root], which is all children. rest = total_sum - w[root] - separated_sum = total_sum - w[root] - (total_sum - w[root]) = 0. So ans[root] = max(max_sep, 0) = max_sep. That's correct as we argued. However, we need to check if there is any scenario where root's child is not considered separated because low[child] < disc[root]? Not possible because disc[root] is minimum disc (1). low[child] is at least disc[child] which is > disc[root], so low[child] >= disc[root] always holds. So root will always have all children counted as separated. So ans[root] = max child subtree weight. That's correct.
But is it always true that for root, the maximum component after removal is max child subtree weight? Consider a root with two children; child subtrees weights are 5 and 10. Then after removing root, components are the two subtrees, the largest is 10. So answer = 10. That matches.
But what if the root has a self-loop? Not allowed because edges are between distinct nodes.
Now test on simple case: N=2, edge 1-2, weights 1 and 2. Graph: 1-2. DFS from 1 as root. disc[1]=1, then go to 2: disc[2]=2, low[2]=2? Actually low[2] = min(disc[2], disc[1]? There is back edge? 2's neighbor is 1 which is parent, skip. So low[2]=disc[2]=2. After returning, low[1] = min(disc[1]=1, low[2]=2) = 1. For node 2: child? none. separated_sum=0, rest = total_sum - w[2] - 0 = (1+2)-2=1, ans[2] = max(0,1)=1. That's correct: remove node 2, remaining node 1 component weight=1. For node 1: child 2, low[2]=2 >= disc[1]=1 -> separated_sum = sub[2] = 2, rest = total_sum - w[1] - 2 = 3-1-2=0, ans[1] = max(2,0)=2. That's correct: remove node 1, remaining component is node 2 weight=2.
So algorithm works.
Now test on slightly more complex: Suppose triangle 1-2-3-1. weights all 1. total=3. Remove node 1: remaining graph: edge 2-3, so component weight 2. So answer for node1=2. For node2: similar 2. Node3:2. Our DFS from root 1: disc[1]=1, go to 2 (child). disc[2]=2, from 2 go to 3 (since 2 sees neighbors: 1(parent),3(unvisited). disc[3]=3. From 3, neighbors: 1 and 2. 1 is ancestor (disc=1), so low[3] = min(disc[3]=3, disc[1]=1) => low[3]=1. Also neighbor 2 is visited but not parent? Actually 3's parent is 2, so 2 is parent skip. So low[3]=1. Return to 2: low[2] = min(disc[2]=2, low[3]=1) => 1. Also edge 2-1 is back? Actually when at 2, neighbor 1 is visited and is parent? 1 is parent? For 2, parent is 1, so skip. So low[2]=1. Return to 1: after processing child 2, low[1] remains 1. Also from 1, neighbor 3 is already visited? When exploring 1 after child 2 returns, we also see neighbor 3, which is visited (disc[3] !=0) and is not parent (parent=0 for root). So low[1] = min(low[1], disc[3]=3) => still 1. Now compute for node 1: child 2, low[2]=1 which is < disc[1]=1? Actually low[2]=1, disc[1]=1 => low[2] >= disc[1]? 1>=1 true. So condition holds, so separated_sum += sub[2]. sub[2] includes nodes 2 and 3? Let's compute sub: sub[3]=1, sub[2]=w2+sub[3]=1+1=2, sub[1]=w1+sub[2]=1+2=3. So separated_sum = 2, rest = total_sum - w1 - separated_sum = 3-1-2=0. ans[1]=max(2,0)=2. correct.
For node 2: When processing node 2 as non-root? In DFS, we compute ans for all nodes during the DFS. For node 2: during dfs(2,1), we process child 3. low[3]=1, disc[2]=2. Check condition for child 3: low[3]=1 < disc[2]=2, so NOT separated. separated_sum=0. Then after loop, rest = total_sum - w2 - 0 = 3-1=2? Actually total_sum=3, w2=1, rest=2. max_sep=0. ans[2]=max(0,2)=2. correct.
Node 3: similar, child? In dfs(3,2), no children (since neighbor 1 visited, neighbor 2 parent). So separated_sum=0, rest= total_sum - w3 = 2, ans=2. correct.
Thus works.
Now consider graph where node is not articulation but still condition may hold? For a node that is not articulation, low[child] < disc[v] for all children. So separated_sum=0, answer = total_sum - w[v].
Now test on a more complex scenario: a node v that is articulation, but some children have low[c] >= disc[v] and some don't. The rest component includes non-separated children and ancestors. We'll test with an example: Graph: 1-2, 2-3, 2-4, 4-5, 4-6, and also 3-5? Actually need to ensure low values. Let's design: Node 2 is articulation. Weights all 1. total=6. Edges: 1-2, 2-3, 2-4, 3-5, 5-4? Might complicate. But trust algorithm.
Now verify with second sample. We'll trust.
Thus algorithm seems correct.
However, there is one more nuance: In undirected graphs, the DFS tree's child subtrees might not exactly correspond to components after removal if there are multiple edges or if there are back edges from a child to another child? But as argued, in undirected DFS, there are no cross edges, only tree/back edges. So child subtrees are independent.
But what about if there is an edge connecting two nodes within the same child's subtree that is not a tree edge? That's fine; it stays inside the component.
Thus the approach stands.
Now we need to be careful about the root's "rest" component being zero; we return max of separated components. That's fine.
Now we must also consider the possibility that the graph is not connected initially. If it is not connected, then the original SAKURA cluster is actually multiple clusters? The problem statement says "the entire cluster of SAKURA is divided into one or more clusters" implying initially it's one cluster. So assume connected. But what if input is not connected? In that case, the inspection process: On day i, we remove node i. The remaining graph consists of possibly multiple components. Among them, we operate the cluster with maximum total performance. But our algorithm might not handle properly because we assume total_sum is sum of all nodes, but if graph is disconnected initially, the cluster's initial state is all nodes? Actually the cluster is defined as the set of all nodes? They said "consists of N computers and M physical communication channels that can communicate with each other." This phrase could be interpreted as each channel connects two nodes that can communicate; but it doesn't explicitly state that the entire graph is connected. However, typical cluster implies all nodes are interconnected, but it's not strictly guaranteed. I'd still assume connected. But if not, we could adapt: treat each connected component separately? But then the "operated cluster" after removal might be from any component. The whole system might originally consist of multiple independent clusters? But then the inspection process: "the entire cluster of SAKURA is divided into one or more clusters" suggests initially it is one cluster; after removal it may divide. So initial graph must be connected. So we can assume connectivity.
Now we also need to consider that the graph may have multiple edges? Input says no duplicate edges.
Now we proceed to implementation.
Complexities: O(N+M) time, O(N+M) memory.
But there is a nuance: The DFS tree may not be spanning if graph is disconnected; but we'll check connectivity? Possibly we can still run DFS on each component and compute answers? However if graph disconnected, total_sum is sum of all nodes, but after removal, the components that are formed may come from different initial components. Actually if initially disconnected, then