답안 #398988

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
398988 2021-05-05T00:51:57 Z couplefire Inside information (BOI21_servers) C++17
0 / 100
3500 ms 261632 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 120005;
const int B = 500;

struct DSU{
    int n;
    vector<int> fa, siz;
    stack<array<int, 2>> st;
    DSU(){}
    DSU(int _n): n(_n){
        fa.resize(n); siz.assign(n, 1);
        for(int i = 0; i<n; i++) fa[i] = i;
    }
    int find(int v){
        while(v != fa[v]) v = fa[v];
        return v;
    }
    bool unite(int u, int v){
        u = find(u), v = find(v);
        if(u == v) return 0;
        if(siz[u] > siz[v]) swap(u, v);
        fa[u] = v; siz[v] += siz[u];
        st.push({u, v});
        return 1;
    }
    bool pop(){
        if(st.empty()) return 0;
        int u = st.top()[0], v = st.top()[1]; st.pop();
        fa[u] = u; siz[v] -= siz[u];
        return 1;
    }
};

int n, q, cnte;
int added[N];
DSU blocks[N/B+5];
DSU global;
array<int, 2> edges[N];

int main(){
    // freopen("a.in", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    memset(added, -1, sizeof added);
    cin >> n >> q; q+=n-1;
    global = DSU(n);
    while(q--){
        char type; cin >> type;
        if(type == 'S'){
            int u, v; cin >> u >> v; --u; --v;
            if(cnte%B == 0) blocks[cnte/B] = DSU(n);
            for(int i = 0, end = cnte/B; i<=end; ++i) blocks[i].unite(u, v);
            if(added[u] == -1) added[u] = cnte;
            if(added[v] == -1) added[v] = cnte;
            edges[cnte] = {u, v};
            cnte++;
        }
        else if(type == 'Q'){
            int v, d; cin >> v >> d; --v; --d;
            if(added[d] == -1){
                if(v == d) cout << "yes" << endl;
                else cout << "no" << endl;
                continue;
            }
            if((cnte-1)/B < (added[d]+B-1)/B){
                for(int i = cnte-1; i>=added[d]; i--) global.unite(edges[i][0], edges[i][1]);
                if(global.find(v) == global.find(d)) cout << "yes" << endl;
                else cout << "no" << endl;
                while(!global.st.empty()) global.pop();
                continue;
            }
            int nxt = (added[d]+B-1)/B, cnt = 0;
            for(int i = nxt*B-1; i>=added[d]; i--) cnt += blocks[nxt].unite(edges[i][0], edges[i][1]);
            if(blocks[nxt].find(v) == blocks[nxt].find(d)) cout << "yes" << endl;
            else cout << "no" << endl;
            while(cnt--) blocks[nxt].pop();
            continue;
        }
        else{
            int v; cin >> v; --v;
            if(added[v] == -1){
                cout << 1 << endl;
                continue;
            }
            if((cnte-1)/B < (added[v]+B-1)/B){
                for(int i = cnte-1; i>=added[v]; i--) global.unite(edges[i][0], edges[i][1]);
                cout << global.siz[global.find(v)] << endl;
                while(!global.st.empty()) global.pop();
                continue;
            }
            int nxt = (added[v]+B-1)/B, cnt = 0;
            for(int i = nxt*B-1; i>=added[v]; i--) cnt += blocks[nxt].unite(edges[i][0], edges[i][1]);
            cout << blocks[nxt].siz[blocks[nxt].find(v)] << endl;
            while(cnt--) blocks[nxt].pop();
            continue;
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 234 ms 1220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 234 ms 1220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 237 ms 1348 KB Output is correct
2 Execution timed out 3581 ms 261632 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 237 ms 1348 KB Output is correct
2 Execution timed out 3581 ms 261632 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 232 ms 1248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 232 ms 1248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 234 ms 1272 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 234 ms 1272 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 232 ms 1248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 232 ms 1248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 240 ms 1220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 240 ms 1220 KB Output isn't correct
2 Halted 0 ms 0 KB -