[树形DP]bzoj1040 - cool's Blog

[树形DP]bzoj1040

cool posted @ 2016年3月11日 19:50 in DP , 404 阅读

1040: [ZJOI2008]骑士

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 3082  Solved: 1179
[Submit][Status][Discuss]

Description

Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英。他们劫富济贫,惩恶扬善,受到社会各界的赞扬。最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争。战火绵延五百里,在和平环境中安逸了数百年的Z国又怎能抵挡的住Y国的军队。于是人们把所有的希望都寄托在了骑士团的身上,就像期待有一个真龙天子的降生,带领正义打败邪恶。骑士团是肯定具有打败邪恶势力的能力的,但是骑士们互相之间往往有一些矛盾。每个骑士都有且仅有一个自己最厌恶的骑士(当然不是他自己),他是绝对不会与自己最厌恶的人一同出征的。战火绵延,人民生灵涂炭,组织起一个骑士军团加入战斗刻不容缓!国王交给了你一个艰巨的任务,从所有的骑士中选出一个骑士军团,使得军团内没有矛盾的两人(不存在一个骑士与他最痛恨的人一同被选入骑士军团的情况),并且,使得这支骑士军团最具有战斗力。为了描述战斗力,我们将骑士按照1至N编号,给每名骑士一个战斗力的估计,一个军团的战斗力为所有骑士的战斗力总和。

Input

第一行包含一个正整数N,描述骑士团的人数。接下来N行,每行两个正整数,按顺序描述每一名骑士的战斗力和他最痛恨的骑士。

Output

应包含一行,包含一个整数,表示你所选出的骑士军团的战斗力。

Sample Input

3
10 2
20 3
30 1

Sample Output

30

HINT

 

对于100%的测试数据,满足N ≤ 1 000 000,每名骑士的战斗力都是不大于   1 000 000的正整数。

 

不知道是不是因为我太弱了,感觉这道题真是一道好题。

首先如果是一棵树,那就是 没上司的晚会(著名树形DP)

但现在变成了环,不过并不重要!

我们可以在环上任取一条边,然后剪断他,记两端点为S,T

所以有以下两种情况:

S不选,T随意。

T不选,S随意。

就变成了 没上司的晚会!

#include <cstdio>
#include <algorithm>
#define maxn 1000010
#define ll long long
using namespace std;
ll v[maxn],g[maxn],f[maxn],ans;
int h[maxn<<1],last[maxn<<1],head[maxn],vis[maxn],S,T,E,l;
void add(int x,int y)
{
	h[++l] = y;
	last[l] = head[x];
	head[x] = l;
}

void dfs(int x,int fa)
{
	vis[x] = 1;
	for (int i = head[x];i;i = last[i])
		if (h[i] != fa)
		{
			if (vis[h[i]])
			{
				S = x;
				T = h[i];
				E = i;
			}
			else dfs(h[i],x);
		}
}
int inver(int x)
{
	return ((x-1)^1)+1;
}

void Tree_DP(int x,int fa,int ban)
{
	f[x]=v[x],g[x]=0;
	for(int i = head[x];i;i = last[i])
	{
		if( i==ban || inver(i)==ban )continue;
		if(h[i] == fa)continue;
		Tree_DP(h[i],x,ban);
		f[x]+=g[h[i]];
		g[x]+=max(f[h[i]],g[h[i]]);
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	for (int i = 1;i <= n;i++)
	{
		int y;
		scanf("%lld%d",&v[i],&y);
		add(i,y);
		add(y,i);
	}
	for (int i = 1;i <= n;i++)
		if (!vis[i])
		{
			dfs(i,0);
			Tree_DP(S,0,E);
			ll temp=g[S];
			Tree_DP(T,0,E);
			temp=max(temp,g[T]);
			ans+=temp;
		}
	printf("%lld",ans);
	return 0;
}

我的DP能力还是有待提高,看来还是得多膜蛤。

另外bzoj2152 也是一道树形DP的基础题(TM还是要看题解)

附上代码

#include <cstdio>
#define maxn 20010
int dp[maxn][3],h[maxn*2],v[maxn*2],last[maxn*2],head[maxn],l,vis[maxn],ans;
void add(int x,int y,int z)
{
	h[++l] = y;
	v[l] = z % 3;
	last[l] = head[x];
	head[x] = l;
}

void dfs(int x)
{
	vis[x] = 1;
	dp[x][0]++;
	for (int i = head[x];i;i = last[i])
		if (!vis[h[i]])
		{
			dfs(h[i]);
			for (int j = 0;j <= 2;j++)
				ans += dp[x][j]*dp[h[i]][(6-v[i]-j)%3],dp[x][j] += dp[h[i]][(6-v[i]+j)%3]; 
		}
}
int gcd(int x,int y)
{
	if (x % y==0) return y;
	return gcd(y,x%y);
}
int main()
{
	int n;
	scanf("%d",&n);
	for (int i = 1;i < n;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z);
		add(y,x,z);
	}
	dfs(1);
	ans = ans*2+n;
	int l = gcd(ans,n*n);
	printf("%d/%d",ans/l,n*n/l);
	return 0;
}

AP SSC computer Ques 说:
2022年9月11日 01:01

Since the Government of AP also provided Computer labs for students in all Government schools also.Computer Knowledge is very much essential nowadays. Even Checking the Results of various examinations, AP SSC computer Question Paper Downloading hall tickets and study materials for multiple exams and booking tickets etc ..need minimum Technical Knowledge is much more important for everyone. Since the Government of AP also provided Computer labs for students in all Government schools also.

how to check forex c 说:
2023年1月21日 14:52

HDFC is a National Cum International Bank that holds many services for its customers, and Forex Card is one such option that HDFC bank does provide in all the various streams based on the customer eligibility and requirements the Forex Card is issued which does help them when they are on their way to a foreign country. how to check forex card balance HDFC Having a Forex Card during your visit abroad from India does helps you in many ways, as they also come with insurance which protects your loaded money always.

Plus One Blueprint 2 说:
2023年2月11日 19:06

Plus One Blueprint 2024 Every one of the students are right now sitting tight for the 11th test Model Paper to be Download. Underneath in this article, we have referenced the total Blueprint for the walk assessment. Plus One Blueprint 2024 Students can download the Marking Scheme from the connection given on this page. According to Board 11th Blueprint 2024, the main test is English will happen on seventh March 2024.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee