Submission #65917991


Source Code Expand

Copy
/*
import sys
sys.setrecursionlimit(10**6)
n=int(input())
g=[[]for _ in range(n)]
edges=[0]*n
for i in range(1,n):
u,v=map(int,input().split())
u-=1
v-=1
g[u].append(v)
g[v].append(u)
edges[i]=(u,v)
tin=[0]*n
tout=[0]*n
parent=[0]*n
timer=0
def dfs(u,p):
global timer
tin[u]=timer
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/*
import sys
sys.setrecursionlimit(10**6)
n=int(input())
g=[[]for _ in range(n)]
edges=[0]*n
for i in range(1,n):
    u,v=map(int,input().split())
    u-=1
    v-=1
    g[u].append(v)
    g[v].append(u)
    edges[i]=(u,v)

tin=[0]*n
tout=[0]*n
parent=[0]*n
timer=0
def dfs(u,p):
    global timer
    tin[u]=timer
    timer+=1
    parent[u]=p
    for v in g[u]:
        if v==p:continue
        dfs(v,u)
    tout[u]=timer-1
dfs(0,-1)
child=[0]*n
for i in range(1,n):
    u,v=edges[i]
    child[i]=u if parent[u]==v else v

from atcoder.fenwicktree import FenwickTree
ft=FenwickTree(n)
for i in range(n):
    ft.add(tin[i],1)
for _ in range(int(input())):
    q=list(map(int,input().split()))
    if q[0]==1:
        x=q[1]-1
        w=q[2]
        ft.add(tin[x],w)
    else:
        y=q[1]
        v=child[y]
        s=ft.sum(tin[v],tout[v]+1)
        ans=abs(ft.sum(0,n)-2*s)
        print(ans)
*/

#include <bits/stdc++.h>
#include <atcoder/fenwicktree>
using namespace std;
using namespace atcoder;

int n;
vector<vector<int>> g;
vector<pair<int,int>> edges;
vector<int> tin, tout, parent, child;
int timer = 0;

void dfs(int u, int p) {
    tin[u] = timer++;
    parent[u] = p;
    for (int v : g[u]) {
        if (v == p) continue;
        dfs(v, u);
    }
    tout[u] = timer - 1;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    g.resize(n);
    edges.resize(n);
    tin.resize(n);
    tout.resize(n);
    parent.resize(n);
    child.resize(n);

    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        g[u].push_back(v);
        g[v].push_back(u);
        edges[i] = {u, v};
    }

    dfs(0, -1);

    for (int i = 1; i < n; ++i) {
        int u = edges[i].first, v = edges[i].second;
        child[i] = (parent[u] == v) ? u : v;
    }

    fenwick_tree<long long> ft(n);
    for (int i = 0; i < n; ++i) {
        ft.add(tin[i], 1);
    }

    int q;
    cin >> q;
    while (q--) {
        int type;
        cin >> type;
        if (type == 1) {
            int x, w;
            cin >> x >> w;
            --x;
            ft.add(tin[x], w);
        } else {
            int y;
            cin >> y;
            int v = child[y];
            long long s = ft.sum(tin[v], tout[v] + 1);
            long long total = ft.sum(0, n);
            cout << abs(total - 2 * s) << '\n';
        }
    }

    return 0;
}

Submission Info

Submission Time
Task F - Compare Tree Weights
User juten
Language C++ 20 (gcc 12.2)
Score 500
Code Size 2533 Byte
Status AC
Exec Time 147 ms
Memory 42904 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 43
Set Name Test Cases
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3436 KB
hand_00.txt AC 147 ms 42904 KB
hand_01.txt AC 117 ms 29904 KB
hand_02.txt AC 140 ms 29252 KB
hand_03.txt AC 136 ms 29216 KB
hand_04.txt AC 146 ms 29176 KB
hand_05.txt AC 142 ms 29064 KB
hand_06.txt AC 1 ms 3500 KB
hand_07.txt AC 145 ms 33764 KB
hand_08.txt AC 141 ms 28980 KB
hand_09.txt AC 129 ms 28948 KB
hand_10.txt AC 125 ms 29444 KB
hand_11.txt AC 116 ms 30536 KB
random_00.txt AC 140 ms 31360 KB
random_01.txt AC 142 ms 31328 KB
random_02.txt AC 142 ms 31292 KB
random_03.txt AC 140 ms 31356 KB
random_04.txt AC 141 ms 31384 KB
random_05.txt AC 117 ms 30148 KB
random_06.txt AC 119 ms 29964 KB
random_07.txt AC 116 ms 30244 KB
random_08.txt AC 120 ms 30196 KB
random_09.txt AC 117 ms 30228 KB
random_10.txt AC 143 ms 29248 KB
random_11.txt AC 143 ms 29184 KB
random_12.txt AC 144 ms 29140 KB
random_13.txt AC 143 ms 29180 KB
random_14.txt AC 144 ms 29244 KB
random_15.txt AC 145 ms 29164 KB
random_16.txt AC 144 ms 29268 KB
random_17.txt AC 145 ms 29140 KB
random_18.txt AC 146 ms 29216 KB
random_19.txt AC 143 ms 29120 KB
random_20.txt AC 143 ms 29200 KB
random_21.txt AC 144 ms 29180 KB
random_22.txt AC 147 ms 29160 KB
random_23.txt AC 144 ms 29180 KB
random_24.txt AC 143 ms 29176 KB
random_25.txt AC 142 ms 29216 KB
random_26.txt AC 143 ms 29188 KB
random_27.txt AC 142 ms 29220 KB
random_28.txt AC 143 ms 29268 KB
random_29.txt AC 146 ms 29156 KB


2025-05-17 (Sat)
22:44:09 +09:00