1 条题解

  • 0
    @ 2024-7-25 13:32:37

    参考答案:

    #include<iostream>
    
    using namespace std;
    
    const int N = 100010;
    int p[N], cnt[N];
    
    int find(int x)
    {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
    
    int main()
    {
        int n, m;
    
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) p[i] = i, cnt[i] = 1;
    
        while (m--)
        {
            string s;
            cin >> s;
            if (s == "C")
            {
                int a, b;
                cin >> a >> b;
                a = find(a), b = find(b);
                if (a != b)
                {
                    p[a] = b;
                    cnt[b] += cnt[a];
                }
            }
            else if (s == "Q1")
            {
                int a, b;
                cin >> a >> b;
                if (find(a) == find(b)) cout << "Yes" << endl;
                else cout << "No" << endl;
    
            }
            else
            {
                int a;
                cin >> a;
                cout << cnt[find(a)] << endl;
            }
        }
    
        return 0;
    }
    
    • 1

    信息

    ID
    85
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    13
    已通过
    3
    上传者