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 s, t, 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之间的权值之和差异最小
思路:
- 有人说这道题目是树形DP, 不过我最终没能把解答和树形DP联系到一起, 即使是研究了好几个人的代码. 可能把下面的sum变量变成dp[u]更适合吧
- 我的思路很直接, DFS枚举所有的边, 选择切除之和满足题意的那条边
总结:
- 题目的输入比较tricky #define LL long long int scanf("%lld", &in), printf("%lld", out)
- long long int 不可以使用 abs; long long 和 int 也不可用 min
- Long long int的INF 定义 可以是 0x7fffffff7fffffff
- 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; }