ACM训练联盟周赛 A. Teemo's bad day
- 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的更多相关文章
- ACM训练联盟周赛 K. Teemo's reunited
Teemo likes to drink raspberry juice. He even spent some of his spare time tomake the raspberry jui ...
- ACM训练联盟周赛 G. Teemo's convex polygon
65536K Teemo is very interested in convex polygon. There is a convex n-sides polygon, and Teemo co ...
- 计蒜客 28449.算个欧拉函数给大家助助兴-大数的因子个数 (HDU5649.DZY Loves Sorting) ( ACM训练联盟周赛 G)
ACM训练联盟周赛 这一场有几个数据结构的题,但是自己太菜,不会树套树,带插入的区间第K小-替罪羊套函数式线段树, 先立个flag,BZOJ3065: 带插入区间K小值 计蒜客 Zeratul与Xor ...
- 计蒜客 ACM训练联盟周赛 第一场 Christina式方格取数 思维
助手Christina发明了一种方格取数的新玩法:在n*m的方格棋盘里,每个格子里写一个数.两个人轮流给格子染色,直到所有格子都染了色.在所有格子染色完后,计算双方的分数.对于任意两个相邻(即有公共边 ...
- 计蒜客 ACM训练联盟周赛 第一场 从零开始的神棍之路 暴力dfs
题目描述 ggwdwsbs最近被Zeratul和Kyurem拉入了日本麻将的坑.现在,ggwdwsbs有13张牌,Kyurem又打了一张,加起来有14张牌.ggwdwsbs想拜托你帮他判断一下,这14 ...
- 计蒜客 ACM训练联盟周赛 第一场 Alice和Bob的Nim游戏 矩阵快速幂
题目描述 众所周知,Alice和Bob非常喜欢博弈,而且Alice永远是先手,Bob永远是后手. Alice和Bob面前有3堆石子,Alice和Bob每次轮流拿某堆石子中的若干个石子(不可以是0个), ...
- ACM训练联盟周赛 Teemo's formula
Teemo has a formula and he want to calculate it quickly. The formula is . As the result may be very ...
- ACM训练联盟周赛(第三场)
A.Teemo's bad day Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his ...
- 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)
M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...
随机推荐
- oracle查询语句大全 oracle 基本命令大全
oracle查询语句大全 oracle 基本命令大全 来源于:http://download.csdn.net/download/jia584643753/5875619 1.create user ...
- 关于锚点页内链接跳转出现问题(不响应,没有反应)的解决方法(ZT)
我们知道,利用锚点可以实现页面链接跳转,也可以实现同一页面内的跳转功能. 例如:<a href="somepage.htm>某页面链接</a> 可以跳转链接到som ...
- Angular 路由route实例
iSun Design & Code AngularJS - 路由 routing 基础示例 AngularJS 路由 routing 能够从页面的一个视图跳转到另外一个视图,对单页面应用来讲 ...
- css:hover伪类的使用
:hover的使用,即当鼠标指针移入元素时,所做出的样式设置 示例一 <!DOCTYPE html> <html lang="en"> <head&g ...
- 修复使用<code>XmlDocument</code>加载含有DOCTYPE的Xml时,加载后增加“[]”字符的错误
C# LINQ TO XML - Remove “[]” characters from the DTD header http://stackoverflow.com/questions/12358 ...
- shell命令cut
cut命令用来操作字符串,可以理解为剪切字符串的工具: cut有两种用法: 1.剪切字符串中的单个字符(-c参数) 例如: str=abcdef echo $str | cut -c 1-1 输出:a ...
- 洛谷 P2424 约数和
题目背景 Smart最近沉迷于对约数的研究中. 题目描述 对于一个数X,函数f(X)表示X所有约数的和.例如:f(6)=1+2+3+6=12.对于一个X,Smart可以很快的算出f(X).现在的问题是 ...
- 从SAP客户主数据里直接创建商机(Opportunity)
在SAP CRM Fiori的Account应用里,直接在Account页面创建商机: 在SAP Cloud for Customer里: 要获取更多Jerry的原创文章,请关注公众号"汪子 ...
- java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
今天写SpringMvc时,遇到这样一个问题: java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config at org.sp ...
- Android(java)学习笔记105:Android启动过程(转载)
转载路径为: http://blog.jobbole.com/67931/ 1. 关于Android启动过程的问题: 当按下Android设备电源键时究竟发生了什么? Android的启动过程是怎么样 ...