답안 #547675

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
547675 2022-04-11T10:48:16 Z wiwiho 동기화 (JOI13_synchronization) C++14
100 / 100
518 ms 24592 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(u);
    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(u);
    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 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 17 ms 2124 KB Output is correct
8 Correct 16 ms 2108 KB Output is correct
9 Correct 17 ms 2132 KB Output is correct
10 Correct 417 ms 19292 KB Output is correct
11 Correct 415 ms 19216 KB Output is correct
12 Correct 441 ms 23868 KB Output is correct
13 Correct 104 ms 19060 KB Output is correct
14 Correct 218 ms 18556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 19276 KB Output is correct
2 Correct 99 ms 19124 KB Output is correct
3 Correct 98 ms 21480 KB Output is correct
4 Correct 98 ms 21444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 21 ms 2388 KB Output is correct
8 Correct 502 ms 21708 KB Output is correct
9 Correct 502 ms 21756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 518 ms 21700 KB Output is correct
2 Correct 151 ms 22000 KB Output is correct
3 Correct 154 ms 22056 KB Output is correct
4 Correct 157 ms 22116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 21 ms 2260 KB Output is correct
7 Correct 481 ms 20080 KB Output is correct
8 Correct 518 ms 24592 KB Output is correct
9 Correct 147 ms 20128 KB Output is correct
10 Correct 255 ms 19832 KB Output is correct
11 Correct 140 ms 22540 KB Output is correct
12 Correct 133 ms 22436 KB Output is correct
13 Correct 152 ms 24300 KB Output is correct