F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Independent Feedback Vertex Set

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 553    Accepted Submission(s): 334


Problem Description
Yukikaze loves graph theory, especially forests and independent sets.
  • Forest: an undirected graph without cycles.
  • Independent set: a set of vertices in a graph such that for every two vertices, there is no edge connecting the two.
Yukikaze has an undirected graph $G=(V,E)$ where $V$ is the set of vertices and $E$ is the set of edges. Each vertex in $V$ has a vertex weight. Now she wants to divide $V$ into two complementary subsets $V_I$ and $V_F$ such that $V_I$ is an independent set, and the induced subgraph $G[V_F]$ is a forest. The induced subgraph $G[V_F]$ is the graph whose vertex set is $V_F$ and whose edge set consists of all of the edges in $E$ that have both endpoints in $V_F$. In addition, she wants to maximize the sum of weights of vertices in $V_I$.

Since this problem is NP-hard for general graphs, she decides to solve a special case of the problem. We can build a special graph by the following steps. Initially, the graph consists of three vertices $1,2,3$ and three edges $(1,2), (2,3), (3,1)$. When we add a vertex $x$ into the graph, we select an edge $(y,z)$ that already exists in the graph and connect $(x,y)$ and $(x,z)$. Keep doing this until there are $n$ vertices in the graph.
 

Input
The first line of the input contains a single integer $T$ ($1 \leq T \leq 10^3$), indicating the number of test cases.

The first line of each test case contains a single integer $n$ ($4 \leq n \leq 10^5$), indicating the number of vertices in the graph. It is guaranteed that the sum of $n$ over all test cases won't exceed $10^6$.

The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$), indicating the weights of the vertices.

Initially, the graph consists of three vertices $1,2,3$ and three edges $(1,2), (2,3), (3,1)$. The $i$-th line of the next $n-3$ lines contains two integers $u, v$ ($1 \leq u, v < i + 3$), indicating the addition of a vertex $i+3$ and two edges $(i+3,u),(i+3,v)$ to the graph. It is guaranteed that $(u,v)$ already exists in the graph.
 

Output
For each test case, print an integer in a single line indicating the maximum sum of weights of vertices in $V_I$.
 

Sample Input
3 4 3 3 2 2 1 2 4 2 5 5 2 2 3 5 3 1 1 1 1 1 2 1 3
 

Sample Output
4 5 3
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 01:35:34, Gzip enabled