E - Ideal Sheet

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は黒いマスと透明なマスからなるシート A,B1 枚ずつと、透明なマスのみからなる無限に広がるシート C を持っています。
また、高橋君には黒いマスと透明なマスからなる、理想とするシート X が存在します。

シート A,B,X の大きさはそれぞれ縦 H_A マス \timesW_A マス、縦 H_B マス \timesW_B マス、縦 H_X マス \timesW_X マスです。
シート A の各マスは .# からなる長さ W_A の文字列 H_AA_1,A_2,\ldots,A_{H_A} によって表され、
A_i (1\leq i\leq H_A)j 文字目 (1\leq j\leq W_A) が、 . のときシート A の上から i 行目かつ左から j 列目のマスは透明なマスであり、 # のとき黒いマスです。
シート B,X の各マスも、同様に長さ W_B の文字列 H_BB_1,B_2,\ldots,B_{H_B} および長さ W_X の文字列 H_XX_1,X_2,\ldots,X_{H_X} によって表されます。

高橋君の目標は、次の手順で、シート A,B,C から、A,B に存在する すべての黒いマスを使って シート X を作り出すことです。

  1. シート A,B をマス目に沿ってシート C に貼り付ける。この時、シート A,B はそれぞれ好きな場所に平行移動させて貼って良いが、シートを切り分けたり、回転させたりしてはいけない。
  2. シート C からマス目に沿って H_X\times W_X マスの領域を切り出す。ここで、切り出されたシートの各マスは、シート A または B の黒いマスが貼り付けられていれば黒いマスに、そうでなければ透明なマスとなる。

このとき、貼り付ける位置と切り出す領域をうまくとることで高橋君は目標を達成できるか、すなわち次の条件をともにみたすことにできるか判定してください。

  • 切り出されたシートはシート A,B黒いマスをすべて 含む。切り出されたシートの上でシート A,B の黒いマスどうしが重なって存在していても構わない。
  • 切り出されたシートは、回転させたり裏返したりすることなくシート X と一致する。

制約

  • 1\leq H_A,W_A,H_B,W_B,H_X,W_X\leq 10
  • H_A,W_A,H_B,W_B,H_X,W_X は整数
  • A_i.# のみからなる長さ W_A の文字列
  • B_i.# のみからなる長さ W_B の文字列
  • X_i.# のみからなる長さ W_X の文字列
  • シート A,B,X はそれぞれ少なくとも 1 つ以上の黒いマスを含む。

入力

入力は以下の形式で標準入力から与えられる。

H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}

出力

高橋君が問題文中の目標を達成できるならば Yes を、できないならば No を出力せよ。


入力例 1

3 5
#.#..
.....
.#...
2 2
#.
.#
5 3
...
#.#
.#.
.#.
...

出力例 1

Yes

まず、シート A をシート C に貼り付けると下図のようになります。

     \vdots
  .......  
  .#.#...  
\cdots.......\cdots
  ..#....  
  .......  
     \vdots

さらに、シート B をシート A と左上を合わせて貼ってみると下図のようになります。

     \vdots
  .......  
  .#.#...  
\cdots..#....\cdots
  ..#....  
  .......  
     \vdots

ここで、上で具体的に図示されている範囲のうち、上から 1 行目かつ左から 2 列目のマスを左上として 5\times 3 マスを切り出すと下図のようになります。

...
#.#
.#.
.#.
...

これはシート A,B のすべての黒いマスを含んでおり、また、シート X と一致しているため条件を満たしています。

よって、Yes を出力します。


入力例 2

2 2
#.
.#
2 2
#.
.#
2 2
##
##

出力例 2

No

シート AB を回転させて貼ってはいけないことに注意してください。


入力例 3

1 1
#
1 2
##
1 1
#

出力例 3

No

どのように貼ったり切り出したりしても、シート B の黒いマスをすべて含むように切り出すことはできないため、1 つめの条件をみたすことができません。 よって、No を出力します。


入力例 4

3 3
###
...
...
3 3
#..
#..
#..
3 3
..#
..#
###

出力例 4

Yes

Score : 300 points

Problem Statement

Takahashi has two sheets A and B, each composed of black squares and transparent squares, and an infinitely large sheet C composed of transparent squares.
There is also an ideal sheet X for Takahashi composed of black squares and transparent squares.

