답안 #547674

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
547674 2022-04-11T10:44:14 Z wiwiho 동기화 (JOI13_synchronization) C++14
50 / 100
602 ms 24552 KB
#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}

using namespace std;
using namespace __gnu_pbds;

#define orz
#ifdef orz
#define print(a...)cerr<<"Line "<<__LINE__<<":",kout("[" + string(#a) + "] = ", a)
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...); }
#else
#define print(a..)
void kout() {}
template<class T1, class ... T2> void kout(T1 a, T2 ... e) {}
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

ll ifloor(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a < 0) return (a - b + 1) / b;
    else return a / b;
}

ll iceil(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a > 0) return (a + b - 1) / b;
    else return a / b;
}

int n;
vector<vector<int>> g;
vector<pii> e;
vector<bool> ok;
vector<int> in, out;
vector<vector<int>> anc;
const int SZ = 20;
vector<int> BIT;
vector<int> lc, cnt;
int ts = 1;

void init(){
    g.resize(n + 1);
    e.resize(n);
    ok.resize(n + 1);
    anc.resize(SZ, vector<int>(n + 1));
    in.resize(n + 1);
    out.resize(n + 1);
    BIT.resize(2 * n + 1);
    lc.resize(n);
    cnt.resize(n + 1, 1);
}

int lowbit(int x){
    return x & -x;
}

void modify(int x, int v){
    for(; x < BIT.size(); x += lowbit(x)){
        BIT[x] += v;
    }
}

int query(int x){
    int ans = 0;
    for(; x > 0; x -= lowbit(x)){
        ans += BIT[x];
    }
    return ans;
}

void dfs(int now, int p){
    anc[0][now] = p;
    in[now] = ts++;
    for(int i : g[now]){
        if(i == p) continue;
        dfs(i, now);
    }
    out[now] = ts++;
    modify(in[now], 1);
    modify(out[now], -1);
}

int getpath(int v){
    return query(in[v]);
}

int findroot(int v){
    int tmp = getpath(v);
    //cerr << "findroot " << v << " : ";
    for(int i = SZ - 1; i >= 0; i--){
        int nxt = anc[i][v];
        if(getpath(nxt) == tmp) v = nxt;
    }
    //cerr << v << "\n";
    //for(int i = 1; i <= n; i++) cerr << query(i) - query(i - 1) << " ";
    //cerr << "\n";
    return v;
}

void addedge(int id){
    ok[id] = true;
    int u = e[id].F, v = e[id].S;
    if(in[u] > in[v]) swap(u, v);
    u = findroot(e[id].F);
    modify(in[v], -1);
    modify(out[v], 1);
    cnt[u] = cnt[u] + cnt[v] - lc[id];
    //cerr << "add " << id << " : " << u << " " << cnt[u] << "\n";
}

void deledge(int id){
    ok[id] = false;
    int u = e[id].F, v = e[id].S;
    if(in[u] > in[v]) swap(u, v);
    u = findroot(e[id].F);
    modify(in[v], 1);
    modify(out[v], -1);
    lc[id] = cnt[u];
    cnt[v] = cnt[u];
    //cerr << "remove " << id << " : " << u << " " << cnt[u] << " , " << v << " " << cnt[v] << "\n";
}

int main(){
    StarBurstStream

    int m, q;
    cin >> n >> m >> q;
    init();

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

    dfs(1, 1);
    
    for(int i = 1; i < SZ; i++){
        for(int j = 1; j <= n; j++){
            anc[i][j] = anc[i - 1][anc[i - 1][j]];
        }
    }

    while(m--){
        int d;
        cin >> d;
        if(ok[d]) deledge(d);
        else addedge(d);
    }

    while(q--){
        int v;
        cin >> v;
        v = findroot(v);
        cout << cnt[v] << "\n";
    }

    return 0;
}

Compilation message

synchronization.cpp: In function 'void modify(int, int)':
synchronization.cpp:102:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |     for(; x < BIT.size(); x += lowbit(x)){
      |           ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 316 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 21256 KB Output is correct
2 Correct 114 ms 21116 KB Output is correct
3 Correct 101 ms 23136 KB Output is correct
4 Correct 100 ms 23216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 23 ms 2640 KB Output is correct
8 Correct 592 ms 24492 KB Output is correct
9 Correct 602 ms 24552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 591 ms 24548 KB Output is correct
2 Correct 169 ms 24120 KB Output is correct
3 Correct 159 ms 24404 KB Output is correct
4 Correct 173 ms 24388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -