#K91937. Connected Component Sum

    ID: 38086 Type: Default 1000ms 256MiB

Connected Component Sum

Connected Component Sum

You are given an undirected graph with n nodes and m edges, along with a list of node values. The nodes are numbered from 1 to n. Your task is to compute the sum of the node values in the connected component that contains a specific node k.

The graph is described by m edges; each edge connects two nodes in both directions. Formally, if we denote an edge by \( (u, v) \), there exists an edge from \( u \) to \( v \) and from \( v \) to \( u \). Let the value of node \( i \) be \( a_i \). The answer is the sum \( \sum_{i \in C} a_i \), where \( C \) is the connected component (either directly or indirectly connected) that contains node \( k \).

Note: All formulas are given in LaTeX format.

inputFormat

The input is given from stdin in the following format:

n m
a1 a2 ... an
u1 v1
u2 v2
... (m lines in total)
k

Where:

  • n is the number of nodes.
  • m is the number of edges.
  • a1, a2, ..., an are the values of the nodes (1-indexed).
  • Each of the next m lines contains two integers u and v representing an undirected edge between node u and node v.
  • k is the target node whose connected component sum is required.

outputFormat

Output a single integer to stdout which is the sum of the values of all nodes in the connected component containing node k.

## sample
5 4
1 2 3 4 5
1 2
1 3
2 4
4 5
3
15