The sizes of sheets A, B, and X are H_A rows \times W_A columns, H_B rows \times W_B columns, and H_X rows \times W_X columns, respectively.
The squares of sheet A are represented by H_A strings of length W_A, A_1, A_2, \ldots, A_{H_A} consisting of . and #.
If the j-th character (1\leq j\leq W_A) of A_i (1\leq i\leq H_A) is ., the square at the i-th row from the top and j-th column from the left is transparent; if it is #, that square is black.
Similarly, the squares of sheets B and X are represented by H_B strings of length W_B, B_1, B_2, \ldots, B_{H_B}, and H_X strings of length W_X, X_1, X_2, \ldots, X_{H_X}, respectively.

Takahashi's goal is to create sheet X using all black squares in sheets A and B by following the steps below with sheets A, B, and C.

  1. Paste sheets A and B onto sheet C along the grid. Each sheet can be pasted anywhere by translating it, but it cannot be cut or rotated.
  2. Cut out an H_X\times W_X area from sheet C along the grid. Here, a square of the cut-out sheet will be black if a black square of sheet A or B is pasted there, and transparent otherwise.

Determine whether Takahashi can achieve his goal by appropriately choosing the positions where the sheets are pasted and the area to cut out, that is, whether he can satisfy both of the following conditions.

  • The cut-out sheet includes all black squares of sheets A and B. The black squares of sheets A and B may overlap on the cut-out sheet.
  • The cut-out sheet coincides sheet X without rotating or flipping.

Constraints

  • 1\leq H_A, W_A, H_B, W_B, H_X, W_X\leq 10
  • H_A, W_A, H_B, W_B, H_X, W_X are integers.
  • A_i is a string of length W_A consisting of . and #.
  • B_i is a string of length W_B consisting of . and #.
  • X_i is a string of length W_X consisting of . and #.
  • Sheets A, B, and X each contain at least one black square.

Input

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

H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}

Output

If Takahashi can achieve the goal described in the problem statement, print Yes; otherwise, print No.


Sample Input 1

3 5
#.#..
.....
.#...
2 2
#.
.#
5 3
...
#.#
.#.
.#.
...

Sample Output 1

Yes

First, paste sheet A onto sheet C, as shown in the figure below.

     \vdots
  .......  
  .#.#...  
\cdots.......\cdots
  ..#....  
  .......  
     \vdots

Next, paste sheet B so that its top-left corner aligns with that of sheet A, as shown in the figure below.

     \vdots
  .......  
  .#.#...  
\cdots..#....\cdots
  ..#....  
  .......  
     \vdots

Now, cut out a 5\times 3 area with the square in the first row and second column of the range illustrated above as the top-left corner, as shown in the figure below.

...
#.#
.#.
.#.
...

This includes all black squares of sheets A and B and matches sheet X, satisfying the conditions.

Therefore, print Yes.


Sample Input 2

2 2
#.
.#
2 2
#.
.#
2 2
##
##

Sample Output 2

No

Note that sheets A and B may not be rotated or flipped when pasting them.


Sample Input 3

1 1
#
1 2
##
1 1
#

Sample Output 3

No

No matter how you paste or cut, you cannot cut out a sheet that includes all black squares of sheet B, so you cannot satisfy the first condition. Therefore, print No.


Sample Input 4

3 3
###
...
...
3 3
#..
#..
#..
3 3
..#
..#
###

Sample Output 4

Yes
F - Neo-lexicographic Ordering

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoder 王国を治める高橋君は、英小文字のアルファベット順を変更することにしました。

新たなアルファベット順はa , b , \ldots, z を並べ替えて得られる文字列 X を用いて表されます。Xi \, (1 \leq i \leq 26) 文字目は、新たな順番において i 番目に小さい英小文字を表します。

AtCoder 王国には N 人の国民がおり、それぞれの国民の名前は S_1, S_2, \dots, S_N です。ここで、S_i \, (1 \leq i \leq N) は英小文字からなります。
これらの名前を、高橋君の定めたアルファベット順に基づく辞書順に従って並べ替えてください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、S_jT_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、ST の長さを比較して、ST より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

制約

  • Xa , b , \ldots, z を並べ替えて得られる
  • 2 \leq N \leq 50000
  • N は整数
  • 1 \leq |S_i| \leq 10 \, (1 \leq i \leq N)
  • S_i は英小文字からなる (1 \leq i \leq N)
  • S_i \neq S_j (1 \leq i \lt j \leq N)

入力

入力は以下の形式で標準入力から与えられる。

