QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#789707#7588. Monster HunterhuangweiliangWA 686ms21008kbC++142.0kb2024-11-27 21:32:172024-11-27 21:32:18

Judging History

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

  • [2024-11-27 21:32:18]
  • 评测
  • 测评结果:WA
  • 用时:686ms
  • 内存:21008kb
  • [2024-11-27 21:32:17]
  • 提交

answer

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e5 + 10;
vector<int> v[N];
int rt[N];
void dfs(int x, int fa){
    rt[x] = fa;
    for(int y : v[x]){
        if(y == fa) continue;
        dfs(y, x);
    }
}
int n;
struct Node{
    int a, b, id, res;
    bool operator < (const Node &A) const {
        if(a > b && A.a <= A.b) return 1;//回血在前
        if(a <= b && A.a > A.b) return 0;
        if(a > b && A.a > A.b) return b <= A.b;
        else return a > A.a;
    }
}p[N];
bool vis[N];
int ans[N];
priority_queue<Node> q;
int find(int x) {
    if(vis[rt[x]]) return rt[x] = find(rt[x]);
    return rt[x];
}
void solve(){
    cin >> n;
    for(int i = 2; i <= n; i++){
        int a, b;
        cin >> a >> b;
        p[i] = Node{a, b, i, 0};
        q.push(p[i]);
    }
    for(int i = 1; i < n; i++){
        int x, y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1, 0);
    int cnt = 0;
    while(!q.empty()){
        auto now = q.top();
        q.pop();
        // cout << now.id << endl;
        if(vis[now.id] || now.res != ans[now.id]) continue;
        vis[now.id] = 1;
        int x = now.id, fax = find(now.id);
        int a = p[fax].a;
        p[fax].a = max(p[fax].a, p[fax].a - p[fax].b + p[x].a);
        p[fax].b = p[fax].b - a - p[x].a + p[x].b + p[fax].a;
        if(fax > 1){
            p[fax].res = ans[fax] = ++cnt;
            q.push(p[fax]);
        }
    }
    cout << p[1].a << endl;
    for(int i = 1; i <= n; i++){
        ans[i] = rt[i] = 0;
        vis[i] = 0;
        v[i].clear();
    }
}
signed main(){
    // freopen("tree.in", "r", stdin);
    // freopen("tree.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while(T--) solve();
    cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
    return 0;
}
/*
1
4
2 6
5 4
6 2
1 2
2 3
3 4
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 10856kb

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: 686ms
memory: 21008kb

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'