AtCoder Beginner Contest 049 & ARC065 連結 / Connectivity AtCoder - 2159 (并查集)
Problem Statement
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 Aby 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)
Input
The input is given from Standard Input in the following format:
N K L
p1 q1
:
pK qK
r1 s1
:
rL sL
Output
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.
Sample Input 1
4 3 1
1 2
2 3
3 4
2 3
Sample Output 1
1 2 2 1
All the four cities are connected to each other by roads.
By railways, only the second and third cities are connected. Thus, the answers for the cities are 1,2,2 and 1, respectively.
Sample Input 2
4 2 2
1 2
2 3
1 4
2 3
Sample Output 2
1 2 2 1
Sample Input 3
7 4 4
1 2
2 3
2 5
6 7
3 5
4 5
3 4
6 7
Sample Output 3
1 1 2 1 2 2 2 题意:给一个无向无环图,边分为两种,一种是铁路,一种是公路。
让求对于1~n中每一个节点i,有多少个节点和它是铁路和公路都联通的。(其中它自己也算,自己与自己一定是联通的。)
思路: 并查集
,我们对公路和铁路分成两个并查集来处理,如果一条公路把城市a到b联通,那么我们就合并a和b的集合,铁路同理。
最后只需要处理下对于每一个节点i的公路和铁路的集合个数。
、
细节见代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int par[maxn];
int par2[maxn];
int n;
int m1,m2;
void init()
{
repd(i,,n)
{
par[i]=i;
par2[i]=i;
}
}
int findpar(int x)
{
return x==par[x]?x:par[x]=findpar(par[x]);
}
int findpar2(int x)
{
return x==par2[x]?x:par2[x]=findpar2(par2[x]);
}
void merg(int x,int y)
{
x=findpar(x);
y=findpar(y);
if(x!=y)
{
par[x]=y;
}
}
void merg2(int x,int y)
{
x=findpar2(x);
y=findpar2(y);
if(x!=y)
{
par2[x]=y;
}
}
std::vector<int> v[maxn];
// std::vector<int> v2[]; int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gg(n);
gg(m1);
gg(m2);
init();// 并查集的预处理
int a,b;
repd(i,,m1)
{
gg(a);
gg(b);
// v[a].pb(b);
// v[b].push_back(a);
merg(a,b);// 合并集合1
}
repd(i,,m2)
{
gg(a);
gg(b);
// v[a].pb(b);
// v[b].push_back(a);
merg2(a,b);// 合并集合2
}
map<pii,int> ans;
repd(i,,n)
{ ans[mp(findpar(i),findpar2(i))]++; }
repd(i,,n)
{
cout<<ans[mp(findpar(i),findpar2(i))]<<" ";
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
AtCoder Beginner Contest 049 & ARC065 連結 / Connectivity AtCoder - 2159 (并查集)的更多相关文章
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
- AtCoder Beginner Contest 064 D - Insertion
AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...
- AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle【暴力】
AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle 我要崩溃,当时还以为是需要什么离散化的,原来是暴力,特么五层循环....我自己写怎么都 ...
随机推荐
- OpenSSL 正确安装
经过几天的各种尝试,总算正常安装了openssl,中途差点各种放弃,最后总算装好了. 环境:Win10 , 为了装OpenSSL 而安装了vs2010,没有验证必须要装的 安装步骤: .从openss ...
- Windows Server 2016-Active Directory复制概念(二)
本章继续补充有关Active Directory复制概念,具体内容如下: 连接对象: 连接对象是一个Active Directory对象,表示从源域控制器到目标域控制器的复制连接.域控制器是单个站点的 ...
- 8. svg学习笔记-文本
毫无疑问,文本也是svg中组成的重要部分,在svg中,用<text>元素来创建文本,文本的使用格式如下: <text x="20" y="30" ...
- 手把手教你“将系统安装在U盘”上,实现个人系统随身带!
本教程纯原创,转载请标注来源. 本教程适用安装的操作系统:Win XP,Win 7,优麒麟,Ubuntu,deepin,linux. 优盘要求:最好是USB3.0,USB2.0也可以,但是优盘至少要求 ...
- Echarts的一些总结
Echarts是专注做统计图表的插件,其本质是使用canvas进行图表的绘制.而如今它的属性和配置也是越来越丰富.基本的配置很简单,比如饼状图,就是数据和要显示的文字和颜色,柱状图,就是横纵坐标和数据 ...
- RMQ 字符串 F. Strings and Queries
F. Strings and Queries time limit per test 2.5 s memory limit per test 256 MB input standard input o ...
- WPF设计の不规则窗体
我们在工作中,经常会需要画一些不规则的窗体,现在总结如下. 一.利用VisualBrush实现.这依赖于VisualBrush的特性,任何控件可以作为画刷,而画刷又可以作为背景. 此种方法可以用于实现 ...
- nginx相关命令
https://www.cnblogs.com/zdz8207/p/CentOS-nginx-yum.html
- 6.03-news_xpath2
import re import requests # 安装支持 解析html和XML的解析库 lxml # pip install lxml from lxml import etree url = ...
- centos下安装 glances 的问题
开始想安装htop 然后 yum installhtop 没有 yum searchhtop 也没有 然后上github 发现一个比htop还华丽的东西. Glances 大概这个样子的. 可以一览 ...