6492: Connectivity

时间限制: 1 Sec  内存限制: 128 MB
提交: 118  解决: 28
[提交][状态][讨论版][命题人:admin]

题目描述

There are N cities. There are also K roads and L railways, extending between the cities. The i-th road bidirectionally connects the pi-th and qi-th cities, and the i-th railway bidirectionally connects the ri-th and si-th cities. No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.
We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads. We will also define connectivity by railways similarly.
For each city, find the number of the cities connected to that city by both roads and railways.

Constraints
2≤N≤2*105
1≤K,L≤105
1≤pi,qi,ri,si≤N
pi<qi
ri<si
When i≠j, (pi,qi)≠(pj,qj)
When i≠j, (ri,si)≠(rj,sj)

输入

The input is given from Standard Input in the following format:
N K L
p1 q1
:
pK qK
r1 s1
:
rL sL

输出

Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.

样例输入

4 3 1
1 2
2 3
3 4
2 3

样例输出

1 2 2 1
统计两个并查集交集所含元素的个数,这个数就是交集中的每一个元素对应的答案(实际上是由每一个元素得到总的个数)
还有就是熟练掌握map和pair的用法,我就是知道思路但不知道怎么实现!
AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+100;
#define p make_pair
map<pair<int,int>,int>mapp;
int sa[maxn],sb[maxn];
struct record
{
int fa[maxn];
void unit(int n)
{
for(int i=1;i<=n;i++)
{
fa[i]=i;
}
}
int finding(int a)
{
if(fa[a]==a)
{
return a;
}
return fa[a]=finding(fa[a]);
}
void union_(int x,int y)
{
int b1=finding(x);
int b2=finding(y);
if(b1!=b2)
{
fa[b1]=b2;
}
}
};
int main()
{
int n,k,l,x,y;
scanf("%d %d %d",&n,&k,&l);
record s1,s2;
s1.unit(n);
s2.unit(n);
for(int i=1;i<=k;i++)
{
scanf("%d %d",&x,&y);
s1.union_(x,y);
}
for(int i=1;i<=l;i++)
{
scanf("%d %d",&x,&y);
s2.union_(x,y);
}
for(int i=1; i<=n; i++)
{
sa[i]=s1.finding(i);
sb[i]=s2.finding(i);
mapp[p(sa[i],sb[i])]++;
}
for(int i=1;i<=n;i++)
{
if(i==n)
{
printf("%d\n",mapp[p(sa[i],sb[i])]);
}
else
{
printf("%d ",mapp[p(sa[i],sb[i])]);
}
}
return 0;
}

Connectivity的更多相关文章

  1. Euler Tour Tree与dynamic connectivity

    Euler Tour Tree最大的优点就是可以方便的维护子树信息,这点LCT是做不到的.为什么要维护子树信息呢..?我们可以用来做fully dynamic connectivity(online) ...

  2. iOS 7 与 Xamarin - MultiPeer Connectivity(转载)

    随着时代的改变,移动设备在生活工作都开始取代原有的pc.设备间的数据交互就成为了必备可少的功能.比较成熟的产品有NFC.这个啪啪的操作很流行,例如分享图片,分享文件等 .但是在iOS设备中还没有NFC ...

  3. Searching External Data in SharePoint 2010 Using Business Connectivity Services

    from:http://blogs.msdn.com/b/ericwhite/archive/2010/04/28/searching-external-data-in-sharepoint-2010 ...

  4. Creating a SharePoint BCS .NET Connectivity Assembly to Crawl RSS Data in Visual Studio 2010

    from:http://blog.tallan.com/2012/07/18/creating-a-sharepoint-bcs-net-assembly-connector-to-crawl-rss ...

  5. sharepoint bcs (bussiness connectivity services)

    sharepoint bcs  在2010 版本中是提供2010 与外部数据连接的. BCS全名Business Connectivity Services,可以把它看成SharePoint 2007 ...

  6. Oracle 11g RAC INS-06006 Passwordless SSH connectivity not set up between the following node(s)

    安装11g RAC的grid时,在Test互信的时候报错INS-06006 Passwordless SSH connectivity not set up between the following ...

  7. Debugging JTAG Connectivity Problems

    2013-12-04 22:34:26 转自:http://processors.wiki.ti.com/index.php/Debugging_JTAG_Connectivity_Problems ...

  8. WCF:为 SharePoint 2010 Business Connectivity Services 构建 WCF Web 服务(第 1 部分,共 4 部分)

    转:http://msdn.microsoft.com/zh-cn/library/gg318615.aspx 摘要:通过此系列文章(共四部分)了解如何在 Microsoft SharePoint F ...

  9. 利用Multipeer Connectivity框架进行WiFi传输

    什么是Multipeer Connectivity? 在iOS7中,引入了一个全新的框架——Multipeer Connectivity(多点连接).利用Multipeer Connectivity框 ...

  10. 利用Multipeer Connectivity框架进行WiFi传输-b

    什么是Multipeer Connectivity? 在iOS7中,引入了一个全新的框架——Multipeer Connectivity(多点连接).利用Multipeer Connectivity框 ...

随机推荐

  1. MS SQL JSON类型type

    在MS SQL Server 2016,已经支持JSON处理. 执行下面代码,将获取ms sql server对象类型以及其说明: IF OBJECT_ID('tempdb.dbo.#json_typ ...

  2. 杭电1003_Max Sum

    这是原题的链接http://acm.hdu.edu.cn/showproblem.php?pid=1003 起初我是利用暴力的方法,求出所有序列的和的情况,每取一个序列就和以知道的最大和作对比,取大者 ...

  3. js混杂笔记

    1.判断对象为空的方法 1)Object.keys({}).length === 0 // true 2)Object.getOwnPropertyNames({}).length === 0 // ...

  4. IT兄弟连 Java语法教程 Java开发环境 JVM、JRE、JDK

    要想开发Java程序,就需要知道什么是JVM.JRE以及JDK.JVM是运行Java程序的核心,JRE是支持Java程序运行的环境,而JDK是Java开发的核心,下面我们分别具体介绍它们以及它们之间的 ...

  5. [转] 【iOS基础知识】之判断NSString是否为整数、浮点数

    //判断是否为整形: - (BOOL)isPureInt:(NSString*)string{ NSScanner* scan = [NSScannerscannerWithString:string ...

  6. JDK动态代理 Proxy InvocationHandler

  7. 常见的HTTP状态码说明

    1.说明 HTTP服务器状态代码定义(Status Code Definitions) 做测试的时候,会产生比较多的HTTP错误,查看其错误,有超时的,链接不到图片的,连接不到服务器等等,很多人经常忘 ...

  8. NET Core 2.0 的 REST API

    用ASP.NET Core 2.0 建立规范的 REST API -- 预备知识 (2) + 准备项目 上一部分预备知识在这 http://www.cnblogs.com/cgzl/p/9010978 ...

  9. Net Core2.0下使用Dapper

    Net Core2.0下使用Dapper 今天成功把.Net Framework下使用Dapper进行封装的ORM成功迁移到.Net Core 2.0上,在迁移的过程中也遇到一些很有意思的问题,值得和 ...

  10. NET Core 2.0 介绍和使用

    NET Core 2.0 特性介绍和使用指南 阅读目录 前言 特性概述 使用指南 .NET Core 2.0和1.0/1.1之间的关系 .NET CORE Rumtime改进 .NET Core SD ...