• 65536K
 

Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his homework.But Teemo is very self-confident, he tells the teacher that the problems in the homework are too simple to solve. So the teacher gets much angrier and says"I will choose a problem in the homework, if you can't solve it, I will call you mother! "

The problem is that:

There is an array A which contains n integers, and an array B which also contains n integers. You can pay one dollar to buy a card which contains two integers a1 and a2, The card can arbitrary number of times transform a single integer a1 to a2 and vise-versa on both array A and Array B. Please calculate the minimum dollars you should pay to make the two array same(For every 1<=i<=n,A[i]=B[i]);

Input Format

  • The first line of the input contains an integer T(1<=T<=10), giving the number of test cases.
  • For every test case, the first line contains an integer n(1<=n<=500000). The second line contains n integers. The i th integer represents A[i](1<=A[i]<=100000). And the third line contains n integers. The i th integer represents B[i](1<=B[i]<=100000).

Output Format

For each test case, output an integer which means the minimum dollars you should pay in a line.

样例输入

1
5
1 1 2 3 2
1 2 3 1 1

样例输出

2
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <string>
#include <map>
#include <cmath>
#include <set>
#include <ctime>
#include <algorithm>
using namespace std;
const int N=5e5+;
const int M=1e5+;
using namespace std;
int t,n,a[N],b[N];
//map<int,int>mp;
int mp[M];
vector<int>ve[M];
int ans1,ans2;
bool vis[M];
void dfs(int x)
{
vis[x]=;
for(int i=;i<ve[x].size();i++){
int y=ve[x][i];
if(!vis[y])
{
dfs(y);
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{ clock_t sta=clock();
scanf("%d",&n);
//mp.erase(mp.begin(),mp.end());//用map会超时
memset(mp,,sizeof(mp));
//memset(vis,0,sizeof(vis));
for(int i=;i<=M;i++) ve[i].clear();
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++) scanf("%d",&b[i]);
for(int i=;i<=n;i++) {
if(a[i]!=b[i]){
mp[a[i]]=;
mp[b[i]]=;
ve[a[i]].push_back(b[i]);
ve[b[i]].push_back(a[i]);//一定是无向图,不然可能一个联通快走不遍
}
}
ans1=ans2=;
memset(vis,,sizeof(vis));
//将该子联通快的所有点和根相互交换
for(int i=;i<=M;i++){
if(mp[i]==){
ans1++;
if(vis[i]==){
dfs(i);
ans2++;
}
}
}
printf("%d\n",ans1-ans2);
clock_t end=clock();
//printf("%d\n",end-sta);
cout<<end-sta<<endl;
}
return ;
}

ACM训练联盟周赛 A. Teemo's bad day的更多相关文章

  1. ACM训练联盟周赛 K. Teemo's reunited

    Teemo likes to drink raspberry juice.  He even spent some of his spare time tomake the raspberry jui ...

  2. ACM训练联盟周赛 G. Teemo's convex polygon

    65536K   Teemo is very interested in convex polygon. There is a convex n-sides polygon, and Teemo co ...

  3. 计蒜客 28449.算个欧拉函数给大家助助兴-大数的因子个数 (HDU5649.DZY Loves Sorting) ( ACM训练联盟周赛 G)

    ACM训练联盟周赛 这一场有几个数据结构的题,但是自己太菜,不会树套树,带插入的区间第K小-替罪羊套函数式线段树, 先立个flag,BZOJ3065: 带插入区间K小值 计蒜客 Zeratul与Xor ...

  4. 计蒜客 ACM训练联盟周赛 第一场 Christina式方格取数 思维

    助手Christina发明了一种方格取数的新玩法:在n*m的方格棋盘里,每个格子里写一个数.两个人轮流给格子染色,直到所有格子都染了色.在所有格子染色完后,计算双方的分数.对于任意两个相邻(即有公共边 ...

  5. 计蒜客 ACM训练联盟周赛 第一场 从零开始的神棍之路 暴力dfs

    题目描述 ggwdwsbs最近被Zeratul和Kyurem拉入了日本麻将的坑.现在,ggwdwsbs有13张牌,Kyurem又打了一张,加起来有14张牌.ggwdwsbs想拜托你帮他判断一下,这14 ...

  6. 计蒜客 ACM训练联盟周赛 第一场 Alice和Bob的Nim游戏 矩阵快速幂

    题目描述 众所周知,Alice和Bob非常喜欢博弈,而且Alice永远是先手,Bob永远是后手. Alice和Bob面前有3堆石子,Alice和Bob每次轮流拿某堆石子中的若干个石子(不可以是0个), ...

  7. ACM训练联盟周赛 Teemo's formula

    Teemo has a formula and he want to calculate it quickly. The formula is . As the result may be very ...

  8. ACM训练联盟周赛(第三场)

    A.Teemo's bad day Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his ...

  9. 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)

    M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...

随机推荐

  1. 基于CDH5.7.x Kylin部署

    配置目标文件为 /etc/profile #Kylin exportKYLIN_HOME=/opt/apache-kylin-1.5.4-cdh5.7-bin #Hadoop export HBASE ...

  2. 为什么数据库ID不能作为URL中的标识符

    最近公司在进行网站的SEO优化,将所有主要页面的URL统一更改为新的格式,其中重要的一项改变是将所有URL的标识符统一为ID,例如过去我们的一个用户的公共页面URL是这样的 https://www.e ...

  3. 《从0到1学习Flink》—— 如何自定义 Data Source ?

    前言 在 <从0到1学习Flink>-- Data Source 介绍 文章中,我给大家介绍了 Flink Data Source 以及简短的介绍了一下自定义 Data Source,这篇 ...

  4. 解决ueditor jquery javascript 取值问题

    代码如下: var content = UE.getEditor('myEditor').getContent();   myEditor是ueditor 的名称name.   代码如下: <t ...

  5. 卡了很久的bug

    背景:在一个简单的项目中,通过循环前端传来的一个数组,使用mongodb条件查询数据,将满足条件的数据push进一个新数组,并返回至前端. 问题:每次调试到第五行,会自动跳过,直接执行12行,打印出来 ...

  6. CentOS7.2上搭建httpbin环境

    CentOS7上搭建httpbin环境 1.安装python31)安装python3.6可能使用的依赖yum -y install openssl-devel bzip2-devel expat-de ...

  7. HDU 2188 悼念512汶川大地震遇难同胞——选拔志愿者(巴什博弈)

    思路:若能给对方留下m+1,就可以胜.否则败. #include <iostream> using namespace std; int main() { int t,n,m;cin> ...

  8. BZOJ 2851: 极限满月 虚树 or 树链的并

    2851: 极限满月 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 170  Solved: 82[Submit][Status][Discuss] ...

  9. C#去掉字符串最后面的一个标点符号的写法

    keywordHtml = keywordHtml.Remove(keywordHtml.LastIndexOf(','),1);

  10. UVA 1623 Enther the Dragon 神龙喝水 (贪心)

    贪心,每次遇到一个满水的湖要下暴雨的时候,就往前找之前最后一次满水之后的第一个没有下雨的且没有被用掉天day1. 因为如果不选这day1,那么之后的湖不一定能选上这一天.如果这一天后面还有没有下雨的天 ...