QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#381930#7588. Monster Hunterchenxinyang2006RE 1ms7960kbC++142.3kb2024-04-07 22:12:562024-04-07 22:12:57

Judging History

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

  • [2024-04-07 22:12:57]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:7960kb
  • [2024-04-07 22:12:56]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
	if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
	if(x > y) x = y;
}

inline int popcnt(int x){
	return __builtin_popcount(x);
}

inline int ctz(int x){
	return __builtin_ctz(x);
}


/*ll power(ll p,int k = mod - 2){
	ll ans = 1;
	while(k){
		if(k % 2 == 1) ans = ans * p % mod;
		p = p * p % mod;
		k /= 2;	
	}
	return ans;
}*/
int T,n;
ll a[100005],b[100005];
int anc[100005];

int cnt;
int head[100005];
struct eg{
	int to,nxt;
}edge[200005];

void make(int u,int v){
	edge[++cnt].to = v;
	edge[cnt].nxt = head[u];
	head[u] = cnt;
}

void dfs(int u,int f){
	anc[u] = f;
	for(int i = head[u];i;i = edge[i].nxt){
		int v = edge[i].to;
		if(v == f) continue;
		dfs(v,u);
	}
}

int fa[100005];
int fnd(int u){
	if(fa[u] == u) return u;
	return fa[u] = fnd(fa[u]);
}

set <pll> P,Q;
void solve(){
	scanf("%d",&n);
	rep(u,2,n) scanf("%lld%lld",&a[u],&b[u]);

	cnt = 0;
	fill(head,head + n + 1,0);
	rep(i,1,n - 1){
		int u,v;
		scanf("%d%d",&u,&v);
		make(u,v);make(v,u);
	}
	dfs(1,0);

	P.clear();Q.clear();
	rep(u,1,n){
		fa[u] = u;
		if(u == 1) continue;
		if(a[u] <= b[u]) P.insert(mkp(a[u],u));
		else Q.insert(mkp(b[u],u));
	} 

	rep(pp,1,n - 1){
		int u = -1;
		if(!P.empty()){
			auto it = P.begin();
			u = it->second;
			P.erase(it);
		}else{
			auto it = prev(Q.end());
			u = it->second;
			Q.erase(it);
		}

		int v = fnd(anc[u]);
		if(v != 1){
			if(a[v] <= b[v]) P.erase(mkp(a[v],v));
			else Q.erase(mkp(b[v],v));
		}
		ll ta = max(a[v],a[v] - b[v] + a[u]),tb = b[u] + b[v] - a[u] - a[v] + ta;
		a[v] = ta;b[v] = tb;
		if(v != 1){
			if(a[v] <= b[v]) P.erase(mkp(a[v],v));
			else Q.erase(mkp(b[v],v));			
		}
		fa[u] = v;
	}
	printf("%lld\n",a[1]);
}

int main(){
	scanf("%d",&T);
	while(T--) solve();
	return 0;
}

详细

Test #1:

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

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
Runtime Error

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: