链接:http://xcacm.hfut.edu.cn/problem.php?id=1234

以后关于字符的输入都用cin吧,换成scanf居然wa了

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <set>
#define Max 10005
using namespace std;
int pre[Max],Rank[Max];
int sum;
void init()
{
for(int i=;i<Max;i++)
pre[i]=i;
return;
}
int find(int x)
{
if(pre[x]==x)
return x;
return pre[x]=find(pre[x]);
}
int unite(int a,int b)
{
int x=find(a);
int y=find(b);
if(Rank[x]==Rank[y])
return ;
sum--;
if(Rank[x]<Rank[y])
pre[y]=x;
else
pre[x]=y;
return ;
}
int main()
{
int n,m;
char ch;
string s,a,b;
map<string,int> u;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&n)!=EOF)
{
sum=n;
u.clear();
init();
for(int i=;i<n;i++)
{
cin>>s;
u[s]=i+;
Rank[i+]=i+;
}
cin>>m;
for(int i=;i<m;i++)
{
cin>>ch;
if(ch=='a')
{
cin>>a>>b;
unite(u[a],u[b]);
}
else if (ch=='q')
{
cin>>a;
int r=find(u[a]);
printf("%d\n",Rank[r]);
}
else if (ch=='t')
printf("%d\n",sum);
}
}
return ;
}

1234: ZJTZYRC筛offer(并查集 )的更多相关文章

  1. 【春训团队赛第四场】补题 | MST上倍增 | LCA | DAG上最长路 | 思维 | 素数筛 | 找规律 | 计几 | 背包 | 并查集

    春训团队赛第四场 ID A B C D E F G H I J K L M AC O O O O O O O O O 补题 ? ? O O 传送门 题目链接(CF Gym102021) 题解链接(pd ...

  2. hdu 4750 Count The Pairs(并查集+二分)

    Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...

  3. L2-007 家庭房产 (25 分) (并查集)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805068539215872 题目: 给定每个人的家庭成员和其自己名 ...

  4. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)

    Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  5. 天梯赛 L2-007. (并查集) 家庭房产

    题目链接 题目描述 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式 ...

  6. PAT 1114 Family Property[并查集][难]

    1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned ...

  7. 【bzoj5183】[Baltic2016]Park 离线+对偶图+并查集

    题目描述 在Byteland的首都,有一个矩形围栏围起来的公园.在这个公园里树和访客都以一个圆形表示.公园有四个出入口,每个角落一个(1=左下角,2=右下角,3=右上角,4=左上角).访客能通过这些出 ...

  8. PATL2-007. 家庭房产-并查集

    L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定每个人的家庭成员和其自己名下的房产,请你统计出每个 ...

  9. PAT甲级——1114 Family Property (并查集)

    此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...

随机推荐

  1. Altium Designer 导出Gerber文件详细教程

    Altium Designer 导出Gerber文件详细教程   1.用Altium打开需要导出Gerber文件的PCB: 2.点击“File”-“fabricatio Outputs ” “Gerb ...

  2. USBSpirit(USB精灵)更新到1.2.300.105

    USBSpirit(USB精灵)是CopyU!的内核引擎,CopyU!的主要功能均由该引擎提供,此次更新主要内容如下:(版本号:1.2.300.105) 1.[修复]:修复了几处引擎的资源泄露问题,提 ...

  3. c语言内存模型

    文章一.C语言的内存分配模型 1.程序代码区:存放函数体的二进制代码. 2.全局区数据区:全局数据区划分为三个区域.全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域,未初始化 ...

  4. HDU_2553——n皇后问题,作弊

    在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出有多少种合法的放置方法.   Inp ...

  5. 寻访上海西服定制店_Enjoy·雅趣频道_财新网

    寻访上海西服定制店_Enjoy·雅趣频道_财新网 寻访上海西服定制店

  6. flume-agent实例

    flume    多种适配,多样化的数据收集    核心概念        event:一条消息        client:访问者        agent:            重要组件Sour ...

  7. Day02_VI基本操作及C基础

    2013年09月30日 星期一 09时37分03秒 回顾:     1. linux系统的知识背景     2. vi的使用 在正常模式下使用nyy可以把光标所在行开始的连续n行拷贝到剪贴板上去 在正 ...

  8. Socket小结

    TCP/IP 要想理解socket首先得熟悉一下TCP/IP协议族, TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间 ...

  9. 学习 Netty 3.x

    study link: http://netty.io/3.6/guide/#architecture 应用场景: Chat server that requires persistent conne ...

  10. [Polymer] Custom Elements: Styling

    Code: <dom-module id="business-card"> <template> <div class="card" ...