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 Ruby (2.3.3) (judge verdict: Time Limit Exceeded)

# coding: utf-8
#
# :u ノードのインデックス
# :k u の出次数
# :v u に隣接する頂点の番号
# :w 隣接する頂点への重み
#
INF  = Float::INFINITY
Node = Struct.new(:u, :k, :v, :w)

class Graph
  attr :nodes, :dist
  def initialize(n)
    @nodes = Array.new(n){ Node.new }
    @nodes = @nodes.map.with_index do |node,idx|
      node.u = idx
      node
    end
    @dist = Array.new(n).map{Array.new(n, 0)}
  end

  def add_graph_edge(u, v, w)
    #puts "#{u} -> #{v} (#{w})"
    if @nodes[u].v.nil?
      @nodes[u].v = [v]
      @nodes[u].w = {v=>w}
    elsif @nodes[u].v.is_a?(Array)
      @nodes[u].v << v
      @nodes[u].w = {v=>w}.merge(@nodes[u].w)
    else
      @nodes[u].v = [@nodes[u].v, v]
      @nodes[u].w = {v=>w}.merge(@nodes[u].w)
    end
    @nodes[u].k = if @nodes[u].v.is_a?(Array)
                    @nodes[u].v.length
                  else
                    1
                  end
  end

  def warshall_floyd()
    # initialize
    for i in 0...@nodes.length
      for j in 0...@nodes.length
        @dist[i][j] = if not @nodes[i].w.nil? and @nodes[i].w.has_key?(j)
                        @nodes[i].w[j]
                      else
                        INF
                      end
      end
      @dist[i][i] = 0
    end

    # calc
    for k in 0...@nodes.length
      for i in 0...@nodes.length
        for j in 0...@nodes.length
          if @dist[i][k] != INF and @dist[k][j] != INF
            @dist[i][j] = [@dist[i][j], @dist[i][k] + @dist[k][j]].min
          end
        end
      end
    end
  end

  def has_negative_cycle?()
    ans = false
    for v in 0...V
      ans = true if @dist[v][v] < 0
    end
    ans
  end
end

lines = $stdin.read
array = lines.split("\n")
N,K,L = array[0].split(" ").map(&:to_i)

pq,rs = [],[]

array[1..K].each do |str|
  pi,qi = str.split(" ").map(&:to_i)
  key,val = [pi,qi].min,[pi,qi].max
  pq << [key,val]
end

array[K+1..K+L+1].each do |str|
  ri,si = str.split(" ").map(&:to_i)
  key,val = [ri,si].min,[ri,si].max
  rs << [key,val]
end

graph_pq = Graph.new(N)
graph_rs = Graph.new(N)

pq.each do |arr|
  u,v = arr.first-1,arr.last-1
  graph_pq.add_graph_edge(u, v, 1)
  graph_pq.add_graph_edge(v, u, 1)
end
rs.each do |arr|
  u,v = arr.first-1,arr.last-1
  graph_rs.add_graph_edge(u, v, 1)
  graph_rs.add_graph_edge(v, u, 1)
end

graph_pq.warshall_floyd()
graph_rs.warshall_floyd()

# graph_pq.dist.each{|d| p d}
# puts "---"
# graph_rs.dist.each{|d| p d}

ans1 = graph_pq.dist.map do |d|
  d.count{|d| d != INF}
end
ans2 = graph_rs.dist.map do |d|
  d.count{|d| d != INF}
end

ans = (0...N).map do |n|
  diff = (0...N).map do |c|
    l = graph_pq.dist[n][c]
    r = graph_rs.dist[n][c]
    if l==INF or r==INF
      0
    else
      1
    end
  end
  #p diff
  diff.inject(&:+)
end

puts ans.join(" ")
#puts "1 1 2 1 2 2 2"