1 solutions

  • 1
    @ 2026-3-12 0:16:20

    emmmemmm

    其实就是个带权并查集喵其实就是个带权并查集喵

    #pragma GCC optimize("Ofast")
    #pragma GCC optimize("unroll-loops")
    /*
    @ Mar.26
    @ problem from:[NOI2002] 银河英雄传说
    */
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define int long long
    using s64 = signed long long;
    using u32 = unsigned int;
    using u64 = unsigned long long;
    #define lod long double
    #ifdef __linux__
    #define gc getchar
    #define pc putchar
    #endif
    #define bdy 30000
    #define rep(i,n) for(int i = 1;i <= n;++i)
    #define rep2(i,n) for(int i = n;i >= n;--i)
    #define debug(x) cout << #x << " : " << x << "\n";
    #define debug2(x,y) cout << #x << " : " << x << " " << #y << " : " << y << "\n";
    
    namespace Cplas17
    {
        const int N = 3e4 + 5;
        int father[N];
        int size1[N], d[N];
        int find(int x)
        {
            if (x == father[x])
                return x;
            int root = find(father[x]);
            d[x] = d[x] + d[father[x]];
            father[x] = root;
            return father[x];
        }
    
        void merge(int a, int b)
        {
            a = find(a), b = find(b);
            d[a] = size1[b];
            size1[b] += size1[a];
            father[a] = b;
        }
        void NaCl(void)
        {
            int t;
            cin >> t;
            for (int i = 1; i <= bdy; ++i)
                father[i] = i, size1[i] = 1;
            while (t--)
                {
                    char c;
                    int x, y;
                    cin >> c >> x >> y;
                    if (c == 'M')
                        {
                            merge(x, y);
                        }
                    else
                        {
                            int a = find(x);
                            int b = find(y);
                            if (a == b)
                                {
                                    cout << max(abs(d[x] - d[y] ) - 1, 0LL) << '\n';
                                }
                            else
                                cout << -1 << '\n';
                        }
                }
        }
    }
    
    auto Freopen = [](unsigned) -> void
    {
        freopen("file.in", "r", stdin);
        freopen("file.out", "w", stdout);
    };
    
    signed main(void) noexcept
    {
        cin.tie(nullptr)->sync_with_stdio(false);
    #ifdef __linux__
        //Freopen();
    #endif
        setvbuf(stdin, NULL, _IOFBF, 1 << 25);
        setvbuf(stdout, NULL, _IOFBF, 1 << 25);
        // cout<<fixed<<setprecision(number);
        int T_ = 1;
        //cin >> T_;
        while (T_--)
            Cplas17::NaCl();
        return 0;
    }
    
    • 1

    Information

    ID
    5317
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    6
    Tags
    # Submissions
    158
    Accepted
    47
    Uploaded By