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. HDU - 6081 2017百度之星资格赛 度度熊的王国战略

    度度熊的王国战略  Accepts: 644  Submissions: 5880  Time Limit: 40000/20000 MS (Java/Others)  Memory Limit: 3 ...

  2. Django 之验证码实现

    1. django-simple-captcha 模块 安装 django-simple-captcha pip install django-simple-captcha pip install P ...

  3. Node-SASS安装

    鉴于国内的环境,node-sass实在是太难安装了,可以直接通过淘宝的npm镜像来安装. 1.安装cnpm(https://npm.taobao.org/) npm install -g cnpm - ...

  4. 洛谷P3847 [TJOI2007]调整队形

    P3847 [TJOI2007]调整队形 题目背景 学校艺术节上,规定合唱队要参加比赛,各个队员的衣服颜色不能很混乱:合唱队员应排成一横排,且衣服颜色必须是左右对称的. 例如:“红蓝绿蓝红”或“红蓝绿 ...

  5. cogs 1901. [国家集训队2011]数颜色

    Cogs 1901. [国家集训队2011]数颜色 ★★★   输入文件:nt2011_color.in   输出文件:nt2011_color.out   简单对比时间限制:0.6 s   内存限制 ...

  6. HTML5元素拖放设置总结

    将元素图片放入div盒子内 1.首先设置元素为可拖放:在img标签内加入draggable=”true”. <img draggable="true"> 2.设置元素的 ...

  7. JSF 与 struts2

    http://suhuanzheng7784877.iteye.com/blog/1041411

  8. day5字典作业详解

    1.day5题目 1.有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2&q ...

  9. JS高级学习历程-11

    [继承] 在php,一个类去继承另一个类,本类实例化出来的对象,既可以调用本身类的成员,也可以调用父类的成员. 在javascript继承主要通过原型实现,构造函数继承一个对象,构造函数的实例会拥有被 ...

  10. CodeForces - 287B-Pipeline(二分)

    Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ult ...