Connectivity
6492: Connectivity
时间限制: 1 Sec 内存限制: 128 MB
提交: 118 解决: 28
[提交][状态][讨论版][命题人:admin]
题目描述
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)
输入
N K L
p1 q1
:
pK qK
r1 s1
:
rL sL
输出
样例输入
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;
}
最新文章
- c++转载系列 std::vector模板库用法介绍
- Spark基础知识汇总
- ubuntu su Authentication failure
- ASP.NET Web API 路由
- javascript 的默认对象
- fis-receiver:一行命令将项目部署到远程服务器
- Linux - gcc和g++的区别
- JavaScript开发之路02(Sencha Touch使用时常见问题及解决办法)
- android 添加左右滑屏手势
- LVS负载均衡中arp_ignore和arp_annonuce参数配置的含义
- Shell第三篇:基本语法
- [java语言]——InetAddress类的getByName()方法
- es head插件通过Nginx http basic 限制访问
- const成员函数用法
- javaScript之分支判断与内置对象
- 合作开发工具——freeze和pipreqs
- EF Core中DbContext可以被Dispose多次
- python正则表达式查找汉字
- Kettle 使用入门
- Android bp语法介绍
热门文章
- Pure CSS 的网格布局(比bootstrap小很多且易扩展的css UI)
- Scipy实现图片去噪
- excel常用函数之find,left,right,mid,len
- unity打包选项编辑器扩展
- poj3191(进制转换)
- django后台管理系统(admin)的简单使用
- 坑爹的 Java 可变参数,把我整得够惨。。
- MFS安装
- oracle view and MATERIALIZED VIEW
- falsk-sqlalchemy 连接数据库出现 No module named &#39;MySQLdb&#39;