• POJ 3140 Contestants Division(树形DP)


    Description

    In the new ACM-ICPC Regional Contest, a special monitoring and submitting system will be set up, and students will be able to compete at their own universities. However there’s one problem. Due to the high cost of the new judging system, the organizing committee can only afford to set the system up such that there will be only one way to transfer information from one university to another without passing the same university twice. The contestants will be divided into two connected regions, and the difference between the total numbers of students from two regions should be minimized. Can you help the juries to find the minimum difference?

    Input

    There are multiple test cases in the input file. Each test case starts with two integers N and M, (1 ≤ N ≤ 100000, 1 ≤ M ≤ 1000000), the number of universities and the number of direct communication line set up by the committee, respectively. Universities are numbered from 1 to N. The next line has N integers, the Kth integer is equal to the number of students in university numbered K. The number of students in any university does not exceed 100000000. Each of the following M lines has two integers st, and describes a communication line connecting university s and university t. All communication lines of this new system are bidirectional.

    N = 0, M = 0 indicates the end of input and should not be processed by your program.

    Output

    For every test case, output one integer, the minimum absolute difference of students between two regions in the format as indicated in the sample output.

    Sample Input

    7 6
    1 1 1 1 1 1 1
    1 2
    2 7
    3 7
    4 6
    6 2
    5 7
    0 0

    Sample Output

    Case 1: 1

    题意:

    一棵树的节点上均有权值, 将这棵树切成两半, 两个part之间的权值之和差异最小

    思路:

    1. 有人说这道题目是树形DP, 不过我最终没能把解答和树形DP联系到一起, 即使是研究了好几个人的代码. 可能把下面的sum变量变成dp[u]更适合吧
    2. 我的思路很直接, DFS枚举所有的边, 选择切除之和满足题意的那条边

    总结:

    1. 题目的输入比较tricky #define LL long long int scanf("%lld", &in), printf("%lld", out)
    2. long long int 不可以使用 abs; long long 和 int 也不可用 min
    3. Long long int的INF 定义 可以是 0x7fffffff7fffffff
    4. Dfs 框架. 对树进行遍历时, 可以使用 visited[]数组来记录访问过的节点, 也可使用 dfs(u, pre) 来实现

    代码

    #include <iostream>
    #include <vector>
    #define LL long long int 
    using namespace std;
    const int MAXN = 100010;
    vector<int> graph[MAXN];
    int students[MAXN];
    LL minDis;
    LL total;
    int N, M;
    LL dfs(int u, int pre) {
    	LL child = 0;
    	LL sum = 0;
    	for(int i = 0; i < graph[u].size(); i ++) {
    		if(graph[u][i] == pre )
    			continue;
    		int v = graph[u][i];
    		child = dfs(v, u);
    		//cout << "u = " << u << " v = " << v <<" " << child<< endl;
    		// 不能用 abs
    		LL dist = (total - 2*child);
    		if( dist < 0 )
    			dist = -dist;
    		minDis = (minDis < dist)? minDis:dist;
    		sum += child;
    	}
    	return sum + students[u];
    }
    
    int main() {
    	freopen("E:\Copy\ACM\poj\3140\in.txt", "r", stdin);
    	int testCase = 0;
    	while(cin >> N >> M && N != 0) {
    		testCase ++;
    		for(int i = 0; i <= N; i ++)
    			graph[i].clear();
    		total = 0;
    		for(int i = 0; i < N; i ++) {
    			cin >> students[i];
    			total += students[i];
    		}
    		int from, to;
    		for(int i = 0; i < M; i ++) {
    			cin >> from >> to;
    			from --; to --;
    			graph[from].push_back(to);
    			graph[to].push_back(from);
    		}
    		minDis = 0x7fffffff3fffffff;
    		dfs(0, -1);
    		
    		printf("Case %d: %lld
    ",testCase, minDis);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    beta冲刺(6/7)
    beta冲刺(5/7)
    beta冲刺(4/7)
    beta(3/7)
    beta冲刺(2/7)
    beta冲刺(1/7)
    团队项目测评博客
    东华理工18级计科五班团队作业六
    东华理工18级计科五班团队作业五
    东华理工18级计科五班团队作业四
  • 原文地址:https://www.cnblogs.com/xinsheng/p/3430645.html
Copyright © 2020-2023  润新知