QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#788013#7588. Monster Hunterkonata2828AC ✓521ms19772kbC++171.5kb2024-11-27 15:34:442024-11-27 15:34:54

Judging History

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

  • [2024-11-27 15:34:54]
  • 评测
  • 测评结果:AC
  • 用时:521ms
  • 内存:19772kb
  • [2024-11-27 15:34:44]
  • 提交

answer

// Hydro submission #6746cb8e9592d6097b866a07@1732692879151
#include<bits/stdc++.h>
using namespace std;
namespace ax_by_c{
typedef long long ll;
const int N=1e5+5;
int n;
ll a[N],b[N];
vector<int>g[N];
int fa[N];
void dfs(int u){
	for(auto v:g[u]){
		if(v==fa[u])continue;
		fa[v]=u;
		dfs(v);
	}
}
struct node{
	int u;
	ll a,b;
	bool operator < (const node& y)const{
		if(b-a>=0&&y.b-y.a<0)return 0;
		if(b-a<0&&y.b-y.a>=0)return 1;
		if(b-a>=0)return a>y.a;
		else return b<y.b;
	}
};
bool vis[N];
int find(int u){
	if(!vis[u])return u;
	return fa[u]=find(fa[u]);
}
void slv(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		fa[i]=0;
		g[i].clear();
		vis[i]=0;
	}
	for(int i=2;i<=n;i++)scanf("%lld %lld",&a[i],&b[i]);
	for(int i=1,x,y;i<n;i++){
		scanf("%d %d",&x,&y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs(1);
	priority_queue<node>pq;
	for(int i=2;i<=n;i++)pq.push({i,a[i],b[i]});
	ll ans=0,cur=0;
	while(pq.size()){
		auto tmp=pq.top();
		pq.pop();
		if(vis[tmp.u]||a[tmp.u]!=tmp.a||b[tmp.u]!=tmp.b)continue;
		vis[tmp.u]=1;
		int pos=find(tmp.u);
		if(pos==1){
			if(cur<tmp.a){
				ans+=tmp.a-cur;
				cur=tmp.a;
			}
			cur-=tmp.a;
			cur+=tmp.b;
		}
		else{
			ll ttmp=max(a[pos],a[pos]-b[pos]+tmp.a);
			b[pos]=b[pos]-a[pos]+tmp.b-tmp.a+ttmp;
			a[pos]=ttmp;
			pq.push({pos,a[pos],b[pos]});
		}
	}
	printf("%lld\n",ans);
}
void main(){
	int T;
	scanf("%d",&T);
	while(T--){
		slv();
	}
}
}
int main(){
	ax_by_c::main();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 6264kb

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: 521ms
memory: 19772kb

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: 514ms
memory: 19716kb

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