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. Spring Data JPA stackoverflow

    1.禁止使用lombok 的@Data 注释 使用@Data注释后,默认会重写父类的toString()方法,hashcode()等方法,在往map里存的时候,会根据equals和hashcode方法 ...

  2. 快速发现并解决maven依赖传递冲突

    此文已由作者翟曜授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近在测试过程中,遇到了几次maven传递依赖冲突的问题,所以记录下解决的过程,遇到类似问题供参照. 问题现象 ...

  3. python反编译之字节码

    如果你曾经写过或者用过 Python,你可能已经习惯了看到 Python 源代码文件:它们的名称以.Py 结尾.你可能还见过另一种类型的文件是 .pyc 结尾的,它们就是 Python "字 ...

  4. Git提交与恢复

    Git提交与恢复 提交修改 git add --all # 提交所有修改文件 git add file file # 提交部分修改文件 $ git status On branch master Yo ...

  5. codeforces358D Dima and Hares【dp】

    从本质入手,这个东西影响取值的就是相邻两个哪个先取 设f[i][0/1]为前i个(i-1,i)中先取i/i-1的值(这里不算上i的贡献 转移就显然了,注意要先复制-inf #include<io ...

  6. ps 命令参数解释

    转自:https://www.cnblogs.com/fps2tao/p/7692482.html A 显示所有进程(等价于-e)(utility)-a 显示一个终端的所有进程,除了会话引线-N 忽略 ...

  7. MyBatis入门Bug集锦X1

  8. CSS样式之操作属性一

    ********css之操作属性******** 一.文本 1.文本颜色:color 颜色属性被用来设置文字的颜色 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 ...

  9. php:php相关的函数或用法记录

    //1:判断字符串是否全是字母组成的 $str = 'AAKAaa_aLJIGF'; var_dump(ctype_alpha($str)); //boolean false,全部是英文时才是返回tr ...

  10. NET Core 2.1.0 now available

    ASP.NET Core 2.1.0 now available https://blogs.msdn.microsoft.com/webdev/2018/05/30/asp-net-core-2-1 ...