X
N
S_1
S_2
\vdots
S_N

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、高橋君の定めたアルファベット順に基づく辞書順に従って国民の名前を並べ替えたとき、i 番目に小さいものを出力すること。


入力例 1

bacdefghijklmnopqrstuvwxzy
4
abx
bzz
bzy
caa

出力例 1

bzz
bzy
abx
caa

高橋君が新たに設定したアルファベット順において、ba より小さく、zy より小さいです。そのため、国民の名前を辞書順に並べ替えると、小さい順に bzz , bzy , abx , caa となります。


入力例 2

zyxwvutsrqponmlkjihgfedcba
5
a
ab
abc
ac
b

出力例 2

b
a
ac
ab
abc

Score : 300 points

Problem Statement

Takahashi, who governs the Kingdom of AtCoder, has decided to change the alphabetical order of English lowercase letters.

The new alphabetical order is represented by a string X, which is a permutation of a, b, \ldots, z. The i-th character of X (1 \leq i \leq 26) would be the i-th smallest English lowercase letter in the new order.

The kingdom has N citizens, whose names are S_1, S_2, \dots, S_N, where each S_i (1 \leq i \leq N) consists of lowercase English letters.
Sort these names lexicographically according to the alphabetical order decided by Takahashi.

What is the lexicographical order?

Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.

Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.

  1. Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
  3. If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.

Constraints

  • X is a permutation of a, b, \ldots, z.
  • 2 \leq N \leq 50000
  • N is an integer.
  • 1 \leq |S_i| \leq 10 \, (1 \leq i \leq N)
  • S_i consists of lowercase English letters. (1 \leq i \leq N)
  • S_i \neq S_j (1 \leq i \lt j \leq N)

Input

Input is given from Standard Input in the following format:

X
N
S_1
S_2
\vdots
S_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the i-th smallest name when the citizens' names are sorted according to the alphabetical order decided by Takahashi.


Sample Input 1

bacdefghijklmnopqrstuvwxzy
4
abx
bzz
bzy
caa

Sample Output 1

bzz
bzy
abx
caa

In the new alphabetical order set by Takahashi, b is smaller than a and z is smaller than y. Thus, sorting the citizens' names lexicographically would result in bzz, bzy, abx, caa in ascending order.


Sample Input 2

zyxwvutsrqponmlkjihgfedcba
5
a
ab
abc
ac
b

Sample Output 2

b
a
ac
ab
abc
G - New Friends

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

1 から N の番号がついた N 人のユーザが利用している SNS があります。

この SNS では 2 人のユーザが互いに友達になれる機能があります。
友達関係は双方向的です。すなわち、ユーザ X がユーザ Y の友達であるならば、必ずユーザ Y はユーザ X の友達です。

現在 SNS 上には M 組の友達関係が存在し、i 組目の友達関係はユーザ A_i とユーザ B_i からなります。

以下の操作を行える最大の回数を求めてください。

  • 操作:3 人のユーザ X, Y, Z であって、X と Y は友達、Y と Z は友達であり、X と Z は友達でないようなものを選ぶ。X と Z を友達にする。

制約

  • 2 \leq N \leq 2\times 10^5
  • 0 \leq M \leq 2\times 10^5
  • 1\leq A_i < B_i \leq N
  • (A_i,B_i) は相異なる
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 B_1
\vdots
A_M B_M

出力

答えを出力せよ。


入力例 1

4 3
1 2
2 3
1 4

出力例 1

3

次のようにして「友達の友達と新たに友達になる」という操作は 3 回行えます。

  • ユーザ 1 が友達(ユーザ 2)の友達であるユーザ 3 と新たに友達になる
  • ユーザ 3 が友達(ユーザ 1)の友達であるユーザ 4 と新たに友達になる
  • ユーザ 2 が友達(ユーザ 1)の友達であるユーザ 4 と新たに友達になる

4 回以上行うことはできません。


入力例 2

3 0

出力例 2

0

もともと友達関係が存在しないとき、新たな友達関係は発生しません。


入力例 3

10 8
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10

出力例 3

12

Score: 350 points

Problem Statement

There is an SNS used by N users, labeled with numbers from 1 to N.

In this SNS, two users can become friends with each other.
Friendship is bidirectional; if user X is a friend of user Y, user Y is always a friend of user X.

Currently, there are M pairs of friendships on the SNS, with the i-th pair consisting of users A_i and B_i.

