답안 #488685

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
488685 2021-11-20T04:41:59 Z SirCovidThe19th 특수한 그래프 (IZhO13_specialg) C++17
100 / 100
250 ms 16480 KB
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, x, y) for (int i = x; i < y; i++)

const int mx = 1e5 + 5;

int n, q, to[mx], dep[mx], tin[mx], tout[mx], ti, cyc[mx]; bool vis[mx], active[mx];
vector<int> startDFS, adj[mx]; 

template<int sz> struct segTreeRP{
    int seg[sz * 2];

    int comb(int a, int b){
        return tin[a] > tin[b] ? a : b; //we want lower node so greatest tin val
    }
    void upd(int l, int r, int v){
        for (l += sz, r += sz; l <= r; r /= 2, l /= 2){
            if (l % 2 == 1) seg[l] = comb(seg[l], v), l++; 
            if (r % 2 == 0) seg[r] = comb(seg[r], v), r--;
        }
    }
    int qry(int i){
        int ret = 0;
        for (i += sz; i; i /= 2) ret = comb(ret, seg[i]);
        return ret;
    }
};segTreeRP<mx> st;

void findStartPts(int cur){
    active[cur] = vis[cur] = 1; int nxt = to[cur]; 
    if (!nxt) startDFS.push_back(cur); //endpoint of chain
    else{
        if (!vis[nxt]) findStartPts(nxt);
        else if (active[nxt]) startDFS.push_back(cur); //part of cycle
    }
    active[cur] = 0;
}
bool isAnc(int A, int B){
    return tin[A] <= tin[B] and tout[A] >= tout[B];
}
void dfs(int cur){
    tin[cur] = ++ti; 
    for (int nxt : adj[cur]){
        if (!tin[nxt]) dep[nxt] = dep[cur] + 1, dfs(nxt);
        else cyc[nxt] = cur;
    }
    tout[cur] = ti;
}
void rem(int X){ //remove edge outgoing from X
    if (cyc[X]) cyc[X] = 0;
    else st.upd(tin[X], tout[X], X);
}
int solve(int A, int B){ //A goes to B
    int ret = 0, root = st.qry(tin[A]);
    
    if (A == B or !isAnc(B, A)){ //go to root and use cycle
        ret += dep[A] - dep[root] + 1;
        A = cyc[root]; root = st.qry(tin[A]);
    }
    if (isAnc(B, A) and tin[B] >= tin[root]) ret += dep[A] - dep[B];
    else ret = -1;

    return ret;
}

int main(){
    cin >> n;
    FOR(i, 1, n + 1){
        cin >> to[i];
        if (to[i]) adj[to[i]].push_back(i); 
    }
    FOR(i, 1, n + 1) if (!vis[i]) findStartPts(i);
    for (int i : startDFS){
        dfs(i);
        st.upd(tin[i], tout[i], i);
    }
    cin >> q;
    while (q--){
        int tp, A, B; cin >> tp;
        if (tp == 1) cin >> A, rem(A);
        else cin >> A >> B, cout<<solve(A, B)<<"\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2764 KB Output is correct
2 Correct 7 ms 2768 KB Output is correct
3 Correct 9 ms 2768 KB Output is correct
4 Correct 24 ms 2980 KB Output is correct
5 Correct 8 ms 2768 KB Output is correct
6 Correct 38 ms 3048 KB Output is correct
7 Correct 38 ms 3068 KB Output is correct
8 Correct 37 ms 3024 KB Output is correct
9 Correct 37 ms 3024 KB Output is correct
10 Correct 39 ms 3088 KB Output is correct
11 Correct 237 ms 16232 KB Output is correct
12 Correct 210 ms 9960 KB Output is correct
13 Correct 223 ms 11912 KB Output is correct
14 Correct 207 ms 9132 KB Output is correct
15 Correct 225 ms 10336 KB Output is correct
16 Correct 208 ms 10096 KB Output is correct
17 Correct 250 ms 14000 KB Output is correct
18 Correct 230 ms 14152 KB Output is correct
19 Correct 225 ms 13896 KB Output is correct
20 Correct 228 ms 16480 KB Output is correct