RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1242 Accepted Submission(s): 524
Total Submission(s): 1242 Accepted Submission(s): 524
Problem Description
RXD has a tree
T
, with the size of
n
. Each edge has a cost.
Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T .
he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk ,
where ⋃Si={2,3,…,n} and for all different i,j , we can conclude that Si⋂Sj=∅ .
Then he calulates res=∑ki=1f({1}⋃Si) .
He wants to maximize the res .
1≤k≤n≤106
the cost of each edge∈[1,105]
Si might be empty.
f(S) means that you need to choose a couple of edges on the tree to make all the points in S connected, and you need to minimize the sum of the cost of these edges. f(S) is equal to the minimal cost
Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T .
he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk ,
where ⋃Si={2,3,…,n} and for all different i,j , we can conclude that Si⋂Sj=∅ .
Then he calulates res=∑ki=1f({1}⋃Si) .
He wants to maximize the res .
1≤k≤n≤106
the cost of each edge∈[1,105]
Si might be empty.
f(S) means that you need to choose a couple of edges on the tree to make all the points in S connected, and you need to minimize the sum of the cost of these edges. f(S) is equal to the minimal cost
Input
There are several test cases, please keep reading until EOF.
For each test case, the first line consists of 2 integer n,k , which means the number of the tree nodes , and k means the number of parts.
The next n−1 lines consists of 2 integers, a,b,c , means a tree edge (a,b) with cost c .
It is guaranteed that the edges would form a tree.
There are 4 big test cases and 50 small test cases.
small test case means n≤100 .
For each test case, the first line consists of 2 integer n,k , which means the number of the tree nodes , and k means the number of parts.
The next n−1 lines consists of 2 integers, a,b,c , means a tree edge (a,b) with cost c .
It is guaranteed that the edges would form a tree.
There are 4 big test cases and 50 small test cases.
small test case means n≤100 .
Output
For each test case, output an integer, which means the answer.
Sample Input
5 4 1 2 3 2 3 4 2 4 5 2 5 6
Sample Output
27
题目大意:给你两个数n,k;下面输入图的n-1条信息分别点点编号,边,然后让你把除了根节点1的所有点分成k个部分,让你求每一部分与根节点所形成的最小斯坦纳树的和最大。
题目思路:官方题解:把1看成整棵树的根.问题相当于把 2∼n的每个点一个 [1,k]的标号.然后根据最小斯坦纳树的定义, (x,fax)这条边的贡献是以 x为根 子树内不同标号的个数目 difi.那么显然有 difi≤min(k,szi) szi表示子树大小.可以通过构造让所有 difi都取到最大值.所以答案就是 ∑x=2nw[x][fax]∗min(szx,k)时间复杂度 O(n).
我解释一下官方题解中的各个表达式的含义。 (x,fax)(x, fa_x)代表的是x节点与其父节点的边,w.....代表的是x节点与它父节点形成的边的长度,szx代表的是以x为根的子树的大小, k代表的就是题目中的k。我们让除了根节点即编号为1的节点之外的点编号(1,k)要想使得最后的结果最大,要使每一条边使用的次数最多,就等价于每一个子树中的所有的节点编号尽可能的不同,那么从最外求的的是最大的每
一次求的都是最大的那么最后求的结果就是最大的。
感觉类似于贪心。
AC代码:
27
题目大意:给你两个数n,k;下面输入图的n-1条信息分别点点编号,边,然后让你把除了根节点1的所有点分成k个部分,让你求每一部分与根节点所形成的最小斯坦纳树的和最大。
题目思路:官方题解:把1看成整棵树的根.问题相当于把 2∼n的每个点一个 [1,k]的标号.然后根据最小斯坦纳树的定义, (x,fax)这条边的贡献是以 x为根 子树内不同标号的个数目 difi.那么显然有 difi≤min(k,szi) szi表示子树大小.可以通过构造让所有 difi都取到最大值.所以答案就是 ∑x=2nw[x][fax]∗min(szx,k)时间复杂度 O(n).
我解释一下官方题解中的各个表达式的含义。 (x,fax)(x, fa_x)代表的是x节点与其父节点的边,w.....代表的是x节点与它父节点形成的边的长度,szx代表的是以x为根的子树的大小, k代表的就是题目中的k。我们让除了根节点即编号为1的节点之外的点编号(1,k)要想使得最后的结果最大,要使每一条边使用的次数最多,就等价于每一个子树中的所有的节点编号尽可能的不同,那么从最外求的的是最大的每
一次求的都是最大的那么最后求的结果就是最大的。
感觉类似于贪心。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL MOD=1e9+7;
const int MAXN=1e6+7;
int head[MAXN],tot;
int sz[MAXN],weight[MAXN];
struct Edge
{
int from,to,cost,nxt;
}e[MAXN*2];
void addedge(int u,int v,int w)
{
e[tot].from=u;
e[tot].to=v;
e[tot].cost=w;
e[tot].nxt=head[u];
head[u]=tot++;
}
void dfs(int u,int fa)
{
sz[u]=1;
for(int i=head[u];i!=-1;i=e[i].nxt)
{
int to=e[i].to;
if(to==fa) continue;
weight[to]=e[i].cost;
dfs(to,u);
sz[u]+=sz[to];
}
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(sz,0,sizeof(sz));
memset(head,-1,sizeof(head));
tot=0;
int u,v,w;
for(int i=1;i<=n-1;++i)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
dfs(1,-1);
LL ans=0;
for(int i=2;i<=n;i++)
{
ans+=(LL)weight[i]*min(sz[i],k);
}
printf("%lld\n",ans);
}
return 0;
}
建图采用的是链式前向星,如有问题参见大佬博客:
http://blog.csdn.net/acdreamers/article/details/16902023