/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 149ms 1.289 MiB
#3 Accepted 94ms 1.293 MiB
#4 Accepted 64ms 1.07 MiB
#5 Accepted 24ms 1.277 MiB
#6 Accepted 203ms 616.0 KiB
#7 Accepted 48ms 540.0 KiB

Code

// I AM A MUSLIM

#include "bits/stdc++.h"

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#define fast_io std::ios::sync_with_stdio(0);std::cin.tie(0)
#define lli long long int
#define flush fflush(stdout)
#define new_line printf("\n")
#define yn(a, b) printf("%s\n", (a) >= (b) ? "Yes":"No")
#define amodm(a, M) (((a)%M+M)%M)
// #define int lli

using pii = std::pair<int,int>;

const int MOD = 1000000007;
const int mxN = 200100;

int lead[mxN];
class DSU {
public:
    DSU(int lim) {
        for (int i = 0; i <= lim; i++) {
            lead[i] = i;
        }
    }
    int find_root(int x) {
        if (x==lead[x]) return x;
        return lead[x] = find_root(lead[x]);
    }
    void merge(int u, int v) {
        lead[find_root(v)] = find_root(u);
    }
};

signed main() {
    int testCases=1;
    scanf("%d",&testCases);
    
    for (int TC = 1; TC <= testCases; TC++) {
        int n, q;
        scanf("%d%d",&n,&q);
        DSU dsu(n);
        
        int pos[n+1];
        for (int i = 1, v; i <= n; i++) {
            scanf("%d",&v);
            pos[v] = i;
        }
        
        for (int i = 0, u, v; i < q; i++) {
            scanf("%d %d",&u,&v);
            dsu.merge(u, v);
        }
        
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            int cur = pos[i];
            if (dsu.find_root(cur) == dsu.find_root(i)) {
                ans++;
            }
        }
        
        printf("%d\n", ans);
        
    }
    
    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 15:38:38
Judged At
2025-01-02 15:38:38
Judged By
Score
100
Total Time
203ms
Peak Memory
1.293 MiB