QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#789440#7588. Monster HunterKFCML 0ms8780kbC++141.5kb2024-11-27 20:21:522024-11-27 20:21:58

Judging History

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

  • [2024-11-27 20:21:58]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:8780kb
  • [2024-11-27 20:21:52]
  • 提交

answer

// Hydro submission #67470ede9592d6097b86f52f@1732710111351
#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N=100010;
vector<int>g[N];
int w[N],fa[N],a[N],b[N],del[N];
struct node{
	int id,a,b;
	bool operator <(const node &cmp)const{
		int f1=(b>a),f2=(cmp.b>cmp.a);
		if(f1==f2){
			if(f1==0)
				return (b==cmp.b?id>cmp.id:b>cmp.b);
			return (a==cmp.a?id>cmp.id:a<cmp.a);
		}
		return (f1==f2?id>cmp.id:f1>f2);
	}
};
set<node>st;
int find(int x){
	return (del[fa[x]]?fa[x]=find(fa[x]):fa[x]);
}
void dfs(int u,int f){
	w[u]=f;
	fa[u]=u;
	for(auto v:g[u]){
		if(v==f)continue;
		dfs(v,u);
	}
}
void solve(){
	int n;
	cin>>n;
	for(int i=2;i<=n;i++)cin>>a[i]>>b[i];
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1,0);
	a[1]=0,b[1]=0;
	for(int i=2;i<=n;i++)st.insert({i,a[i],b[i]});
	int T=n-1;
	while(T--){
		node it=*st.begin();st.erase(it);
		int id=find(w[it.id]);
		del[it.id]=1;
		if(id!=1)st.erase({id,a[id],b[id]});
		int aa=a[id];
		a[id]=max(a[id],a[id]+a[it.id]-b[id]);
		b[id]=b[id]+b[it.id]-aa-a[it.id]+a[id];
		fa[it.id]=id;
		if(id!=1)st.insert({id,a[id],b[id]});
	}
	cout<<a[1]<<'\n';
	for(int i=1;i<=n;i++)g[i].clear(),fa[i]=0,w[i]=0;
	st.clear();
} 
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int qwq;
	cin>>qwq;
	while(qwq--)
		solve();
	return 0;
}
/*
5
1 3
3 2
1 2
9 5
2 1
3 1
4 2
5 2
*/

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 8780kb

input:

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

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: -100
Memory Limit Exceeded

input:

10
50000
112474 198604
847262 632597
871374 962220
971398 284540
10360 695245
43233 569941
64019 203468
598469 997911
41841 657552
333198 936119
546370 180011
58831 901040
959767 396595
710441 277461
429299 209831
494164 138327
393982 581911
993164 617488
108113 160310
55841 611360
301395 291074
149...

output:


result: