QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#788245#7588. Monster Hunterkonata2828AC ✓377ms49348kbC++231.6kb2024-11-27 16:20:142024-11-27 16:20:15

Judging History

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

  • [2024-11-27 16:20:15]
  • 评测
  • 测评结果:AC
  • 用时:377ms
  • 内存:49348kb
  • [2024-11-27 16:20:14]
  • 提交

answer

// Hydro submission #6746d63d9592d6097b868366@1732695613482
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
int n,h[N],e[N],ne[N],idx;
inline void add(int a,int b){e[idx]=b,ne[idx]=h[a],h[a]=idx++;}
struct node{
	int a,b;
	inline bool operator < (const node t)const{
		if((a<b)>(t.a<t.b))return 0; 
		if((a<b)<(t.a<t.b))return 1;
		if(a<b)return (a>t.a);
		else return (b<t.b);
	}
	inline node operator + (const node y)const{
		int A=max(this->a,this->a-this->b+y.a);
		int B=this->b-this->a+y.b-y.a+A;
		return (node){A,B};
	}
}a[N];
priority_queue<pair<node,int>>q;
priority_queue<pair<node,int>>v[N];
inline void eval(int u){
	while(v[u].size()){
		if(v[u].top().first<a[u])return;
		int j=v[u].top().second;
		a[u]=a[u]+v[u].top().first,v[u].pop();
		if(v[u].size()<v[j].size())swap(v[u],v[j]);
		while(v[j].size())v[u].push(v[j].top()),v[j].pop();
	}
}
void dfs(int u,int fa){
	for(int i=h[u];~i;i=ne[i]){
		int j=e[i];
		if(j==fa)continue;
		dfs(j,u),v[u].emplace(a[j],j);
	}
	eval(u);
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int T;
	cin>>T;
	while(T--){
		cin>>n,idx=0;
		for(int i=0;i<=n;i++)h[i]=-1,a[i].a=a[i].b=0;
		for(int i=2;i<=n;i++)cin>>a[i].a>>a[i].b;
		for(int i=1;i<n;i++){
			int a,b;
			cin>>a>>b;
			add(a,b),add(b,a);
		}
		dfs(1,-1);
		q.emplace(a[1],1);
		while(q.size()){
			auto t=q.top();q.pop();
			a[0]=a[0]+t.first;
			while(v[t.second].size())q.push(v[t.second].top()),v[t.second].pop();
		}
		cout<<a[0].a<<"\n";
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 17684kb

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: 0
Accepted
time: 318ms
memory: 48592kb

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:

63495498
541156749
191617241
177090559
383609891
27231993394
19064806867
37194996614
84584890
84657366

result:

ok 10 numbers

Test #3:

score: 0
Accepted
time: 377ms
memory: 49348kb

input:

1109
100
49335125 540429
21327621 30666761
84726139 32583318
62343520 86386375
68371125 80203961
11703807 57711659
4086043 20964787
71845269 8258000
70046842 34257600
86312745 39362281
92327731 31351601
27782858 53379001
51945765 34797724
7197540 99710490
43627335 61003907
256869 8469838
89150549 45...

output:

74012350
485460189
68654921
22821963
702398949
944191
63683983
49851540
539751698
378185590
17649578
153086960
85518043
218934054
40886105
365498548
568650050
115493085
13675680
20327516
19299519
47328093
101160112
314233771
7506569
20797884
140632255
699457438
11257338
71975351
699154224
597663853
...

result:

ok 1109 numbers