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: Accepted)

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

namespace ABC049D
{
    class Program
    {
        public static void Solve(Input input)
        {
            var n = input.NextInt();
            var k = input.NextInt();
            var l = input.NextInt();
            var road = new UnionFind(n);
            var rail = new UnionFind(n);

            for (int i = 0; i < k; i++)
            {
                var p = input.NextInt() - 1;
                var q = input.NextInt() - 1;

                road.Merge(p, q);
            }

            for (int i = 0; i < l; i++)
            {
                var r = input.NextInt() - 1;
                var s = input.NextInt() - 1;

                rail.Merge(r, s);
            }

            var map = new Dictionary<Tuple<int, int>, int>();

            for (int i = 0; i < n; i++)
            {
                var x = road.FindRoot(i);
                var y = rail.FindRoot(i);
                var t = new Tuple<int, int>(x, y);

                if (!map.ContainsKey(t))
                {
                    map.Add(t, 0);
                }
                map[t]++;
            }

            var count = new int[n];
            for (int i = 0; i < n; i++)
            {
                var x = road.FindRoot(i);
                var y = rail.FindRoot(i);
                var t = new Tuple<int, int>(x, y);

                count[i] = map[t];
            }

            Console.WriteLine(string.Join(" ", count));
        }

        /// <summary>
        /// 重み付きUF木
        /// </summary>
        class UnionFind
        {
            int[] Rank { get; set; }
            int[] Size { get; set; }
            int[] ParentId { get; set; }
            int[] DiffWeight { get; set; }

            /// <summary>
            /// 
            /// </summary>
            /// <param name="size">ノードの総数</param>
            public UnionFind(int size)
            {
                this.Rank = new int[size];
                this.Size = Enumerable.Repeat(1, size).ToArray();
                this.ParentId = Enumerable.Range(0, size).ToArray();
                this.DiffWeight = new int[size];
            }

            /// <summary>
            /// 同一の親を持つか
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <returns></returns>
            public bool Same(int x, int y)
            {
                return FindRoot(x) == FindRoot(y);
            }

            /// <summary>
            /// 指定ノードのrootを返す
            /// </summary>
            /// <param name="x"></param>
            /// <returns></returns>
            public int FindRoot(int x)
            {
                if (x != this.ParentId[x])
                {
                    // 経路中のノードのパラメータを更新しておく
                    var root = FindRoot(this.ParentId[x]);
                    this.DiffWeight[x] += this.DiffWeight[this.ParentId[x]];
                    this.ParentId[x] = root;
                }

                return this.ParentId[x];
            }

            /// <summary>
            /// 親経路との重さを返す
            /// </summary>
            /// <param name="x"></param>
            /// <returns></returns>
            public int Weight(int x)
            {
                FindRoot(x);

                return this.DiffWeight[x];
            }

            /// <summary>
            /// x,yが同一の親を持つ場合、Weightの差分（y-x）を返す
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <returns></returns>
            public int Weight(int x, int y)
            {
                return Weight(y) - Weight(x);
            }

            /// <summary>
            /// 重みをつけて合体する
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <param name="w"></param>
            public void Merge(int x, int y, int w = 0)
            {
                w += Weight(x) - Weight(y);

                x = FindRoot(x);
                y = FindRoot(y);

                if (x == y) return;

                if (this.Rank[x] < this.Rank[y])
                {
                    this.ParentId[x] = y;
                    this.Size[y] += this.Size[x];
                    this.DiffWeight[x] = -w;
                }
                else
                {
                    this.ParentId[y] = x;
                    this.Size[x] += this.Size[y];
                    this.DiffWeight[y] = w;

                    if (this.Rank[x] == this.Rank[y])
                    {
                        this.Rank[x]++;
                    }
                }
            }

            /// <summary>
            /// xと同じ親を持つノードの数
            /// </summary>
            /// <param name="x"></param>
            /// <returns></returns>
            public int SameCount(int x)
            {
                x = FindRoot(x);

                return this.Size[x];
            }
        }

        #region Main
        public static void Main(string[] args)
        {
            // 出力が少ないときはこれをセットする方が時間かかるけれど
            // その場合は出力がボトルネックになっていないので、まあ良しとする
            var needsFlushOutput = true;
            if (needsFlushOutput)
            {
                // 細かく出力しないようにする
                var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);
            }

