题目描述:
D - 連結 / Connectivity
Time Limit: 2 sec / Memory Limit: 256 MB
Score : 400400 points
Problem Statement
There are NN cities. There are also KK roads and LL railways, extending between the cities. The ii-th road bidirectionally connects the pipi-th and qiqi-th cities, and the ii-th railway bidirectionally connects the riri-th and sisi-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 AA and BB are connected by roads if city BB is reachable from city AA 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∗1052≦N≦2∗105
- 1≦K,L≦1051≦K,L≦105
- 1≦pi,qi,ri,si≦N1≦pi,qi,ri,si≦N
- pi<qipi<qi
- ri<siri<si
- When i≠ji≠j, (pi,qi)≠(pj,qj)(pi,qi)≠(pj,qj)
- When i≠ji≠j, (ri,si)≠(rj,sj)(ri,si)≠(rj,sj)
Input
The input is given from Standard Input in the following format:
NN KK LL p1p1 q1q1 : pKpK qKqK r1r1 s1s1 : rLrL sLsLOutput
Print NN integers. The ii-th of them should represent the number of the cities connected to the ii-th city by both roads and railways.
Sample Input 1 Copy
Copy
4 3 1 1 2 2 3 3 4 2 3Sample Output 1 Copy
Copy
1 2 2 1All 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,21,2,2 and 11, respectively.
Sample Input 2 Copy
Copy
4 2 2 1 2 2 3 1 4 2 3Sample Output 2 Copy
Copy
1 2 2 1Sample Input 3 Copy
Copy
7 4 4 1 2 2 3 2 5 6 7 3 5 4 5 3 4 6 7Sample Output 3 Copy
Copy
1 1 2 1 2 2 2
题意:
N个点,K条A型边,L条B型边,问每个点与多少点“相连”,
此处相连指既可以通过纯A型边相连,也可以通过纯B型边相连。
思路:
可以根据两种不同的边划分两种联通块(并查集,dfs都可以),
然后一个点如果和另一个点相通,那个他两个一定属于相同的联通块
(2种联通快均是),所以,对于每一个点,我们可以用两个联通快标
号组成的数对来代表该点,然后只需要输出每个点对应数对的value即
可,数对可以用map存储,数对作为key,对应点数为value。
代码实现:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=2e5+100;
const int M=4e5+100;
int head1[N],ver1[M],Next1[M];
int tot1,tot2;
int add1(int x,int y)
{
ver1[++tot1]=y;
Next1[tot1]=head1[x];
head1[x]=tot1;
}
int head2[N],ver2[M],Next2[M];
int add2(int x,int y)
{
ver2[++tot2]=y;
Next2[tot2]=head2[x];
head2[x]=tot2;
}
int cnt=0;
int c1[N],c2[N];
void dfs1(int x)
{
c1[x]=cnt;
for(int i=head1[x]; i; i=Next1[i])
{
int y=ver1[i];
if(c1[y])continue;
dfs1(y);
}
}
void dfs2(int x)
{
c2[x]=cnt;
for(int i=head2[x]; i; i=Next2[i])
{
int y=ver2[i];
if(c2[y])continue;
dfs2(y);
}
}
map<pair<int,int>,int>MM;
int main()
{
int n,k,l;
cin>>n>>k>>l;
for(int i=1; i<=k; i++)
{
int x,y;
cin>>x>>y;
add1(x,y);
add1(y,x);
}
for(int i=1; i<=l; i++)
{
int x,y;
cin>>x>>y;
add2(x,y);
add2(y,x);
}
for(int i=1; i<=n; i++)
{
if(c1[i]==0)
{
cnt++;
dfs1(i);
}
}
cnt=0;
for(int i=1; i<=n; i++)
{
if(c2[i]==0)
{
cnt++;
dfs2(i);
}
}
for(int i=1; i<=n; i++)
MM[make_pair(c1[i],c2[i])]++;
for(int i=1; i<=n; i++)
{
cout<<MM[make_pair(c1[i],c2[i])]<<" ";
if(i==n)cout<<endl;
}
return 0;
}