Determine the maximum number of times the following operation can be performed:

  • Operation: Choose three users X, Y, and Z such that X and Y are friends, Y and Z are friends, but X and Z are not. Make X and Z friends.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_i < B_i \leq N
  • The pairs (A_i, B_i) are distinct.
  • All input values are integers.

Input

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

N M
A_1 B_1
\vdots
A_M B_M

Output

Print the answer.


Sample Input 1

4 3
1 2
2 3
1 4

Sample Output 1

3

Three new friendships with a friend's friend can occur as follows:

  • User 1 becomes friends with user 3, who is a friend of their friend (user 2)
  • User 3 becomes friends with user 4, who is a friend of their friend (user 1)
  • User 2 becomes friends with user 4, who is a friend of their friend (user 1)

There will not be four or more new friendships.


Sample Input 2

3 0

Sample Output 2

0

If there are no initial friendships, no new friendships can occur.


Sample Input 3

10 8
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10

Sample Output 3

12
H - Sensor Optimization Dilemma 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

ある製品の製造には 1,2,\dots,N の番号が付いた N 個の工程が必要です。

各工程 i について、それを処理する 2 種類の機械 S_i,T_i が売られています。

  • 機械 S_i : 1 台につき 1 日あたり製品 A_i 個分の処理ができ、 1P_i 円で導入できる
  • 機械 T_i : 1 台につき 1 日あたり製品 B_i 個分の処理ができ、 1Q_i 円で導入できる

それぞれの機械は 0 台以上何台でも導入できます。

機械の導入の結果、工程 i1 日あたり製品 W_i 個分処理できるようになったとします。
このとき、製造能力を W の最小値、すなわち \displaystyle \min^{N}_{i=1} W_i と定義します。

全体の予算が X 円のとき、達成可能な製造能力の最大値を求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 100
  • 1 \le A_i,B_i \le 100
  • 1 \le P_i,Q_i,X \le 10^7

入力

入力は以下の形式で標準入力から与えられる。

N X
A_1 P_1 B_1 Q_1
A_2 P_2 B_2 Q_2
\vdots
A_N P_N B_N Q_N

出力

答えを整数として出力せよ。


入力例 1

3 22
2 5 3 6
1 1 3 3
1 3 2 4

出力例 1

4

例えば、次の通り機械を導入することで製造能力を 4 にすることができ、これが達成可能な最大値です。

  • 工程 1 に対し機械 S_12 台導入する。
    • 1 日あたり製品 4 個分の処理に相当し、導入に合計 10 円かかる。
  • 工程 2 に対し機械 S_21 台導入する。
    • 1 日あたり製品 1 個分の処理に相当し、導入に合計 1 円かかる。
  • 工程 2 に対し機械 T_21 台導入する。
    • 1 日あたり製品 3 個分の処理に相当し、導入に合計 3 円かかる。
  • 工程 3 に対し機械 T_32 台導入する。
    • 1 日あたり製品 4 個分の処理に相当し、導入に合計 8 円かかる。

入力例 2

1 10000000
100 1 100 1

出力例 2

1000000000

入力例 3

1 1
1 10000000 1 10000000

出力例 3

0

正の製造能力が得られない場合もあります。


入力例 4

10 7654321
8 6 9 1
5 6 4 3
2 4 7 9
7 8 9 1
7 9 1 6
4 8 9 1
2 2 8 9
1 6 2 6
4 2 3 4
6 6 5 2

出力例 4

894742

Score : 475 points

Problem Statement

The manufacturing of a certain product requires N processes numbered 1,2,\dots,N.

For each process i, there are two types of machines S_i and T_i available for purchase to handle it.

  • Machine S_i: Can process A_i products per day per unit, and costs P_i yen per unit.
  • Machine T_i: Can process B_i products per day per unit, and costs Q_i yen per unit.

You can purchase any number of each machine, possibly zero.

Suppose that process i can handle W_i products per day as a result of introducing machines.
Here, we define the production capacity as the minimum of W, that is, \displaystyle \min^{N}_{i=1} W_i.

Given a total budget of X yen, find the maximum achievable production capacity.

Constraints

  • All input values are integers.
  • 1 \le N \le 100
  • 1 \le A_i,B_i \le 100
  • 1 \le P_i,Q_i,X \le 10^7

Input

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

N X
A_1 P_1 B_1 Q_1
A_2 P_2 B_2 Q_2
\vdots
A_N P_N B_N Q_N

Output

