hdu 5495 LCS(并查集)
Posted Gealo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 5495 LCS(并查集)相关的知识,希望对你有一定的参考价值。
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(1≤n≤105) - 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.
The first line contains an integer n(1≤n≤105) - 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.
题意:给你两串数字然后让你求一个顺序使得这两个序列按这种顺序排列后LCS最大,并输出LCS的长度。
由于这题的数据大用LCS肯定不行,枚举方案肯定也不行。但是由于可以随意移动且数字是1~n中的所有数,所以可以将关联的数字求出来,
这些关联的数最大能组成len-2的LCS序列拿例题打个比方。
6
1 5 3 2 6 4
3 6 2 4 5 1
关联后可以得到
(1-3-2-4-1),(5-6-5)
1 3 2 4 5 6
3 2 4 1 6 5
所以组合后LCS就为(5-2)+(3-2)=4
还有如果只有两个如(5-5)这时候加1就行
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int M = 1e5 + 10; int a[M] , b[M]; int f[M] , va[M]; void init(int n) { for(int i = 0 ; i <= n ; i++) { f[i] = i; va[i] = 1; } } int find(int x) { if(x != f[x]) f[x] = find(f[x]); return f[x]; } void Union(int x , int y) { int xx = find(x); int yy = find(y); if(xx != yy) { f[x] = yy; va[yy] += va[xx]; } } int main() { int t; scanf("%d" , &t); while(t--) { int n; scanf("%d" , &n); init(M); for(int i = 0 ; i < n ; i++) { scanf("%d" , &a[i]); } for(int i = 0 ; i < n ; i++) { scanf("%d" , &b[i]); Union(a[i] , b[i]); } int count = 0; for(int i = 0 ; i < n ; i++) { if(f[a[i]] == a[i]) { if(va[a[i]] == 1) { count++; } else { count += (va[a[i]] - 1); } } } printf("%d\n" , count); } return 0; }
以上是关于hdu 5495 LCS(并查集)的主要内容,如果未能解决你的问题,请参考以下文章
带权并查集(含种类并查集)经典模板 例题:①POJ 1182 食物链(经典)②HDU - 1829 A bug's life(简单) ③hihoCoder 1515 : 分数调查(示例代码(代