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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

using Pair = System.Collections.Generic.KeyValuePair<int, int>;

class Program
{
	public Program() { }

	static void Main(string[] args)
	{
		new Program().prog();
	}
	Scanner cin;
	const int MOD = 1000000007;
	const int INF = int.MaxValue - 10;
	const long INFL = long.MaxValue - 10;
	const double EPS = 1e-8;
	const double PI = 3.1415926536;
	
	void prog()
	{
		cin = new Scanner();
		int[,] dir8 = new int[8, 2] { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 }, { 1, 1 } };
		int[,] dir4 = new int[4, 2] { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } };
		/*
		StreamReader sr = new StreamReader(@"D:\Download\2017-pr-t1-in5.txt");
		StreamWriter sw = new StreamWriter(@"D:\prog\joi\1-5.txt");
		string[] line = sr.ReadLine().Split(' ');
		sr.Close();
		sw.Close();
		*/
		int N = cin.nextInt();
		int K = cin.nextInt();
		int L = cin.nextInt();
		UnionFind D = new UnionFind(N);
		UnionFind T = new UnionFind(N);
		for (int i = 0; i < K; i++)
		{
			int p = cin.nextInt() - 1;
			int q = cin.nextInt() - 1;
			D.unite(p, q);
		}
		for (int i = 0; i < L; i++)
		{
			int p = cin.nextInt() - 1;
			int q = cin.nextInt() - 1;
			T.unite(p, q);
		}
		D.makeSet();
		T.makeSet();

		int[] ans = new int[N];

		for (int i = 0; i < N; i++)
		{
			if (ans[i] > 0) continue;
			int j = 0, k = 0;
			int d = D.setnum[i];
			int t = T.setnum[i];
			List<int> share = new List<int>();
			while (j < D.set[d].Count && k < T.set[t].Count)
			{
				if (D.set[d][j] == T.set[t][k])
				{
					share.Add(D.set[d][j]);
					j++;
					k++;
				}else if(D.set[d][j] > T.set[t][k])
				{
					k++;
				}
				else
				{
					j++;
				}
			}
			for (int l = 0; l < share.Count; l++)
			{
				ans[share[l]] = share.Count;
			}
		}
		
		for (int i = 0; i < N; i++)
		{
			Console.Write(ans[i]);
			if (i != N - 1) Console.Write(' ');
		}
		
		Console.WriteLine();
	}
	class UnionFind
	{
		int N;
		int[] parent;
		int[] rank;
		public List<int>[] set;
		public int[] setnum;

		public void makeSet()
		{
			List<int> par = new List<int>();
			setnum = new int[N];
			int[] numset = new int[N];
			for (int i = 0; i < N; i++)
			{
				int p = find(i);
				if (!par.Contains(p))
				{
					setnum[i] = par.Count;
					numset[p] = i;
					par.Add(p);
				}
				else
				{
					setnum[i] = setnum[numset[p]];
				}
			}
			set = new List<int>[par.Count];
			for (int i = 0; i < par.Count; i++)
			{
				set[i] = new List<int>();
			}
			for (int i = 0; i < N; i++)
			{
				set[setnum[i]].Add(i);
			}
		}

		public UnionFind(int N)
		{
			this.N = N;
			parent = new int[N];
			rank = new int[N];
			for (int i = 0; i < N; i++)
			{
				parent[i] = i;
			}
		}

		public int find(int x)
		{
			if (parent[x] == x)
			{
				return x;
			}
			else
			{
				return parent[x] = find(parent[x]);
			}
		}

		public void unite(int x, int y)
		{
			x = find(x);
			y = find(y);
			if (rank[x] < rank[y])
			{
				parent[x] = y;
			}
			else
			{
				parent[y] = x;
				if (rank[x] == rank[y]) rank[x]++;
			}
		}

		public bool same(int x, int y)
		{
			return find(x) == find(y);
		}
	}
}

class Scanner
{
	string[] s;
	int i;

	char[] cs = new char[] { ' ' };

	public Scanner()
	{
		s = new string[0];
		i = 0;
	}

	public string next()
	{
		if (i < s.Length) return s[i++];
		string st = Console.ReadLine();
		while (st == "") st = Console.ReadLine();
		s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
		i = 0;
		return next();
	}

	public int nextInt()
	{
		return int.Parse(next());
	}

	public long nextLong()
	{
		return long.Parse(next());
	}

	public double nextDouble()
	{
		return double.Parse(next());
	}
}