BZOJ1369: [Baltic2003]gem tree DP

Source: Internet
Author: User
Tags time limit
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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.