HDOJ-5438-Ponds[拓扑排序动态删点]

Ponds

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4038    Accepted Submission(s): 1198


Problem Description
Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one pipe between two ponds. Each pond has a value  v .

Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode.

Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds
 

Input
The first line of input will contain a number  T(1T30)  which is the number of test cases.

For each test case, the first line contains two number separated by a blank. One is the number  p(1p104)  which represents the number of ponds she owns, and the other is the number  m(1m105)  which represents the number of pipes.

The next line contains  p  numbers  v1,...,vp , where  vi(1vi108)  indicating the value of pond  i .

Each of the last  m  lines contain two numbers  a  and  b , which indicates that pond  a  and pond  b  are connected by a pipe.
 

Output
For each test case, output the sum of the value of all connected components consisting of odd number of ponds after removing all the ponds connected with less than two pipes.
 

Sample Input
  
  
1 7 7 1 2 3 4 5 6 7 1 4 1 5 4 5 2 3 2 6 3 6 2 7
 

Sample Output
  
  
21
 

Source

题意:一共有n个池塘被m个水管相连,每个池塘都对应一个价值。现在为了节约经费要移除一些池塘和水管。移除的条件是:这个池塘所连接的其他池塘的个数小于2个。移除到直到不能移除为止。问所有包含奇数个池塘的连通分量的经费总和是多少?

思路:利用拓扑排序动态删点。在拓扑排序过程中将deg<=1的节点的vis设为true并且入栈,出栈后将与其相连的节点的deg均减1,若deg<=1同样标记vis为true同时入栈。直到栈为空为止,这样vis为true的节点表示已经删除。最后采用dfs遍历所有连通分量,将奇数个节点的连通分量的价值总和加入答案。

PS:这里要感谢nofaker提供的java优化输入方法。
Accepted5438390MS14360K2676 BJavavcodes

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.StringTokenizer;

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		try {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				tokenizer = new StringTokenizer(reader.readLine());
			}
		} catch (Exception e) {
			return null;
		}
		return tokenizer.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

class BigInt {
	long box;

	BigInt(long box) {
		this.box = box;
	}
}

public class Main {
	static final int MAXN = 10005;
	int[] val = new int[MAXN];
	int[] deg = new int[MAXN];
	boolean[] vis = new boolean[MAXN];
	int n, m;
	ArrayList<Integer>[] arc = new ArrayList[MAXN];

	void topsort() {
		Deque<Integer> stack = new ArrayDeque<Integer>();
		for (int i = 1; i <= n; ++i) {
			if (deg[i] <= 1) {
				stack.push(i);
				vis[i] = true;
			}
		}
		while (!stack.isEmpty()) {
			int u = stack.pop();
			for (int i = 0, size = arc[u].size(); i < size; ++i) {
				int v = arc[u].get(i);
				if (!vis[v]) {
					deg[v]--;
					if (deg[v] <= 1) {
						stack.push(v);
						vis[v] = true;
					}
				}
			}
		}
	}

	int dfs(int u, BigInt sum) {
		vis[u] = true;
		int num = 0;
		sum.box += val[u];
		for (int i = 0, size = arc[u].size(); i < size; ++i) {
			int v = arc[u].get(i);
			if (!vis[v]) {
				num += dfs(v, sum);
			}
		}
		return num + 1;
	}

	Main() {
		InputReader in = new InputReader(System.in);
		int T = in.nextInt();
		while (T-- != 0) {
			n = in.nextInt();
			m = in.nextInt();
			for (int i = 1; i <= n; ++i) {
				vis[i] = false;
				arc[i] = new ArrayList<Integer>();
				deg[i] = 0;
				val[i] = in.nextInt();
			}
			for (int i = 0; i < m; ++i) {
				int u = in.nextInt();
				int v = in.nextInt();
				arc[u].add(v);
				arc[v].add(u);
				deg[u]++;
				deg[v]++;
			}
			topsort();
			long res = 0;
			for (int i = 1; i <= n; ++i) {
				if (!vis[i]) {
					BigInt sum = new BigInt(0);
					if ((dfs(i, sum) & 1) == 1) {
						res += sum.box;
					}
				}
			}
			System.out.println(res);
		}
	}

	public static void main(String[] args) {
		new Main();
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值