/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.812 MiB
#2 Accepted 214ms 10.316 MiB
#3 Accepted 88ms 7.09 MiB
#4 Accepted 63ms 6.914 MiB
#5 Accepted 13ms 3.496 MiB
#6 Accepted 114ms 3.145 MiB
#7 Accepted 25ms 2.902 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll , ll>                   PII;
const int sz = 1e5 + 5 ;
int n , q , x , y, a[sz] , point[sz] ;
vector < int > Edge[sz] ;
bool vis[sz] ;
void dfs(int id , int p) {
   vis[id] = true ; point[id] = p ;
   for(auto child : Edge[id]){
    if(!vis[child]) dfs(child , p) ;
   }
}
void solve()
{
   cin >> n >> q ;
   for(int i = 1 ; i <= n ; i++) cin >> a[i] ,point[i] = vis[i] = 0 , Edge[i].clear() ;
   while(q--){
    cin >> x >> y ;
    Edge[x].pb(y) ;
    Edge[y].pb(x) ;
   }
   int p = 1 , Ans = 0;
   for(int i = 1 ; i <= n ; i++) if(!vis[i]) dfs(i , p++) ;
   for(int i = 1 ; i <= n ; i++) if(point[i] == point[a[i]]) Ans++ ;
   cout << Ans << "\n" ;
}
int main()
{
     __Heart__
    // READ("input2.txt") ;
    // WRITE("output2.txt") ;
     int t ; cin >> t ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1119 Maximizing Fixed Points
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-28 00:42:47
Judged At
2024-11-11 02:36:00
Judged By
Score
100
Total Time
214ms
Peak Memory
10.316 MiB