• 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. jQuery:如何给动态生成的元素绑定事件?

    jQuery的html()可以给现在元素附加新的元素,innerHTML也可以,那么,如何给这些新生成的元素绑定事件呢?直接在元素还未生成前就绑定肯定是无效的,因为所绑定的元素目前根本不存在. 然而, ...

  2. 关于office转换成pdf组件服务中的DCOM配置问题

    在开始->运行 中录入“dcomcnfg” 单击“确定”后弹出“组件服务”窗口 依次选择“组件服务”->“计算机”->“我的电脑”->“DCOM配置” 在“DCOM配置”下找到 ...

  3. where whereis locate find 的用法

    1.where :where ifconfig.用来搜索命令,显示命令是否存在以及路径在哪 2.whereis:whereis vim .用来搜索程序名,而且只搜索二进制文件(参数-b).man说明文 ...

  4. pixhawk 固件Firmware内执行make px4fmu-v2_default 编译报错解决办法

    执行下列指令报错 make px4fmu-v2_default /bin/sh: 1: Tools/check_cmake.sh: Permission denied Makefile:44: Not ...

  5. AtCoderBeginnerContest109题解

    第一次AK,真爽qwq A 很zz啊,,直接判断三种情况就行 /* */ #include<iostream> #include<cstdio> #include<cst ...

  6. Vector 容器简单介绍

    # Vector STL简要介绍 关于STL中的vector容器,以下做一些相关介绍. #### vector 简要概述 vector 称作向量类,属于容器类,实现了动态的数组,用于元素数量变化的对象 ...

  7. 使用Python-OpenCV向图片添加噪声(高斯噪声、椒盐噪声)

    在matlab中,存在执行直接得函数来添加高斯噪声和椒盐噪声.Python-OpenCV中虽然不存在直接得函数,但是很容易使用相关的函数来实现. 代码: import numpy as np impo ...

  8. vue2使用animate css

    先上几个链接 vue插件大集合:awesome-vue vue2插件: vue2-animate:vue2-animate vue2插件vue2-animateDEMO: vue2-animatede ...

  9. ctDNA|endosymbiosis

    5.10叶绿体基因组编码多种蛋白质和RNA 叶绿体和线粒体的共同点:叶绿体和线粒体的大小,功能(编码区)大体一致,但叶绿体拥有更多基因,所以在编码tRNA时,也有内含子作为被剪切片段. 因为在原核生物 ...

  10. java基础——冒泡排序

    最近开始准备面试,所以将Java基础复习一遍,又学习了冒泡排序 冒泡排序的基本思想是,对相邻的元素进行两两比较,顺序相反则进行交换,这样,每一趟会将最小或最大的元素“浮”到顶端,最终达到完全有序 ja ...