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;
class Program
{
    static void Main(string[] args)
    {
            // 入力
            string[] sS = Console.ReadLine().Trim().Split(' ');
            int iN = int.Parse(sS[0]);
            int iK = int.Parse(sS[1]);
            int iL = int.Parse(sS[2]);
            List<ClsAtoB> lstD = new List<ClsAtoB>();
            List<ClsAtoB> lstT = new List<ClsAtoB>();
            for (int i = 0; i < iK; i++)
            {
                sS = Console.ReadLine().Trim().Split(' ');
                int ia = int.Parse(sS[0]);
                int ib = int.Parse(sS[1]);
                lstD.Add(new ClsAtoB() { iA = ia, iB = ib, });          // 道路の関係
                //lstD.Add(new ClsAtoB() { iA = ib, iB = ia, });
            }
            for (int i = 0; i < iL; i++)
            {
                sS = Console.ReadLine().Trim().Split(' ');
                int ia = int.Parse(sS[0]);
                int ib = int.Parse(sS[1]);
                lstT.Add(new ClsAtoB() { iA = ia, iB = ib, });          // 鉄道の関係
                //lstT.Add(new ClsAtoB() { iA = ib, iB = ia, });
            }

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            // 準備
            //if (lstD.Count > 1) { lstD.Sort(delegate (ClsAtoB mca1, ClsAtoB mca2) { return (int)(mca1.iA - mca2.iA) == 0 ? (mca1.iB - mca2.iB) : (mca1.iA - mca2.iA); }); }     // iAの昇順 の iBの昇順
            //if (lstT.Count > 1) { lstT.Sort(delegate (ClsAtoB mca1, ClsAtoB mca2) { return (int)(mca1.iA - mca2.iA) == 0 ? (mca1.iB - mca2.iB) : (mca1.iA - mca2.iA); }); }     // iAの昇順 の iBの昇順
            List<List<int>> lstDG = new List<List<int>>();              // 道路の繋がっているグループ
            List<List<int>> lstTG = new List<List<int>>();              // 鉄道の繋がっているグループ
            for (int i = 0; i < lstD.Count; i++)
            {
                Boolean bEx = false;
                for (int j = 0; j < lstDG.Count; j++)
                {
                    if (lstDG[j].Contains(lstD[i].iA) && lstDG[j].Contains(lstD[i].iB)) { bEx = true; break; }
                    else if (lstDG[j].Contains(lstD[i].iA) || lstDG[j].Contains(lstD[i].iB))
                    {
                        if (lstDG[j].Contains(lstD[i].iA)) { lstDG[j].Add(lstD[i].iB); }
                        else if (lstDG[j].Contains(lstD[i].iB)) { lstDG[j].Add(lstD[i].iA); }
                        bEx = true;
                        break;
                    }
                }
                if (!bEx)
                {
                    List<int> oneUsr = new List<int>();
                    oneUsr.Add(lstD[i].iA);
                    oneUsr.Add(lstD[i].iB);
                    lstDG.Add(oneUsr);
                }
            }
            for (int i = 0; i < lstT.Count; i++)
            {
                Boolean bEx = false;
                for (int j = 0; j < lstTG.Count; j++)
                {
                    if (lstTG[j].Contains(lstT[i].iA) && lstTG[j].Contains(lstT[i].iB)) { bEx = true; break; }
                    else if (lstTG[j].Contains(lstT[i].iA) || lstTG[j].Contains(lstT[i].iB))
                    {
                        if (lstTG[j].Contains(lstT[i].iA)) { lstTG[j].Add(lstT[i].iB); }
                        else if (lstTG[j].Contains(lstT[i].iB)) { lstTG[j].Add(lstT[i].iA); }
                        bEx = true;
                        break;
                    }
                }
                if (!bEx)
                {
                    List<int> oneUsr = new List<int>();
                    oneUsr.Add(lstT[i].iA);
                    oneUsr.Add(lstT[i].iB);
                    lstTG.Add(oneUsr);
                }
            }

            // 解析
            List<int> lstRet = new List<int>();
            for (int i = 1; i <= iN; i++)
            {
                List<int> lstTmp = new List<int>();
                for (int j = 0; j < lstDG.Count; j++)
                {
                    if (lstDG[j].Contains(i))
                    {
                        for (int k = 0; k < lstDG[j].Count; k++)
                        {
                            if (lstDG[j][k] != i) { lstTmp.Add(lstDG[j][k]); }                  // 道路で繋がっている都市
                            //lstTmp.Add(lstDG[j][k]);
                        }
                        break;
                    }
                }
                if (lstTmp.Count == 0) { lstRet.Add(1); continue; }
                List<int> lstTm2 = new List<int>();
                for (int j = 0; j < lstTG.Count; j++)
                {
                    if (lstTG[j].Contains(i))
                    {
                        for (int k = 0; k < lstTG[j].Count; k++)
                        {
                            //if (lstTG[j][k] != i && lstTmp.Contains(i)) { lstTm2.Add(lstTG[j][k]); }
                            if (lstTmp.Contains(lstTG[j][k])) { lstTm2.Add(lstTG[j][k]); }      // 道路と鉄道で繋がっている都市
                        }
                        break;
                    }
                }
                if (lstTm2.Count == 0) { lstRet.Add(1); continue; }
                lstRet.Add(lstTm2.Count + 1);                                                   // 自分も+1
            }

            //出力
            string sOut = string.Join(" ", lstRet.ToArray());
            Console.WriteLine("{0}", sOut.ToString().Trim());

            sw.Stop();
#if DEBUG
            Console.WriteLine();
            Console.WriteLine("{0}", sw.Elapsed.ToString());
            Console.WriteLine();
            for (int i = 0; i < lstD.Count; i++)
            {
                sOut = lstD[i].iA.ToString() + " " + lstD[i].iB.ToString();
                Console.WriteLine("lstD__{0}={1}", i.ToString().Trim(), sOut.ToString().Trim());
            }
            for (int i = 0; i < lstDG.Count; i++)
            {
                sOut = string.Join(" ", lstDG[i].ToArray());
                Console.WriteLine("lstDG_{0}={1}", i.ToString().Trim(), sOut.ToString().Trim());
            }
            for (int i = 0; i < lstT.Count; i++)
            {
                sOut = lstT[i].iA.ToString() + " " + lstT[i].iB.ToString();
                Console.WriteLine("lstT__{0}={1}", i.ToString().Trim(), sOut.ToString().Trim());
            }
            for (int i = 0; i < lstTG.Count; i++)
            {
                sOut = string.Join(" ", lstTG[i].ToArray());
                Console.WriteLine("lstTG_{0}={1}", i.ToString().Trim(), sOut.ToString().Trim());
            }
            Console.WriteLine("続行するには何かキーを押してください．．．");
            Console.ReadKey();
#endif
    }
        private class ClsAtoB
        {
            public int iA { get; set; } = 0;
            public int iB { get; set; } = 0;
        }
}