QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#789446#7588. Monster Hunterkonata2828AC ✓845ms22492kbC++141.4kb2024-11-27 20:22:422024-11-27 20:22:45

Judging History

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

  • [2024-11-27 20:22:45]
  • 评测
  • 测评结果:AC
  • 用时:845ms
  • 内存:22492kb
  • [2024-11-27 20:22:42]
  • 提交

answer

// Hydro submission #67470f109592d6097b86f5a0@1732710160843
#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];
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 (fa[x]!=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]);
		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: 1ms
memory: 7552kb

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: 845ms
memory: 22412kb

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: 833ms
memory: 22492kb

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