首页 > 其他分享 >D. Book of Evil(树的直径+换根dp)

D. Book of Evil(树的直径+换根dp)

时间:2023-03-09 11:37:14浏览次数:51  
标签:int memset long Book dist1 Len sizeof 换根 dp

#include <bits/stdc++.h>
#define debug1(a) cout << #a << '=' << a << endl;
#define debug2(a, b) cout << #a << " = " << a << "  " << #b << " = " << b << endl;
#define debug3(a, b, c) cout << #a << " = " << a << "  " << #b << " = " << b << "  " << #c << " = " << c << endl;
#define debug4(a, b, c, d) cout << #a << " = " << a << "  " << #b << " = " << b << "  " << #c << " = " << c << "  " << #d << " = " << d << endl;
#define debug5(a, b, c, d, e) cout << #a << " = " << a << "  " << #b << " = " << b << "  " << #c << " = " << c << "  " << #d << " = " << d << "  " << #e << " = " << e << endl;
#define endl "\n"
#define fi first
#define se second
#define caseT \
	int T;    \
	cin >> T; \
	while (T--)
// #define int long long
// #define int __int128
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

// #pragma GCC optimize(3,"Ofast","inline")
// #pragma GCC optimize(2)

inline int rd()
{
	int f = 1, x = 0;
	char c = getchar();
	while (c < '0' || '9' < c)
	{
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while ('0' <= c && c <= '9')
		x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return f * x;
}

// 常数定义
const double eps = 1e-6;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10, M = 2 * N;
int h[N], e[M], ne[M], idx;
bool affected[N];
int dist1[N], dist2[N];

int n, m, d;
int ans;

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void dfs1(int u, int father)
{
	if(affected[u])dist1[u] = 0;

	int &d1 = dist1[u], &d2 = dist2[u];

	for (int i = h[u]; i != -1; i = ne[i])
	{
		int v = e[i];
		if (v == father)
			continue;
			
		dfs1(v, u);
		int d = dist1[v] + 1;
		// debug1(d);
		if(dist1[v] == -1)continue;



		if (d >= d1)
			d2 = d1, d1 = d;
		else if (d > d2)
			d2 = d;
	}

	// return dist;
}

void dfs2(int u, int fa,int preLen)
{
	if(max(dist1[u],preLen) <= d)ans ++;
	// debug2(u,max(dist1[u],preLen));
	for (int i = h[u]; i != -1; i = ne[i])
	{
		int v = e[i];
		if (v == fa)
			continue;
		
		int Len = preLen;
		if(dist1[v] >= 0)Len = (dist1[v] == dist1[u]-1)?max(Len,dist2[u])+1:max(Len,dist1[u])+1;
		else Len = max(Len,dist1[u]) + 1;
		
		dfs2(v, u, Len);

	}
}

void solve()
{
	memset(h, -1, sizeof h);
	memset(dist1,-0x3f,sizeof dist1);
	memset(dist2,-0x3f,sizeof dist2);
	
	// memset(f, -1, sizeof f);

	cin >> n >> m >> d;
	for (int i = 0; i < m; i++)
	{
		int t;
		cin >> t;
		affected[t] = 1;
	}

	for (int i = 0; i < n - 1; i++)
	{
		int x, y;
		cin >> x >> y;
		add(x, y);
		add(y, x);
	}

	dfs1(1, -1);

	dfs2(1, -1, -1044266559);
	// for(int i = 1;i <= n;i ++)debug3(i,dist1[i],dist2[i]);
	cout << ans << endl;
}

signed main()
{

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	// caseT
	solve();

	return 0;
}
/*

*/

标签:int,memset,long,Book,dist1,Len,sizeof,换根,dp
From: https://www.cnblogs.com/cfddfc/p/17197696.html

相关文章

  • TCP/UDP
    一、概述接着温顾TCP/UDP UDP(用户数据报):1.无连接2.不可靠传输协议3.传输速率比较快4.首部字段较少5.应用场景......
  • 【DP】LeetCode 剑指 Offer 10- I. 斐波那契数列
    题目链接剑指Offer10-I.斐波那契数列思路递推,思路可以参考剑指Offer10-II.青蛙跳台阶问题代码classSolution{publicintfib(intn){inta......
  • Jupyter Notebook特性-可视化输出 【chatgpt生成】
    JupyterNotebook可以输出各种可视化图表,例如线图、散点图、条形图等。这些图表可以直接在Notebook中显示,用户可以通过交互式操作进行缩放、平移等操作,以便更好地探索数......
  • DP练习1题解
    这是2023.3.7DP题单十个题的题解。T1ColoredSubgraphs(CF1796E)简要题意:给定一棵树,你要对树进行链剖分,必须剖成上下竖直的链,求剖后最短链的长度最大值。最小值最大......
  • Jupyter Notebook 运行朴素贝叶斯算法及讲解 【chatgpt生成】
    首先,我们需要导入所需的库。在本示例中,我们将使用NumPy和Scikit-learn库。请确保在执行下面的代码之前已安装这些库。在第一步中,我们导入了所需的库,这些库包括NumPy和Sci......
  • datahub 采集oracle数据 DPI-1047: Cannot locate a 64-bit Oracle Client library: l
    datahub命令行采集oracle报错如下:datahubingest-coracle.ymlsqlalchemy.exc.DatabaseError:(cx_Oracle.DatabaseError)DPI-1047:Cannotlocatea64-bitOr......
  • C# 监听窗口分辨率/DPI变更
    C#监听窗口分辨率/DPI变更 当程序运行,窗口已经加载后,如果修改屏幕分辨率,会影响窗口的正常显示。举个案例:悬浮窗口,显示在屏幕右下角。当分辨率、文本显示比例变更后......
  • MCP2515国产替代DP2515带有SPI 接口的独立CAN 控制器
    DP2515是一款独立控制器局域网络(ControllerAreaNetwork,CAN)协议控制器,完全支持CANV2.0B技术规范。该器件能发送和接收标准和扩展数据帧以及远程帧。DP2515自带的两个验......
  • udp客户端 用不用 bind 的区别
    无连接的socket的客户端和服务端以及面向连接socket的服务端通过调用bind函数来配置本地信息。使用bind函数时,通过将my_addr.sin_port置为0,函数会自动为你选择一个未占用的......
  • 腾讯云Ubuntu安装wordpress (1/3)
    主要用了下面的命令安装apachesudoaptinstallapache2-y安装mariadbsudoaptinstallmariadb-servermariadb-client-y取人mariadb是否安装成功sudosy......