zoukankan      html  css  js  c++  java
  • 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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <vector>
     5 #include <string>
     6 #include <string>
     7 #include <map>
     8 #include <cmath>
     9 #include <set>
    10 #include <ctime>
    11 #include <algorithm>
    12 using namespace std;
    13 const int N=5e5+9;
    14 const int M=1e5+9;
    15 using namespace std;
    16 int t,n,a[N],b[N];
    17 //map<int,int>mp;
    18 int mp[M];
    19 vector<int>ve[M];
    20 int ans1,ans2;
    21 bool vis[M];
    22 void dfs(int x)
    23 {
    24     vis[x]=1;
    25     for(int i=0;i<ve[x].size();i++){
    26         int y=ve[x][i];
    27         if(!vis[y])
    28         {
    29             dfs(y);
    30         }
    31     }
    32 }
    33 int  main()
    34 {
    35     scanf("%d",&t);
    36     while(t--)
    37     {    clock_t sta=clock();
    38         scanf("%d",&n);
    39         //mp.erase(mp.begin(),mp.end());//用map会超时
    40         memset(mp,0,sizeof(mp));
    41         //memset(vis,0,sizeof(vis));
    42         for(int i=1;i<=M;i++)  ve[i].clear();
    43         for(int i=1;i<=n;i++)
    44         {
    45             scanf("%d",&a[i]);
    46         }
    47         for(int i=1;i<=n;i++)  scanf("%d",&b[i]);
    48         for(int i=1;i<=n;i++)  {
    49             if(a[i]!=b[i]){
    50                 mp[a[i]]=1;
    51                 mp[b[i]]=1;
    52                 ve[a[i]].push_back(b[i]);
    53                 ve[b[i]].push_back(a[i]);//一定是无向图,不然可能一个联通快走不遍
    54             }
    55         }
    56         ans1=ans2=0;
    57         memset(vis,0,sizeof(vis));
    58         //将该子联通快的所有点和根相互交换
    59         for(int i=1;i<=M;i++){
    60            if(mp[i]==1){
    61                ans1++;
    62                if(vis[i]==0){
    63                    dfs(i);
    64                    ans2++;
    65                }
    66            }    
    67         }
    68         printf("%d
    ",ans1-ans2);
    69         clock_t end=clock();
    70         //printf("%d
    ",end-sta);
    71         cout<<end-sta<<endl;
    72     }
    73     return 0;
    74 }
  • 相关阅读:
    spring boot 2.0.3+spring cloud (Finchley)2、搭建负载均衡Ribbon (Eureka+Ribbon+RestTemplate)
    spring boot 2.0.3+spring cloud (Finchley)1、搭建服务注册和发现组件Eureka 以及构建高可用Eureka Server集群
    windows下安装redis
    Spring注解@Resource和@Autowired的区别
    spring常用管理bean注解
    maven中package和install命令的区别
    SpringBoot打war包并部署到tomcat下运行
    使用jquery.qrcode生成二维码及常见问题解决方案
    动态生成二维码插件 jquery.qrcode.js
    Notepad++的列编辑功能
  • 原文地址:https://www.cnblogs.com/tingtin/p/9399100.html
Copyright © 2011-2022 走看看