HACK
查看原帖
HACK
1069395
yjf1225楼主2025/7/28 16:29
6
1 2 1
2 3 1
3 5 1
2 4 1
4 6 1

2 1
0 0
0 0
0 0
1 0
0 1

我在luogu上过了这道题,但是过不了这个数据

数据是否太水了?

错误代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<assert.h>
#include<queue>
#include<bits/extc++.h>
#define int long long
#define PII pair<int, int>
typedef long long LL;
using namespace std;
namespace Read{
	inline int read(){
		int f = 0, d = 1; char op;
		while(op = getchar(), !isdigit(op)) if(op == '-') d = -1;
		while(isdigit(op)) f = (f << 1) + (f << 3) + (op ^ 48), op = getchar();
		return f * d;
	}
} using namespace Read;
namespace YYY{
	const int N = 1e6 + 10, INF = 1e12;
	int ans;
	int h[N], ne[2 * N], e[2 * N], w[2 * N], idx;
	int valx[N], valy[N];
	__gnu_pbds::priority_queue<int, greater<int> > q1[N], q2[N];
	void add(int x, int y, int z){e[ ++ idx] = y, ne[idx] = h[x], w[idx] = z, h[x] = idx; }
	void dfs(int u, int last, int depth){
		for(int i = 1; i <= valx[u]; i ++ ) q1[u].push(depth);
		for(int i = 1; i <= valy[u]; i ++ ) q2[u].push(depth - INF);
		for(int i = h[u]; i != - 1; i = ne[i]){
			if(e[i] != last){
				dfs(e[i], u, depth + w[i]);
				q1[u].join(q1[e[i]]); q2[u].join(q2[e[i]]);
			}
		}
		while(q1[u].size() && q2[u].size() && q1[u].top() + q2[u].top() - 2 * depth < 0){
			// cout<<"###"<<endl;
			int tmpx = q1[u].top(), tmpy = q2[u].top();
			ans += q1[u].top() + q2[u].top() - 2 * depth;
			q1[u].pop(); q2[u].pop();
			q1[u].push(2 * depth - tmpy); q2[u].push(2 * depth - tmpx);
		}
	}
	signed main(){
		memset(h, -1, sizeof(h));
		int n; n = read();
		int x, y, z;
		for(int i = 1; i < n; i ++ ) x = read(), y = read(), z = read(), add(x, y, z), add(y, x, z);
		for(int i = 1; i <= n; i ++ ) valx[i] = read(), valy[i] = read(), ans += valy[i] * INF;
		dfs(1, -1, 0);
		cout << ans << '\n';
		return 0;
	}
}
signed main(){
	return YYY::main();
}
2025/7/28 16:29
加载中...