답안 #623598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
623598 2022-08-06T04:08:53 Z Hanksburger 동기화 (JOI13_synchronization) C++17
0 / 100
51 ms 39208 KB
#include <bits/stdc++.h>
using namespace std;
int par[100005][17], bit[100005], ok[100005], in[100005], out[100005], a[100005], b[100005], n, m, q, t;
vector<pair<int, int> > edge;
vector<int> adj[100005];
int update(int x, int y)
{
    for (int i=x; i<=n; i+=i&(-i))
        bit[i]+=y;
}
int query(int x)
{
    int ans=0;
    for (int i=x; i; i-=i&(-i))
        ans+=bit[i];
    return ans;
}
int findPar(int u)
{
    for (int i=16; i>=0; i--)
        if (query(in[u])==query(in[par[u][i]]))
            u=par[u][i];
    return u;
}
void dfs(int u, int p)
{
    par[u][0]=p;
    for (int i=1; i<=16; i++)
        par[u][i]=par[par[u][i-1]][i-1];
    in[u]=++t;
    for (int v:adj[u])
        if (v!=p)
            dfs(v, u);
    out[u]=t+1;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> q;
    for (int i=1; i<n; i++)
    {
        int u, v;
        cin >> u >> v;
        edge.push_back({u, v});
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1, 1);
    for (int i=1; i<=n; i++)
    {
        update(in[i], 1);
        update(out[i], -1);
        a[i]=1;
    }
    for (int i=1; i<=m; i++)
    {
        int x, u, v, w;
        cin >> x;
//        cout << "i=" << i << '\n';
        u=edge[x-1].first;
        v=edge[x-1].second;
        if (par[u][0]==v)
            swap(u, v);
        w=findPar(u);
        if (ok[x])
        {
            a[v]=b[v]=a[w];
            update(in[v], 1);
            update(out[v], -1);
        }
        else
        {
            a[w]+=a[v]-b[v];
            update(in[v], -1);
            update(out[v], 1);
        }
        ok[x]^=1;
//        cout << "------------- a: ";
//        for (int j=1; j<=n; j++)
//            cout << a[j] << ' ';
//        cout << '\n';
//        cout << "------------- bit: ";
//        for (int j=1; j<=n; j++)
//            cout << bit[j] << ' ';
//        cout << '\n';
//        cout << "------------- bit: ";
//        for (int j=1; j<=n; j++)
//            cout << query(j) << ' ';
//        cout << '\n';
    }
    for (int i=1; i<=q; i++)
    {
        int x;
        cin >> x;
        cout << a[findPar(x)] << '\n';
    }
    return 0;
}

Compilation message

synchronization.cpp: In function 'int update(int, int)':
synchronization.cpp:10:1: warning: no return statement in function returning non-void [-Wreturn-type]
   10 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5376 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 43 ms 34648 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5364 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 51 ms 39208 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -