/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 7ms 7.414 MiB
#2 Accepted 419ms 20.941 MiB
#3 Accepted 197ms 16.062 MiB
#4 Accepted 140ms 16.086 MiB
#5 Accepted 20ms 8.188 MiB
#6 Accepted 170ms 8.332 MiB
#7 Accepted 48ms 7.559 MiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=3e5+10,MOD=1000000007;
typedef long long ll;
int vis[M];
vector<int>edge[M];
vector<int>vec;
void dfs(int x){
	vis[x]=1;
	vec.push_back(x);
	for(int i:edge[x]){
		if(!vis[i]){
			dfs(i);
		}
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    cin>>t;
    while(t--){
    	int n,q;
    	cin>>n>>q;
    	vector<int>a(n+1);
    	for(int i=1;i<=n;i++)cin>>a[i];
    	for(int i=1;i<=q;i++){
    		int x,y;
    		cin>>x>>y;
    		edge[x].push_back(y);
    		edge[y].push_back(x);
    	}
    	int ans=0;
    	for(int i=1;i<=n;i++){
    		if(!vis[i]){
    			dfs(i);
    			set<int>st;
    			for(int ii:vec)st.insert(a[ii]);
    			for(int ii:vec){
    				if(st.find(ii)!=st.end())ans++;
    			}
    			vec.clear();
    			
    		}
    	}
    	for(int i=0;i<=n;i++){
    		edge[i].clear();
    		vis[i]=0;
    	}
    	cout<<ans<<"\n";
    	
    
    
    
    
    
}

    
    return 0;
 
}

Information

Submit By
Type
Submission
Problem
P1119 Maximizing Fixed Points
Contest
Lockout contest round-1 ( Araf al jami vs Kamonasish Roy)
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-30 15:29:58
Judged At
2024-12-17 11:26:10
Judged By
Score
100
Total Time
419ms
Peak Memory
20.941 MiB