/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 255ms 11.789 MiB
#3 Accepted 73ms 7.117 MiB
#4 Accepted 51ms 6.883 MiB
#5 Accepted 35ms 11.754 MiB
#6 Accepted 198ms 1.648 MiB
#7 Accepted 31ms 532.0 KiB

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++) {
            auto it=lower_bound(p.begin(),p.end(),pos[i]);
            if(it!=p.end()){
                if(*it==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
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 17:37:03
Judged At
2025-01-02 17:37:03
Judged By
Score
100
Total Time
255ms
Peak Memory
11.789 MiB