hdu 5495 LCS 置换群


LCS

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 207    Accepted Submission(s): 105


Problem Description
You are given two sequence  {a1,a2,...,an}  and  {b1,b2,...,bn} . Both sequences are permutation of  {1,2,...,n} . You are going to find another permutation  {p1,p2,...,pn}  such that the length of LCS (longest common subsequence) of  {ap1,ap2,...,apn}  and  {bp1,bp2,...,bpn}  is maximum.
 

Input
There are multiple test cases. The first line of input contains an integer  T , indicating the number of test cases. For each test case:

The first line contains an integer  n(1n105)  - the length of the permutation. The second line contains  n  integers  a1,a2,...,an . The third line contains  n integers  b1,b2,...,bn .

The sum of  n  in the test cases will not exceed  2×106 .
 

Output
For each test case, output the maximum length of LCS.
 

Sample Input
  
  
2 3 1 2 3 3 2 1 6 1 5 3 2 6 4 3 6 2 4 5 1
 

Sample Output
  
  
2 4
 

Source

有几个循环置换,且长度大于2,那么n-多少就是答案


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;

#define maxn 100007
int a[maxn],b[maxn],vis[maxn];
int dfs(int x){
    vis[x] = 1;
    if(vis[b[a[x]]]) return 1;
    return dfs(b[a[x]])+1;
}
int main(){
    int t,n,u;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i = 1;i <= n; i++){
            scanf("%d",&u);
            a[u] = i;
        }
        for(int i = 1;i <= n; i++)
            scanf("%d",&b[i]);
        memset(vis,0,sizeof(vis));
        int ans = n;
        for(int i = 1;i <= n; i++){
            if(vis[i] == 0) {
                if(dfs(i) > 1) ans--;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值