QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#792182 | #7588. Monster Hunter | gs25 | WA | 1023ms | 33576kb | C++20 | 3.9kb | 2024-11-29 03:56:33 | 2024-11-29 03:56:34 |
Judging History
answer
#include <bits/stdc++.h>
#include <chrono>
#include <array>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = 1; i <= n; ++i)
#define ff first
#define ss second
using namespace std;
typedef long long ll;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ", ";
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? ", " : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef LOCAL
#define dbg(x...) \
cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \
_print(x); \
cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll lb, ll ub) { return uniform_int_distribution<ll>(lb, ub)(rng); }
const int MAXN = 1001000;
ll a[MAXN],b[MAXN],d[MAXN],parr[MAXN];
int par[MAXN];
ll chk[MAXN];
vector<int> adj[MAXN];
int sssss;
bool shit;
int find(int x){
if(x==parr[x]) return x;
return parr[x] = find(parr[x]);
}
void merge(int x, int y){
parr[x] = y;
a[y] = a[y] + max(0ll,a[x]-b[y]);
d[y] = d[y] + d[x];
b[y] = a[y] + d[y];
}
void dfs(int v, int p){
for(auto ne : adj[v]) if(ne!=p){
par[ne] = v; dfs(ne,v);
}
}
bool vis[MAXN];
void solve(){
int n; cin >> n;
sssss=0;
ll kk = 0;
for(int i=2; i<=n; i++){
cin >> a[i] >> b[i];
d[i] = b[i] - a[i];
kk += d[i];
}
rrep(i,n) parr[i] = i;
rrep(i,n) adj[i].clear();
rrep(i,n) chk[i] = 0;
rep(i,n-1){
int a,b; cin>>a>>b; adj[a].pb(b); adj[b].pb(a);
}
rrep(i,n) par[i] = 0;
par[1] = 1;
dfs(1,1);
set<array<ll,2>> s;
rrep(i,n){
if(d[i]>=0 && i!=1) s.insert({a[i],i});
}
// chk -> 내가 바뀔때 1 추가
set<int> fuck;
while(!s.empty()){
auto [ax,x] = *s.begin();
s.erase({ax,x});
if(fuck.find(x)!=fuck.end()) continue;
fuck.insert(x);
int y = par[x];
y = find(y);
merge(x,y);
if(d[y]>=0 && y!=1) s.insert({a[y],y}); //pq.push({a[y],y,chk[y]});
}
rrep(i,n) vis[i]= 0;
s.clear();
rrep(i,n){
int x = find(i);
if(vis[x] || x==1) continue;
vis[x] = 1;
assert(d[x]<0);
//pq.push({-b[x],x,chk[x]});
s.insert({-b[x],x});
}
while(!s.empty()){
auto [mbx,x] = *s.begin();
s.erase({mbx,x});
if(fuck.find(x)!=fuck.end()) continue;
fuck.insert(x);
int y = par[x];
y = find(y);
merge(x,y);
if(y!=1) s.insert({-b[y],y}); //pq.push({-b[y],y,chk[y]});
}
cout << a[1] << "\n";
assert(fuck.size()==n-1);
for(int i=2; i<=n; i++) assert(fuck.find(i)!=fuck.end());
//assert(d[1]==kk);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
cin >> t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17972kb
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
Wrong Answer
time: 1023ms
memory: 33576kb
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 604652243 796269298 796269298 796269298 796269298 796269298 796269298 796269298 796269298
result:
wrong answer 2nd numbers differ - expected: '541156749', found: '604652243'