/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 265ms 11.758 MiB
#3 Wrong Answer 72ms 6.945 MiB

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

class disjointset {
public:
    vector<ll> parent, size;

    disjointset(ll n) {
        parent.resize(n + 1);
        size.resize(n + 1);

        for (ll i = 1; i <= n; i++) {
            parent[i] = i;
            size[i] = 1;
        }
    }

    ll findparent(ll node) {
        if (node == parent[node]) {
            return node;
        }
        return parent[node] = findparent(parent[node]);
    }

    void join(ll u, ll v) {
        ll up = findparent(u), vp = findparent(v);
        if (up == vp) return;
        if (size[up] <= size[vp]) {
            parent[up] = vp;
            size[vp] += size[up];
        } else {
            parent[vp] = up;
            size[up] += size[vp];
        }
    }
};

void solve() {
    ll n, q,cnt=0;cin >> n >> q;

    vector<ll> v(n + 1);
    for (ll i = 1; i <= n; i++) {
        cin >> v[i];
    }

    disjointset dsu(n);

    for (ll x,y,i = 0; i < q; i++) {
        cin >> x >> y;
        dsu.join(x, y);
    }

    unordered_map<ll, vector<ll>> adj;
    for (ll i = 1; i <= n; i++) {
        adj[dsu.findparent(i)].push_back(i);
    }

    for (auto &u : adj) {
        vector<ll> pos = u.second;
        vector<ll> p;

        for (int i=0;i<pos.size();i++) {
            p.push_back(v[pos[i]]);
        }

        sort(pos.begin(), pos.end());
        sort(p.begin(), p.end());

        for (int i = 0; i < pos.size(); i++) {
            if (p[i] == pos[i]) {
                cnt++;
            }
        }
    }

    cout << cnt << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    ll t=1;cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1119 Maximizing Fixed Points
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 16:50:21
Judged At
2025-01-02 16:50:21
Judged By
Score
5
Total Time
265ms
Peak Memory
11.758 MiB