Print the answer as an integer.


Sample Input 1

3 22
2 5 3 6
1 1 3 3
1 3 2 4

Sample Output 1

4

For example, by introducing machines as follows, we can achieve a production capacity of 4, which is the maximum possible.

  • For process 1, introduce 2 units of machine S_1.
    • This allows processing 4 products per day and costs a total of 10 yen.
  • For process 2, introduce 1 unit of machine S_2.
    • This allows processing 1 product per day and costs a total of 1 yen.
  • For process 2, introduce 1 unit of machine T_2.
    • This allows processing 3 products per day and costs a total of 3 yen.
  • For process 3, introduce 2 units of machine T_3.
    • This allows processing 4 products per day and costs a total of 8 yen.

Sample Input 2

1 10000000
100 1 100 1

Sample Output 2

1000000000

Sample Input 3

1 1
1 10000000 1 10000000

Sample Output 3

0

There may be cases where a positive production capacity cannot be achieved.


Sample Input 4

10 7654321
8 6 9 1
5 6 4 3
2 4 7 9
7 8 9 1
7 9 1 6
4 8 9 1
2 2 8 9
1 6 2 6
4 2 3 4
6 6 5 2

Sample Output 4

894742
I - Endless Walk

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 頂点 M 辺からなる単純な有向グラフ G があり、頂点には頂点 1, 頂点 2, \ldots, 頂点 N と番号がついています。 また、i (1\leq i\leq M) 本目の辺は頂点 U_i から頂点 V_i へ張られています。

高橋君がある頂点から始めて、G の上を有向辺に沿って頂点から頂点へ移動することを繰り返します。 G の頂点のうち、高橋君がうまく経路を選ぶことで、その頂点から始めていくらでも移動を繰り返すことができるようなものの個数を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 0 \leq M \leq \min(N(N-1), 2\times 10^5)
  • 1 \leq U_i,V_i\leq N
  • U_i\neq V_i
  • i\neq j ならば (U_i,V_i)\neq (U_j,V_j)
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

答えを出力せよ。


入力例 1

5 5
1 2
2 3
3 4
4 2
4 5

出力例 1

4

頂点 2 を始点とした場合、頂点 2 \to 頂点 3 \to 頂点 4 \to 頂点 2 \to 頂点 3 \to \cdots と高橋君はいくらでも移動を繰り返す事ができます。 頂点 3, 頂点 4 を始点とした場合も同様です。 頂点 1 からは最初に頂点 2 に移動して、以下同様にいくらでも行動を繰り返すことができます。
一方、頂点 5 からは一度も移動することができません。

よって、条件を満たすのは頂点 1, 2, 3, 44 つであるので、 4 を出力します。


入力例 2

3 2
1 2
2 1

出力例 2

2

単純な有向グラフにおいて、2 つの頂点の間を互いに逆向きに結ぶ辺が、ともに存在する事はあり得ることに注意してください。 また、G は連結であるとは限りません。

Score : 500 points

Problem Statement

We have a simple directed graph G with N vertices and M edges. The vertices are labeled as Vertex 1, Vertex 2, \ldots, Vertex N. The i-th edge (1\leq i\leq M) goes from Vertex U_i to Vertex V_i.

Takahashi will start at a vertex and repeatedly travel on G from one vertex to another along a directed edge. How many vertices of G have the following condition: Takahashi can start at that vertex and continue traveling indefinitely by carefully choosing the path?

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 0 \leq M \leq \min(N(N-1), 2\times 10^5)
  • 1 \leq U_i,V_i\leq N
  • U_i\neq V_i
  • (U_i,V_i)\neq (U_j,V_j) if i\neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Print the answer.


Sample Input 1

5 5
1 2
2 3
3 4
4 2
4 5

Sample Output 1

4

When starting at Vertex 2, Takahashi can continue traveling indefinitely: 2 \to 3 \to 4 \to 2 \to 3 \to \cdots The same goes when starting at Vertex 3 or Vertex 4. From Vertex 1, he can first go to Vertex 2 and then continue traveling indefinitely again.
On the other hand, from Vertex 5, he cannot move at all.

Thus, four vertices ―Vertex 1, 2, 3, and 4― satisfy the conditions, so 4 should be printed.


Sample Input 2

3 2
1 2
2 1

Sample Output 2

2

Note that, in a simple directed graph, there may be two edges in opposite directions between the same pair of vertices. Additionally, G may not be connected.