76分蒟蒻求助
查看原帖
76分蒟蒻求助
234224
青鸟_Blue_Bird楼主2020/6/3 21:19

RT,三个点莫名其妙的WA了?

记录:https://www.luogu.com.cn/record/34120482

#include <bits/stdc++.h>
using namespace std;
#define N 100010
//#define isdigit(c) ((c)>='0'&&(c)<='9')

int read(){
	int x = 0, s = 1;
	char c = getchar();
	while(!isdigit(c)){
		if(c == '-')s = -1;
		c = getchar();
	}
	while(isdigit(c)){
		x = (x << 1) + (x << 3) + (c ^ '0');
		c = getchar();
	}
	return x * s;
}

struct node{
	int v, w, next;
} t[N << 1];
int f[N];
int n, s;
int son[N], next_dis[N];
int dis[N];
bool vis[N];

int bian = 0;
inline void add(int u, int v, int w){
	t[++bian] = (node){v, w, f[u]}, f[u] = bian;
	t[++bian] = (node){u, w, f[v]}, f[v] = bian;
	return ;
}

int max_dis = -233333, s1;
void dfs1(int now, int father, int dis){
	if(dis > max_dis){
		s1 = now;
		max_dis = dis;
	}
	for(int i = f[now]; i; i = t[i].next){
		int v = t[i].v, w = t[i].w;
		if(v != father)
			dfs1(v, now, dis + w);
	}
	return ;
}

void dfs2(int now, int father){
	for(int i = f[now]; i; i = t[i].next){
		int v = t[i].v, w = t[i].w;
		if(v != father){
			dis[v] = dis[now] + w;
			dfs2(v, now);
			if(son[now] == -1) son[now] = v, next_dis[now] = w;
			else if(dis[v] > dis[son[now]]){
				son[now] = v;  
				next_dis[now] = w;
			}
		}
	}
	return ;
}

int ans = (int)8e9, ans2 = -666666;
void dfs3(int now, int father){
	vis[now] = 1;
//	printf("now:  %d\n", now);
	for(int i = f[now]; i; i = t[i].next){
		int v = t[i].v, w = t[i].w;
		if(vis[v]) continue;
		if(v != father){
			dis[v] = dis[now] + w;
			vis[v] = 1;
			dfs3(v, now);
		}
	}
//	printf("v:  %d  dis:  %d\n", now, dis[now]); 
	ans2 = max(ans2, dis[now]);
	return ;
}

int main(){
//	freopen("hh.txt", "r", stdin);
	n = read(), s = read();
	for(int i = 1;i < n; i++){
		int x = read(), y = read(), w = read();
		add(x, y, w);
	}	
	dfs1(1, 1, 0);
	memset(son, -1, sizeof(son));
	memset(dis, 127, sizeof(dis));
	dfs2(s1, s1);  /*记录了树的直径*/
//	for(int i = s1; ~i; i = son[i]){
//		printf("now:  %d  son:  %d\n", i, son[i]);
//	}   
	memset(dis, 0, sizeof(dis));
	for(int i = s1; ~i; i = son[i]){
		memset(vis, 0, sizeof(vis));
		for(int j = i, cnt = 0; cnt <= s && ~j; j = son[j] ){
			vis[j] = 1;
			cnt += next_dis[j];
		}
		ans2 = -666666;
		for(int j = i, cnt = 0; cnt <= s && ~j; j = son[j]  ){
			dis[j] = 0;
//			printf("in:  %d\n", j);
			dfs3(j, j);
			cnt += next_dis[j];
//			printf("new\n\n");
		}
		ans = min(ans, ans2);
//		printf("ans:  %d  ans2:  %d\n", ans, ans2); 
	}
	printf("%d\n", ans);
	return 0;
}
2020/6/3 21:19
加载中...