bzoj 1369: [Baltic2003]gemTime Limit:2 Sec Memory limit:64 MB submit:343 solved:219 [Submit][status][discuss]
The following:
TREEDP, the color of each point is about 8 species enough.
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int n=10005;
const int m=20005;
int cnt,to[m],lj[n],nxt[m];
void Insert (int f,int t)
{
cnt++,to[cnt]=t,nxt[cnt]=lj[f],lj[f]=cnt;
cnt++,to[cnt]=f,nxt[cnt]=lj[t],lj[t]=cnt;
}
int n,f[n][9],fa[n];
void Dfs (int x)
{for
(int i=lj[x];i;i=nxt[i])
if (to[i]!=fa[x])
{
fa[to[i]]=x;
DFS (To[i]);
for (int j=1;j<=8;j++)
{
int mn=1e9;
for (int k=1;k<=8;k++)
if (k!=j) mn=min (Mn,f[to[i]][k]);
f[x][j]+=mn;
}
}
for (int i=1;i<=8;i++) f[x][i]+=i;
}
int main ()
{
scanf ("%d", &n);
for (int i=1;i<n;i++)
{
int x, y;
scanf ("%d%d", &x,&y);
Insert (x, y);
}
DFS (1);
int ans=1e9;
for (int i=1;i<=8;i++) ans=min (Ans,f[1][i]);
printf ("%d", ans);
DescriptionGive a tree, ask you to mark the tree node on the right value, the weight can be any positive integer only limit condition is that the two nodes are adjacent to the same weight, requires a scheme, so that the total value of the whole tree is minimal.
InputFirst give a number n, representing the tree has n points, n<=10000 below the N-1 line, representing two points connected
OutputThe minimum total weight value
Sample Input10
7 5
1 2
1 7
8 9
4 1
9 7
5 6
10 2
9 3
Sample Output14
HINT
Source
[Submit] [Status] [Discuss]