1 条题解

  • 0
    @ 2025-7-29 20:31:38

    C++ :

    /*
    * @By: Ocean
    * @Email: weatheringocean@gmail.com
    * @Created In: 2025-06-16 21:48:19
    */
    
    #include <bits/stdc++.h>
    using namespace std;
    
    using i64 = long long;
    using u64 = unsigned long long;
    using i32 = int;
    using u32 = unsigned;
    using f64 = double;
    
    template <typename T>
    using min_heap = priority_queue<T, vector<T>, greater<T>>;
    template <typename T>
    using max_heap = priority_queue<T, vector<T>, less<T>>;
    
    
    void solve()
    {
        int n, m;
        if (!(cin >> n >> m)) return;
        vector<int> A(n + 1), B(n + 1);
        for (int i = 1; i <= n; ++i)
            cin >> A[i] >> B[i];
        vector<int> owner(m + 1, 0);
        vector<int> answer(n + 1, 0);
        int matched = 0;
        for (int i = n; i >= 1; --i) {
            int s = i;
            int ment = A[s];
            while (true) {
                if (owner[ment] == 0) {
                    owner[ment] = s;
                    ++matched;
                    break;
                }
                if (owner[ment] < s) {
                    break;
                }
                int t = owner[ment];
                owner[ment] = s;
                s = t;
                ment = (A[s] ^ B[s] ^ ment);
            }
            answer[i] = matched;
        }
        for (int i = 1; i <= n; ++i)
            cout << answer[i] << '\n';
    }
    
    int main(int argc, char *argv[], char *envp[])
    {
        cin.tie(nullptr)->sync_with_stdio(false);
    
        // for (int i = 1, n = (cin >> n, n); i <= n; i++)
            solve();
    
        return 0;
    }
    
    
    • 1

    信息

    ID
    4223
    时间
    1000ms
    内存
    128MiB
    难度
    3
    标签
    递交数
    0
    已通过
    0
    上传者