QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#884268#7588. Monster HunterlopzithAC ✓468ms19564kbC++143.2kb2025-02-05 23:06:582025-02-05 23:06:59

Judging History

This is the latest submission verdict.

  • [2025-02-05 23:06:59]
  • Judged
  • Verdict: AC
  • Time: 468ms
  • Memory: 19564kb
  • [2025-02-05 23:06:58]
  • Submitted

answer

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
#define DEFAULT 5e5 + 5
#define int long long
#define i64 long long
#define i128 __int128
#define ull unsigned long long
#define db double
#define ldb long double
#define Arr std::vector
#define Ptn std::pair
#define fi first
#define se second
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define popc(x) __builtin_popcountll(x)
#define clz(x) __builtin_clzll(x)
#define ctz(x) __builtin_ctzll(x)
#define chkmin(x, y) x = std::min(x, y)
#define chkmax(x, y) x = std::max(x, y)
#define FILE(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
#define TEST(x) freopen(x ".in", "r", stdin), freopen("test.out", "w", stdout)
#define KEL(x) freopen(x ".in", "r", stdin), freopen("a.out", "w", stdout)
#define debug std::cout << "Running on " << __FUNCTION__ << ' ' << __LINE__ << std::endl;
#define fail assert(0);
#define lopzith return 0;
bool BeginPoint;
const int INF = 0x3f3f3f3f, llINF = 0x3f3f3f3f3f3f3f3f, P = 998244353;
const db eps = 1e-9;
const ull base = 13331;
const int N = 1e5 + 5;
int _, n, a[N], b[N], fa[N], _fa[N], vis[N];
Arr<int> e[N];
struct LOP
{
    int a, b, id;
    friend bool operator < (LOP x, LOP y)
    {
        return std::max(x.a, x.a + y.a - x.b) > std::max(y.a, x.a + y.a - y.b);
    }
};
std::priority_queue<LOP> S;
inline int read()
{
    int w = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        w = (w << 3) + (w << 1) + (ch - 48);
        ch = getchar();
    }
    return w * f;
}

void DFS(int p)
{
    for (auto to : e[p])
    {
        if (to == fa[p]) continue;
        fa[to] = p;
        DFS(to);
    }
}

int find(int x) { return _fa[x] == x ? x : _fa[x] = find(_fa[x]); }

void Solve()
{
    vis[1] = 1;
    while (!S.empty())
    {
        auto x = S.top(); S.pop();
        if (vis[x.id] || x.a != a[x.id] || x.b != b[x.id]) continue;
        vis[x.id] = 1;
        int Fa = find(fa[x.id]);
        int A = std::max(a[Fa], a[Fa] - b[Fa] + a[x.id]), B = A + b[Fa] + b[x.id] - a[Fa] - a[x.id];
        a[Fa] = A, b[Fa] = B;
        _fa[x.id] = Fa;
        S.push((LOP){a[Fa], b[Fa], Fa});
    }
    printf("%lld\n", a[1]);
    for (int i = 1; i <= n; i++) a[i] = b[i] = 0;
}

bool EndPoint;
signed main()
{
    std::cerr << "Memory = " << fabs(&BeginPoint - &EndPoint) / 1048576.0 << "MB" << std::endl;

    _ = read();
    while (_--)
    {
        n = read(); 
        while (!S.empty()) S.pop();
        for (int i = 2; i <= n; i++)
        {
            int A = read(), B = read();
            a[i] = A, b[i] = B;
            S.push((LOP){A, B, i});
        }
        for (int i = 0; i <= n; i++)
        {
            _fa[i] = i, fa[i] = vis[i] = 0;
            e[i].clear();
        } // clear
        for (int i = 1; i < n; i++)
        {
            int u = read(), v = read();
            e[u].eb(v), e[v].eb(u);
        }
        DFS(1);
        Solve();
    }

    std::cerr << "Time = " << (double)clock() / CLOCKS_PER_SEC << "s" << std::endl;
    lopzith
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9828kb

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: 462ms
memory: 19336kb

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: 468ms
memory: 19564kb

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