Luo Gu P2147 [SDOI2008] cave survey

Title
The title is a \ (LCT \) bare title, but the title of the positive solution is LCT, and violence disjoint-set can solve this problem.
Since this problem is the need for the amount of middle path springboard, so that the problem does not need nor can the path compression.
First, let's consider Connect and Destroy operations.
\ (Connect \) Operation:
Since u, v will even as a whole, so FA [U] = FA [V].
\ (The Destroy \) Operation:
Since u, v will not be connected into a whole, so FA [ . v] = 0 i.e., normalized to the original state.
\ (Query \) operation:
the operation u or v can be obtained by a node in a skip up their parent node, u = v, or until one of the nodes hop 0.
at this time, u or v is determined whether they are equal. If they are equal then the upper one chain.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define int long long
#define N 4011
using namespace std;
int n, m, fa[401100];
inline int query(int u, int v)
{   
    while (v && u != v) 
    {
        v = fa[v];
        if (v == u)
            return 1;
    }
    if (u == 0) return 1;
    if (v == u) return 1;
    return 0;
}   
inline void connect(int u, int v)
{
    fa[u] = v;
}
inline void destroy(int u, int v) 
{
    fa[v] = 0; 
}
signed main()   
{
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= m; i++)
    {       
        string s; int u, v, temp = 0, u1, ha;
        cin >> s;
        scanf("%lld%lld", &u, &v);
        u1 = u;
        while (u1)
        {
            ha = fa[u1];
            fa[u1] = temp; temp = u1; u1 = ha;
        }
        if (s == "Connect")
            connect(u, v);
        if (s == "Destroy")
            destroy(u, v);
        if (s == "Query")
        {
            if (query(u, v))
                printf("Yes\n");
            else
                printf("No\n");
        }
    }       
    return 0;
}

Guess you like

Origin www.cnblogs.com/liuwenyao/p/10993671.html