QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#788119#7588. Monster HunterKFCRE 0ms0kbC++141.2kb2024-11-27 15:59:282024-11-27 15:59:42

Judging History

你现在查看的是最新测评结果

  • [2024-11-27 15:59:42]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-27 15:59:28]
  • 提交

answer

// Hydro submission #6746d15d9592d6097b8675e2@1732694366071
#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int N = 1e5 + 10;
vector<int>g[N];
int a[N], b[N], fa[N], f[N];
int find(int x){return x == f[x] ? x : f[x] = find(f[x]);}

void dfs(int x, int f)
{
	fa[x] = f;
	for(auto j:g[x]) if(j!=f) dfs(j,x);
}

struct ab{int a,b,id;};
bool operator < (const ab &a, const ab &b)
{
	int va = (a.a <= a.b), vb = (b.a <= b.b);
	if(va!=vb) return va < vb;
	if(!va) return a.b < b.b;
	return a.a > b.a;
}
priority_queue<ab>pq;

signed main()
{
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	int n, i, u, v;
	scanf("%lld", &n);
	for(i=1;i<=n;i++) f[i] = i;
	for(i=2;i<=n;i++) scanf("%lld%lld", &a[i], &b[i]);
	for(i=1;i<n;i++) scanf("%lld%lld", &u, &v), g[u].emplace_back(v), g[v].emplace_back(u);
	dfs(1,0);
	for(i=2;i<=n;i++) pq.push({a[i],b[i],i});
	while(!pq.empty())
	{
		ab t = pq.top();pq.pop();
		int d = t.id;
		if(t.a!=a[d]||t.b!=b[d]) continue;
		int f = find(fa[d]);
		int ab = b[f] + b[d] - a[f] - a[d];
		a[f] = max(a[f],a[f]-b[f]+a[d]), b[f] = a[f] + ab;
		if(f!=1) pq.push({a[f],b[f],f});
		::f[d] = f;
	}
	printf("%lld", a[1]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Dangerous Syscalls

input:

1
4
2 6
5 4
6 2
1 2
2 3
3 4

output:


result: