QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#789442 | #7588. Monster Hunter | fangzichang | AC ✓ | 419ms | 16128kb | C++20 | 2.2kb | 2024-11-27 20:22:10 | 2024-11-27 20:22:14 |
Judging History
answer
#include<bits/extc++.h>
#include<bits/stdc++.h>
//241127 NOIP T4
//#pragma GCC optimize(2)
//This code contains niche emotional elements. It is recommended to watch it after the age of 18.
#ifdef __unix__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
#define debug(x) fprintf(stderr,"Line %d,%s=%lld\n",__LINE__,#x,(ll)(x))
#define debugline fprintf(stderr,"--------------------------\n")
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define Inf (int)INFINITY
#define inf 0x3f3f3f3f
#define pb push_back
#define ll long long
#define endl '\n'
#define y second
#define x first
using namespace std;
const int N=1e5+10;
void read(){};
template<class T1,class...T2>
inline void read(T1&ret,T2&...rest){
ret=0;char c=gc();bool f=0;
for(;!isdigit(c);c=gc())f=c=='-';
for(;isdigit(c);c=gc())ret=ret*10+c-'0';
f&&(ret=-ret),read(rest...);
}
#define cin(...) read(__VA_ARGS__)
template<class T>
inline void print(T x){
static short s[35],t=0;
bool f=x<0;if(f)x=-x;
do s[t++]=x%10,x/=10;while(x);
for(f&&pc('-');t;)pc(s[--t]+'0');
}
bool vi[N];
ll a[N],b[N];
int T,n,fa[N],tfa[N];
basic_string<int>nxt[N];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
void dfs(int u){
for(int v:nxt[u])
if(v^tfa[u])tfa[v]=u,dfs(v);
}
struct Node{
int u;ll a,b;
Node(const int&_u,const ll&_a,const ll&_b):u(_u),a(_a),b(_b){}
bool operator<(const Node&n)const{
if((b<0)^(n.b<0))return(b<0)>(n.b<0);
if(b<0)return a+b<n.a+n.b;
return a>n.a;
}
};
priority_queue<Node,vector<Node>,less<Node>>pq;
int main(){
for(cin(T);T;T--){
cin(n);
for(int i=1;i<=n;i++)
a[i]=b[i]=vi[i]=0,nxt[i].clear();
vi[1]=1;
for(int i=2;i<=n;i++)
cin(a[i],b[i]),b[i]-=a[i],pq.emplace(i,a[i],b[i]);
for(int i=1,u,v;i<n;i++)
cin(u,v),nxt[u]+=v,nxt[v]+=u;
iota(fa+1,fa+n+1,1),dfs(1);
for(int u,f;!pq.empty();){
u=pq.top().u,pq.pop();
if(vi[u])continue;
vi[u]=1,f=find(tfa[u]);
// cerr<<u<<" -> "<<f<<" ("<<a[u]<<","<<b[u]<<")"<<" ("<<a[f]<<","<<b[f]<<")\n";
a[f]=max(a[f],a[u]-b[f]);
b[f]+=b[u],fa[u]=f;
if(f^1)pq.emplace(f,a[f],b[f]);
}
print(a[1]),pc(endl);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7760kb
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: 419ms
memory: 16128kb
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: 408ms
memory: 16000kb
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