            // 通常は引数無しで、ファイルから読み出したい場合はpath指定する
            var input = new Input();
            // 仮想的に標準入力をセットする
            //input.SetText("");

            Solve(input);

            Console.Out.Flush();
        }
        #endregion

        #region Competitive Template
#pragma warning disable CS0414
        static readonly int MOD = (int)1e9 + 7;
        static readonly int[] dx = { 1, 0, -1, 0 };
        static readonly int[] dy = { 0, 1, 0, -1 };
        /// <summary> 左上が原点 </summary>
        static readonly char[] dir = { 'R', 'U', 'L', 'D' };
#pragma warning restore CS0414

        /// <summary>
        /// 値を書き換え可能なTuple（ぐらいのイメージ）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        public class Pair<T, U> : IComparable<Pair<T, U>>, IEquatable<Pair<T, U>>
            where T : IComparable<T>
            where U : IComparable<U>
        {
            public T V1 { get; set; }
            public U V2 { get; set; }

            public Pair(T v1, U v2)
            {
                this.V1 = v1;
                this.V2 = v2;
            }

            public Pair(Pair<T, U> src)
            {
                this.V1 = src.V1;
                this.V2 = src.V2;
            }

            public int CompareTo(Pair<T, U> other)
            {
                var r = this.V1.CompareTo(other.V1);
                if (r == 0) r = this.V2.CompareTo(other.V2);

                return r;
            }

            public bool Equals(Pair<T, U> other)
            {
                return other != null && this.CompareTo(other) == 0;
            }

            public override string ToString()
            {
                return $"{V1} {V2}";
            }

            public override bool Equals(object obj)
            {
                return Equals(obj as Pair<T, U>);
            }

            public override int GetHashCode()
            {
                var hashCode = 1989511945;
                hashCode = hashCode * -1521134295 + V1.GetHashCode();
                hashCode = hashCode * -1521134295 + V2.GetHashCode();
                return hashCode;
            }

            public static bool operator ==(Pair<T, U> a, Pair<T, U> b)
            {
                if (object.ReferenceEquals(a, null))
                {
                    return object.ReferenceEquals(b, null);
                }

                return a.Equals(b);
            }

            public static bool operator !=(Pair<T, U> a, Pair<T, U> b)
                => !(a == b);
            public static bool operator <(Pair<T, U> a, Pair<T, U> b)
                => a.CompareTo(b) < 0;
            public static bool operator <=(Pair<T, U> a, Pair<T, U> b)
                => a.CompareTo(b) <= 0;
            public static bool operator >(Pair<T, U> a, Pair<T, U> b)
                => a.CompareTo(b) > 0;
            public static bool operator >=(Pair<T, U> a, Pair<T, U> b)
                => a.CompareTo(b) >= 0;
        }

        public class Input
        {
            // 変な改行コードがたまに混じっているので、一応セパレート指定する
            // スペース単独指定の方がもちろん早い
            static readonly char[] separator = { ' ', '\r', '\n' };
            Queue<string> q { get; set; }
            StreamReader sr;

