UVA-6436 The Busiest City 树形DFS求解

给出n个顶点,n-1条边,对于每一个顶点来说每有一条路径经过,繁荣度+1,求最大繁荣度。

经过的含义就是这条路径使用了跟这个顶点相连的边中的的两条,任意组合都可以,所以要找出每个顶点相连的边延伸出去有多少种情况。

从第一个顶点开始建树,对于第i个节点有sum[i]个子节点,因此dp[i]=sum[i]*(n-1-sum[i]),再加上节点的n棵子树的节点数乘积/2。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iomanip>
#include <algorithm>
using namespace std;
const int maxn=22000;
struct Edge
{
	int v;
	int next;
}edge[maxn<<1];
int next[maxn];
int sum[maxn];
int ans,n,num;
void addedge(int u,int v)
{
	edge[num].next=next[u];
	next[u]=num;
	edge[num++].v=v;
}
void dfs(int father,int u,int N)
{
	int v,m,temp;
	for(int i=next[u];i!=-1;i=edge[i].next)
	{
		v=edge[i].v;
		if(v==father)
		{
			continue;
		}
		dfs(u,v,N);
		sum[u]+=sum[v];
	}
	m=sum[u]*(N-1-sum[u]);
	//cout<<father<<" "<<u<<" "<<m<<" "<<sum[u]<<endl;
	//cout<<"___________"<<endl;
	temp=0;
	for(int i=next[u];i!=-1;i=edge[i].next)
	{
		v=edge[i].v;
		if(v==father)
		{
			continue;
		}
		temp+=sum[v]*(sum[u]-sum[v]);
	}
	m+=temp/2;
	//cout<<father<<" "<<u<<" "<<m<<" "<<sum[u]<<endl;
	ans=max(ans,m);
	sum[u]++;
}
int main()
{
	int t;
	int k=1;
	int p,q;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		num=0;
		memset(sum,0,sizeof(sum));
		memset(next,-1,sizeof(next));
		for(int i=0;i<n-1;i++)
		{
			scanf("%d%d",&p,&q);
			addedge(p,q);
			addedge(q,p);
		}
		ans=0;
		dfs(0,1,n);
		printf("Case #%d: %d\n",k++,ans);
	}
	return 0;
}

UVA-6436 The Busiest City 树形DFS求解

时间: 2024-08-10 05:26:07

UVA-6436 The Busiest City 树形DFS求解的相关文章

uva 12033 - Game of CS(树形删边)

题目链接:uva 12033 - Game of CS 题目大意:给定图,以0为根节点,每条边有一个长度,两个人轮流操作,每次为一条边上色,上一个单位长度,当一条边的颜色被涂满,则算作是减掉整段子树.判断先手是否必胜. 解题思路:SG定理,对于当前节点u,每次考虑字节点v,u-v边的长度为l 当l为1时:sg(u) ^= (sg(v) + 1) 当l为奇数时: 需要判断sg(v)奇偶性,奇数-1,偶数+1: 当l为偶数时:sg(u) ^= sg(v) #include <cstdio> #in

UVA 152-Tree&#39;s a Crowd(暴力求解三维坐标求最短距离)

Tree's a Crowd Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Tree's a Crowd  Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like a tree--Think Fig'' has invented a new

ACM/ICPC 之 DFS求解欧拉回路+打表(POJ1392)

本题可以通过全部n位二进制数作点,而后可按照某点A的末位数与某点B的首位数相等来建立A->B有向边,以此构图,改有向图则是一个有向欧拉回路,以下我利用DFS暴力求解该欧拉回路得到的字典序最小的路径. //求咬尾数,一个2^n位环形二进制数,该二进制的每n位连续二进制数都不同 //DFS求解欧拉回路 //Time:32ms Memory:1668K #include<iostream> #include<cstring> #include<cstdio> using

uva 219 - Department of Redundancy Department(dfs+剪枝)

题目链接:uva 219 - Department of Redundancy Department 题目大意:给定一些关系,问哪一些关系是可以被替代的,如果可以被替代,给出替代的方案,一种即可. 解题思路:因为总共也就26个字母,所以用二进制数表示状态.剪枝,每次将所有可选关系均考虑进去都无法满足则是false. #include <cstdio> #include <cstring> #include <algorithm> using namespace std;

选课 树形DP+多叉树转二叉树+dfs求解答案

问题 A: 选课 时间限制: 1 Sec  内存限制: 128 MB提交: 6  解决: 3[提交][状态][答疑][寄存][题解] 题目描述 大 学里实行学分.每门课程都有一定的学分,学生只要选修了这门课并考核通过就能获得相应的学分.学生最后的学分是他选修的各门课的学分的总和. 每个学生都要选择规定数量的课程.其中有些课程可以直接选修,有些课程需要一定的基础知识,必须在选了其它的一些课程的基础上才能选修.例如,<数据结 构>必须在选修了<高级语言程序设计>之后才能选修.我们称&l

uva 10859 Placing Lampposts,树形dp

// uva 10859 Placing Lampposts // 树形dp // // 题目的意思是一个无向无环图中,有一些顶点和一些边 // 要在顶点上放置灯笼(灯笼可以照亮与它相邻接的点), // 使得所有的边都能被灯笼照亮,其中可能有一些边被两个灯笼 // 照亮,则要求使得所有边都被灯笼照亮所需灯笼的最小值, // 并且,此时边同时被两个灯笼照亮的数目应尽可能的多 // // 思路是 // d[i][0]表示在节点i不放置灯笼所需的灯笼的最小值 // d[i][1]表示在节点i放置灯笼所

UVA 12186 Another Crisis(树形DP)

A couple of years ago, a new world wide crisis started, leaving many people with economical problems. Some workers of a particular company are trying to ask for an increase in their salaries. The company has a strict hierarchy, in which each employee

LeetCode OJ 108. Convert Sorted Array to Binary Search Tree DFS求解

题目链接:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 108. Convert Sorted Array to Binary Search Tree My Submissions Question Total Accepted: 68378 Total Submissions: 187560 Difficulty: Medium Given an array where elements ar

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M