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 我要崩溃,当时还以为是需要什么离散化的,原来是暴力,特么五层循环....我自己写怎么都 ...
随机推荐
- 孟岩:怎么看待Coin与Token的关系?
由于中英文的隔阂,很多在英文世界里一目了然.不言自明的词汇,翻译成中文之后意义模糊.难以理解.比如在区块链和加密数字货币领域,coin 和 token 的区别,很长时间困扰着我们,并且引发争论. 后来 ...
- Hibernate 5 入门指南-基于JPA
首先创建\META-INF\persistence.xml配置文件并做简单的配置 <persistence xmlns="http://java.sun.com/xml/ns/pers ...
- gcc5.4报错对‘std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()’未定义的引用
我在编译ligra是遇到了这个问题,网上搜了一遍,发现是了原因https://gcc.gnu.org/onlinedocs/libstdc%2B%2B/manual/using_dual_abi.ht ...
- C语言 矩阵的转置及矩阵的乘法
C语言 矩阵的转置及矩阵的乘法 //凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1.矩阵的转置 #include<stdio.h> #defi ...
- [福大软工] Z班 第7次成绩排行榜
作业要求 http://www.cnblogs.com/easteast/p/7668887.html 评分细则 本次作业评分较为简单,只包含了两个方面的得分,一个是团队任务的计划(10'),一个是采 ...
- own address as source address
1222.762730] br0: received packet on nbif0 with own address as source address[ 1222.769697] br0: rec ...
- ubuntu下安裝sogou拼音
方法/步骤 打开搜狗输入法Linux版的官网http://pinyin.sogou.com/linux/?r=pinyin,并下载你需要的版本,这里选择64位版. 在Ubuntu14.01下可以直接点 ...
- eshint的配置
{ "strict" : "implied", //文件里面使用"use strict" "undef" : true, ...
- 2018年6月,最新php工程师面试总结
面试经常被问到的问题总结 1.字符串函数 2.数组函数 3.cookie和session的区别 4.状态码以及其功能
- SQL LIKE 操作符
LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式. LIKE 操作符 LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式. SQL LIKE 操作符语法 SELECT colum ...