            /// <summary>
            /// 特定のファイルから読み出したい場合はpath指定する
            /// </summary>
            /// <param name="path"></param>
            public Input(string path = "")
            {
                q = new Queue<string>();

                if (string.IsNullOrEmpty(path))
                {
                    sr = new StreamReader(Console.OpenStandardInput());
                }
                else
                {
                    sr = new StreamReader(path);
                }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            /// <param name="items"></param>
            public void SetText(IEnumerable<string> items)
            {
                foreach (var item in items)
                {
                    SetText(item);
                }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public bool SetText(string s)
            {
                if (s == null) return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                {
                    q.Enqueue(elem);
                }

                return true;
            }

            /// <summary>
            /// 要素が存在するか
            /// </summary>
            /// <returns></returns>
            public bool Any()
                => q.Any() || read();


            /// <summary>
            /// 内部queueに入力からの値をsplitして格納する
            /// </summary>
            /// <returns></returns>
            bool read()
            {
                var s = sr.ReadLine();
                if (s == null) return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                {
                    q.Enqueue(elem);
                }

                if (!q.Any()) return read();

                return true;
            }

            /// <summary>
            /// 次のstringを一つ読み込む
            /// </summary>
            /// <returns></returns>
            public string Next()
            {
                if (!q.Any())
                {
                    if (!read())
                    {
                        return "";
                    }
                }

                return q.Dequeue();
            }

            /// <summary>
            /// 指定個数queueにたまるまでenqueueし続ける
            /// </summary>
            /// <param name="n"></param>
            /// <returns></returns>
            bool accumulate(int n)
            {
                while (q.Count() < n)
                {
                    if (!read()) return false;
                }

                return true;
            }

            public int NextInt() => int.Parse(Next());
            public long NextLong() => long.Parse(Next());
            public double NextDouble() => double.Parse(Next());

            /// <summary>
            /// n個の要素をparseして、それぞれにoffsetをaddした配列を返す
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="n"></param>
            /// <param name="offset"></param>
            /// <param name="parse"></param>
            /// <param name="add"></param>
            /// <returns></returns>
            T[] NextT<T>(int n, T offset, Func<string, T> parse, Func<T, T, T> add)
            {
                if (!accumulate(n)) return null;

                var a = new T[n];

                for (int i = 0; i < n; i++)
                    a[i] = add(parse(q.Dequeue()), offset);

                return a;
            }

            public string[] Next(int n) => NextT(n, "", x => x, (x, y) => x);
            public int[] NextInt(int n, int offset = 0) => NextT(n, offset, int.Parse, (x, y) => x + y);
            public long[] NextLong(int n, long offset = 0) => NextT(n, offset, long.Parse, (x, y) => x + y);
            public double[] NextDouble(int n, double offset = 0.0) => NextT(n, offset, double.Parse, (x, y) => x + y);

            /// <summary>
            /// n個の要素をparseして、それぞれにoffsetをaddした配列を返す
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="n"></param>
            /// <param name="offset"></param>
            /// <param name="parse"></param>
            /// <param name="add"></param>
            /// <returns></returns>
            void NextT<T>(T[] a, T[] b, T offset, Func<string, T> parse, Func<T, T, T> add)
            {
                if (a.Length != b.Length) return;
                if (!accumulate(a.Length * 2)) return;

                for (int i = 0; i < a.Length; i++)
                {
                    a[i] = add(parse(q.Dequeue()), offset);
                    b[i] = add(parse(q.Dequeue()), offset);
                }
            }

            public void Next(string[] a, string[] b) => NextT(a, b, "", x => x, (x, y) => x);
            public void NextInt(int[] a, int[] b, int offset = 0) => NextT(a, b, offset, int.Parse, (x, y) => x + y);
            public void NextLong(long[] a, long[] b, long offset = 0) => NextT(a, b, offset, long.Parse, (x, y) => x + y);
            public void NextDouble(double[] a, double[] b, double offset = 0.0) => NextT(a, b, offset, double.Parse, (x, y) => x + y);
        }

        static class Utils
        {
            public static void Print<T>(IEnumerable<T> ie, string sep = " ") => Console.WriteLine(string.Join(sep, ie));
            public static void Print<T>(params T[] objs) => Print(objs);
            public static T Max<T>(params T[] objs) => objs.Max();
            public static T Min<T>(params T[] objs) => objs.Min();

            public static void Swap<T>(ref T x, ref T y) where T : struct { var t = x; x = y; y = t; }
            public static void Swap<T>(IList<T> list, int i, int j) where T : struct { var t = list[i]; list[i] = list[j]; list[j] = t; }

            /// <summary>
            /// vでfillされたT[d1][d2]配列を作成する
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="d1"></param>
            /// <param name="d2"></param>
            /// <param name="v"></param>
            /// <returns></returns>
            public static T[][] Create2DArray<T>(int d1, int d2, T v = default(T))
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(v, d2).ToArray()).ToArray();
            }

            /// <summary>
            /// vでfillされたT[d1][d2][d3]配列を作成する
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="d1"></param>
            /// <param name="d2"></param>
            /// <param name="d3"></param>
            /// <param name="v"></param>
            /// <returns></returns>
            public static T[][][] Create3DArray<T>(int d1, int d2, int d3, T v = default(T))
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(0, d2).Select(__ =>
                     Enumerable.Repeat(v, d3).ToArray()).ToArray()).ToArray();
            }
        }
        #endregion
    }
}