제출 #1178157

#제출 시각아이디문제언어결과실행 시간메모리
1178157chikien2009동기화 (JOI13_synchronization)C++20
100 / 100
175 ms19784 KiB
#include <bits/stdc++.h>

using namespace std;

void setup()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

class FENWICK_TREE
{
    private:
    int tree_size;
    vector<int> tree;
    public:
    inline FENWICK_TREE(int new_size)
    {
        tree_size = new_size + 1;
        tree.resize(tree_size + 1, 0);
    }
    inline void Update(int pos, int val)
    {
        while (pos <= tree_size)
        {
            tree[pos] += val;
            pos += pos & (~(pos - 1));
        }
    }
    inline int Get(int pos)
    {
        int res = 0;
        while (0 < pos)
        {
            res += tree[pos];
            pos -= pos & (~(pos - 1));
        }
        return res;
    }
} ft(100000);

int n, m, q, a, b, c, d;
int x[100000], y[100000], head[100000], tail[100000], p[20][100000], score[100000], add[100000];
vector<int> g[100000];
bool created[100000];

inline void DFS(int node, int par)
{
    head[node] = a;
    a++;
    p[0][node] = par;
    for (int i = 1; i < 20; ++i)
    {
        p[i][node] = p[i - 1][p[i - 1][node]];
    }
    for (auto & i : g[node])
    {
        if (i != par)
        {
            DFS(i, node);
        }
    }
    tail[node] = a;
}

inline int GetPar(int inp)
{
    int cur = inp;
    for (int i = 19; i >= 0; --i)
    {
        if (ft.Get(head[p[i][cur]]) == ft.Get(head[inp]))
        {
            cur = p[i][cur];
        }
    }
    return cur;
}

int main()
{
    setup();

    cin >> n >> m >> q;
    for (int i = 0; i < n - 1; ++i)
    {
        cin >> x[i] >> y[i];
        g[x[i] - 1].push_back(y[i] - 1);
        g[y[i] - 1].push_back(x[i] - 1);
    }
    a = 1;
    DFS(0, 0);
    for (int i = 0; i < n; ++i)
    {
        score[i] = 1;
        ft.Update(head[i], 1);
        ft.Update(tail[i], -1);
    }
    while (m--)
    {
        cin >> a;
        b = y[a - 1] - 1;
        a = x[a - 1] - 1;
        if (head[b] < head[a])
        {
            swap(a, b);
        }
        a = GetPar(a);
        if (!created[b])
        {
            score[a] += score[b] - add[b];
            ft.Update(head[b], -1);
            ft.Update(tail[b], 1);
        }
        else
        {   
            score[b] = add[b] = score[a];
            ft.Update(head[b], 1);
            ft.Update(tail[b], -1);
        }
        created[b] ^= 1;
    }
    while (q--)
    {
        cin >> a;
        cout << score[GetPar(a - 1)] << "\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...