• 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. c#线程倒计时器源码

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  2. Minor GC ,Full GC 触发条件

    Minor GC ,Full GC 触发条件 Minor GC触发条件:当Eden区满时,触发Minor GC. Full GC触发条件: (1)调用System.gc时,系统建议执行Full GC, ...

  3. vue2.0:(五)、路由vue-router

    好的,接下来,我们来写路由.用的是vue2.0的路由. 步骤一:配置main.js import Vue from 'vue'; import App from './App'; import rou ...

  4. ABAP自定义功能函数

    1.实现计算器中阶乘函数 FUNCTION zfun_mm_001. *"---------------------------------------------------------- ...

  5. macOS Sierra 最新系统找回允许任何软件安装

    终端输入就可以了 安装macOS Sierra后,会发现系统偏好设置的“安全与隐私”中默认已经去除了允许“任何来源”App的选项,无法运行一些第三方应用. 如果需要恢复允许“任何来源”的选项,即关闭G ...

  6. Jmeter进行接口的性能测试

    一.录制Jmeter脚本 录制Jmeter脚本有两种方法,一种是设置代理:一种则是利用badboy软件,badboy软件支持导出jmx脚本. 这里我们介绍第二种方法,利用badboy录制脚本,然后导出 ...

  7. 【UML】部署图Deployment diagram(实现图)(转)

    http://blog.csdn.net/sds15732622190/article/details/49049665 前言 下面要介绍UML中的部署图,和构件图一样,它也属于实现图的一种,五种静态 ...

  8. Android(java)学习笔记117:SharedPreferences(轻量级存储类)

    1.SharedPreferences是Android平台上一个轻量级的存储类,简单的说就是可以存储一些我们需要的变量信息.2个activity 之间的数据传递除了可以他通过intent来传递数据,还 ...

  9. SpringMVC-概述和入门程序

    三层架构和MVC B/S三层架构 表现层:web层,一般使用MVC模型 业务层:service层 持久层:dao层 MVC模型 Model:数据模型,JavaBean的类,用来进行数据封装 View: ...

  10. JS的闭包、高阶函数、柯里化

    本文原链接:https://cloud.tencent.com/developer/article/1326958 https://cloud.tencent.com/developer/articl ...