답안 #749887

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
749887 2023-05-28T20:15:49 Z jaredgrxss 동기화 (JOI13_synchronization) C++14
0 / 100
8000 ms 262144 KB
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
using namespace std;

int timer = 1;
int anc[100005][20];
int tin[100005], tout[100005];
vector<int> g[100005];
int bit[100005], info[100005], last_sync[100005];
bool active[100005];
pair<int,int> edges[2*100005];

void dfs(int node = 1, int p = 0) {
    anc[node][0] = p;
    for (int i = 1; i < 20 && anc[node][i-1]; i++) {
        anc[node][i] = anc[anc[node][i-1]][i-1];
    }
    info[node] = 1;
    tin[node] = timer++;
    for (int u : g[node]){
        if (u != p)
            dfs(u, node);
    }
    tout[node] = timer;
}

void add(int i, int v) {
    for (; i < 100005; i += i & -i) bit[i] += v;
}

int get(int i) {
    int r = 0;
    for (; i; i -= i & -i) r += bit[i];
    return r;
}

int find_anc(int node) {
    int lca = node;
    for (int i = 19; ~i; i--) {
        if (anc[lca][i] && get(tin[anc[lca][i]]) == get(tin[node]))
            lca = anc[lca][i];
    }
    return lca;
}


void solve(){ 
    int N, M, Q;
    cin >> N >> M >> Q;

    for (int i = 0; i < N; i++) {
        int v, u; cin >> v >> u;
        edges[i].first = v;
        edges[i].second = u;
        g[v].push_back(u);
        g[u].push_back(v);
    }

    dfs();
    for (int i = 0; i < N+1; i++) {
        add(tin[i], -1);
        add(tout[i], 1);
    }

    for (int i = 0; i < M; i++) {
        int line; cin >> line;

        int u = edges[line].first, v = edges[line].second;

        if (anc[u][0] == v) swap(u,v);

        if (active[line]) {
            info[v] = last_sync[v] = info[find_anc(u)];
            add(tin[v], -1);
            add(tout[v], 1);
        } else {
            info[find_anc(u)] += info[v] - last_sync[v];
            add(tin[v], 1);
            add(tout[v], -1);
        }

        active[line] = !active[line];
    }

    for (int i = 0; i < Q; i++) {
        int node; cin >> node;
        cout << info[find_anc(node)] << '\n';
    }

}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t; cin >> t; 
    while(t--) solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 8051 ms 2644 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 569 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 629 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 561 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 8055 ms 2644 KB Time limit exceeded
2 Halted 0 ms 0 KB -