4 条题解

  • 0
    @ 2026-5-19 16:43:36
    #include<vector>
    #include<iostream>
    #include<map>
    #include<queue>
    
    using namespace std;
    using ll = long long ;
    
    priority_queue<int> mxheap;
    priority_queue<int,vector<int>,greater<int>> mnheap;
    
    const int N =1e6+9;
    int n;
    int a[N];//存储原数组
    int c[N];//存储联通
    int p[N];//存储数量
    
    void solve(){
    	cin>>n;
    	for(int i=1; i<=n;i++) cin>>a[i];
    	
    	int id = 0;
    	
    	for(int i=1; i<=n;i++){
    		if(!c[i]) id++;
    		
    		
    		int curr = i ;
    		int count = 0 ;
    		while(!c[curr]){
    			c[curr] = id;
    			count++;
    			curr = a[curr];
    			
    			p[id] = count;
    		}
    		
    	}
    	
    	int mx =0 ;
    	for(int i=1; i<=id ; i++){
    		mx = max(mx , p[i]);
    	}
    	
    	for(int i=1; i<=n;i++){
    		int left = c[i];
    		int right = c[i+1];
    		
    		if(left != right){
    			mx = max(mx , p[left] + p[right]);
    		}
    	}
    	
    	
    	cout<<mx<<endl;
    }
    
    int main(){
    	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    	int t_ = 1;
    	while(t_--) solve();
    	
    	
    	return 0;
    }
    
    

    信息

    ID
    608
    时间
    1000ms
    内存
    256MiB
    难度
    普及
    标签
    递交数
    431
    已通过
    139
    上传者