답안 #882399

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
882399 2023-12-03T06:42:32 Z 12345678 동기화 (JOI13_synchronization) C++17
50 / 100
180 ms 33872 KB
#include <bits/stdc++.h>
 
using namespace std;

#define int long long

const int nx=1e5+5, kx=17;
int n, m, q, u[nx], v[nx], pa[nx][kx], lvl[nx], on[nx], dp[nx], l[nx], in[nx], out[nx], t, x, vs[nx];
vector<pair<int, int>> d[nx];
 
void dfs(int u, int p)
{
    lvl[u]=lvl[p]+1; pa[u][0]=p; dp[u]=1; in[u]=++t;
    for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
    for (auto [v, idx]:d[u]) if (v!=p) dfs(v, u);
    out[u]=t;
}

struct fenwick
{
    int d[nx];
    void add(int idx, int vl)
    {
        for (int i=idx; i<nx; i+=(i&-i)) d[i]+=vl;
    }
    int find(int idx)
    {
        int res=0;
        for (int i=idx; i>0; i-=(i&-i)) res+=d[i];
        return res;
    }
} f;

int findrt(int u)
{
    for (int i=kx-1; i>=0; i--) if (f.find(u)-f.find(pa[u][i])==lvl[u]-lvl[pa[u][i]]) u=pa[u][i];
    return u;
}

void dfs2(int u, int vl)
{
    vs[u]=1;
    dp[u]=vl;
    for (auto [v, idx]:d[u]) if (on[idx]&&v!=pa[u][0]) dfs2(v, vl); 
}

int32_t main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>m>>q;
    for (int i=1; i<n; i++) cin>>u[i]>>v[i], d[u[i]].push_back({v[i], i}), d[v[i]].push_back({u[i], i});
    dfs(1, 1);
    for (int i=1; i<=m; i++)
    {
        cin>>x;
        if (lvl[u[x]]>lvl[v[x]]) swap(u[x], v[x]);
        if (on[x])
        {
            l[v[x]]=dp[v[x]]=dp[findrt(v[x])];
            f.add(in[v[x]], -1);
            f.add(out[v[x]]+1, 1);
        }
        else
        {
            dp[findrt(u[x])]+=dp[v[x]]-l[v[x]];
            f.add(in[v[x]], 1);
            f.add(out[v[x]]+1, -1);
        }
        on[x]=!on[x];
    }
    for (int i=1; i<=n; i++) if (!vs[i]) dfs2(findrt(i), dp[findrt(i)]);
    while (q--) cin>>x, cout<<dp[x]<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Incorrect 2 ms 10588 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 31620 KB Output is correct
2 Correct 66 ms 31428 KB Output is correct
3 Correct 73 ms 33104 KB Output is correct
4 Correct 74 ms 33108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10720 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 13 ms 11948 KB Output is correct
8 Correct 174 ms 33308 KB Output is correct
9 Correct 177 ms 33360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 180 ms 33304 KB Output is correct
2 Correct 99 ms 33624 KB Output is correct
3 Correct 85 ms 33820 KB Output is correct
4 Correct 84 ms 33872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Incorrect 1 ms 10588 KB Output isn't correct
3 Halted 0 ms 0 KB -