Problem:
Score : 400 points

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 p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-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*10^5

1 ≦ K, L≦ 10^5

1 ≦ p_i, q_i, r_i, s_i ≦ N

p_i < q_i

r_i < s_i

When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)

When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)

Input

The input is given from Standard Input in the following format:

N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L

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

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

    #include <iostream>
    #include <string>
    #include <set>  
    #include <algorithm>
    #include <vector>
    #include <cmath>
    #include <queue>
    #include <deque>
    #include <cstring>
    #include <cstdio>
    #include <map>
    #include <numeric>
	#include <cassert>
    #include <iomanip>
    #include <sstream>
using namespace std;
const int MAXN=2e5+10;
vector<int> G[2][MAXN];
int Fa[2][MAXN];
int n;
void Init()
{
	for(int k=0;k<2;k++)
		for(int i=0;i<n;i++)
		{
			G[k][i].clear();
			G[k][i].push_back(i);
			Fa[k][i]=i;
		}
}
int Getfa(int x,int z)
{
	return (Fa[z][x]==x?x:Fa[z][x]=Getfa(Fa[z][x],z));
}
void Union(int x,int y,int z)
{
	//cout<<0<<" "<<x<<" "<<y<<endl;
	//cout<<Fa[z][y]<<" ";
	x=Getfa(x,z);
	y=Getfa(y,z);
	//cout<<1<<" "<<x<<" "<<y<<endl;
	//system("pause");
	if(x==y)return;
	if(G[z][x].size()>G[z][y].size())
	{
		Fa[z][y]=x;
		for(int i=0;i<G[z][y].size();i++)
			G[z][x].push_back(G[z][y][i]);
		G[z][y].clear();
	}
	else
	{
		Fa[z][x]=y;
		for(int i=0;i<G[z][x].size();i++)
			G[z][y].push_back(G[z][x][i]);
		G[z][x].clear();
	}
}
int main()
{
	int kl[2];
	cin>>n>>kl[0]>>kl[1];
	Init();
	for(int tc=0;tc<2;tc++)
	{
		for(int i=0;i<kl[tc];i++)
		{
			int x,y;
			cin>>x>>y;
			x--,y--;
			Union(x,y,tc);
			//cout<<1<<endl;
		}
		//for(int i=0;i<n;i++)
		//{
			//cout<<i<<" "<<G[tc][i].size()<<endl;
		//}
		//system("pause");
	}
	for(int i=0;i<n;i++)
	{
		int ii=Getfa(i,0);
		int iii=Getfa(i,1);
		vector<int> v(G[0][ii]);
		v.insert(v.end(),G[1][iii].begin(),G[1][iii].end());
		sort(v.begin(),v.end());
		v.erase(unique(v.begin(),v.end()),v.end());
		int cnt=G[0][ii].size()+G[1][iii].size()-v.size();
		cout<<cnt<<" ";
		/*cur.clear();
		int cnt=0;
		int ii=Getfa(i,0);
		int iii=Getfa(i,1);
		if(ii<iii)
		{//cout<<i<<" "<<ii<<" "<<iii<<endl;
		//system("pause");
		for(int j=0;j<G[0][ii].size();j++)
			cur.insert(G[0][ii][j]);
		//cout<<cur.size()<<endl;
		//system("pause");
		//system("pause");
		for(int j=0;j<G[1][iii].size();j++)
		{
		int a=G[1][iii][j];
		if(cur.find(a)!=cur.end())cnt++;
	}
		}
		else
		{
		for(int j=0;j<G[1][iii].size();j++)
			cur.insert(G[1][iii][j]);
		//cout<<cur.size()<<endl;
		//system("pause");
		//system("pause");
		for(int j=0;j<G[0][ii].size();j++)
		{
			int a=G[0][ii][j];
			if(cur.find(a)!=cur.end())cnt++;	
		}*/
	}
		//cout<<cnt<<" ";
	cout<<endl;
	return 0;
}