Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1, 2, \dots, N の番号が、辺には 1, 2, \dots, M の番号が付けられています。
辺 i \, (i = 1, 2, \dots, M) は頂点 u_i, v_i を結んでいます。
このグラフがパスグラフであるか判定してください。
単純無向グラフとは
単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。
パスグラフとは
頂点に 1, 2, \dots, N の番号が付けられたN 頂点のグラフがパスグラフであるとは、(1, 2, \dots, N) を並べ変えて得られる数列 (v_1, v_2, \dots, v_N) であって、以下の条件を満たすものが存在することをいいます。
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
- 入力される値は全て整数
- 入力で与えられるグラフは単純
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
与えられたグラフがパスグラフなら Yes、そうでないなら No と出力せよ。
入力例 1
4 3 1 3 4 2 3 2
出力例 1
Yes
与えらえたグラフは下図のようであり、これはパスグラフです。

入力例 2
2 0
出力例 2
No
与えらえたグラフは下図のようであり、これはパスグラフではありません。

入力例 3
5 5 1 2 2 3 3 4 4 5 5 1
出力例 3
No
与えらえたグラフは下図のようであり、これはパスグラフではありません。

Score : 300 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, 2, \dots, N, and the edges are numbered 1, 2, \dots, M.
Edge i \, (i = 1, 2, \dots, M) connects vertices u_i and v_i.
Determine if this graph is a path graph.
What is a simple undirected graph?
A simple undirected graph is a graph without self-loops or multiple edges whose edges do not have a direction.
What is a path graph?
A graph with N vertices numbered 1, 2, \dots, N is said to be a path graph if and only if there is a sequence (v_1, v_2, \dots, v_N) that is a permutation of (1, 2, \dots, N) and satisfies the following conditions:
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
- All values in the input are integers.
- The graph given in the input is simple.
Input
The 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 Yes if the given graph is a path graph; print No otherwise.
Sample Input 1
4 3 1 3 4 2 3 2
Sample Output 1
Yes
Illustrated below is the given graph, which is a path graph.

Sample Input 2
2 0
Sample Output 2
No
Illustrated below is the given graph, which is not a path graph.

Sample Input 3
5 5 1 2 2 3 3 4 4 5 5 1
Sample Output 3
No
Illustrated below is the given graph, which is not a path graph.

Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個の文字列 S_1,\ldots,S_N が与えられます。
全ての要素が 1 以上 N 以下であるような長さ K の数列 (A_1,\ldots,A_K) に対し、
文字列 f(A_1,\ldots,A_K) を S_{A_1}+S_{A_2}+\dots+S_{A_K} と定めます。ここで + は文字列の連結を表します。
N^K 個の数列全てについての f(A_1,\dots,A_K) を辞書順に並べたとき、小さい方から X 番目の文字列を求めてください。
制約
- 1\leq N \leq 10
- 1\leq K \leq 5
- 1\leq X \leq N^K
- S_i は英小文字からなる長さ 10 以下の文字列
- N,K,X は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K X S_1 \vdots S_N
出力
答えを出力せよ。
入力例 1
3 2 6 abc xxx abc
出力例 1
abcxxx
- f(1,1)=
abcabc - f(1,2)=
abcxxx - f(1,3)=
abcabc - f(2,1)=
xxxabc - f(2,2)=
xxxxxx - f(2,3)=
xxxabc - f(3,1)=
abcabc - f(3,2)=
abcxxx - f(3,3)=
abcabc
であり、これらを辞書順に並べた abcabc, abcabc, abcabc, abcabc, abcxxx, abcxxx, xxxabc, xxxabc, xxxxxx の 6 番目は abcxxx です。
入力例 2
5 5 416 a aa aaa aa a
出力例 2
aaaaaaa
Score : 300 points
Problem Statement
You are given N strings S_1,\ldots,S_N.
For a sequence (A_1,\ldots,A_K) of length K where all elements are between 1 and N, inclusive,
define the string f(A_1,\ldots,A_K) as S_{A_1}+S_{A_2}+\dots+S_{A_K}. Here, + represents string concatenation.
When all f(A_1,\dots,A_K) for the N^K sequences are sorted in lexicographical order, find the X-th smallest string.
Constraints
- 1\leq N \leq 10
- 1\leq K \leq 5
- 1\leq X \leq N^K
- S_i is a string consisting of lowercase English letters with length at most 10.
- N, K, and X are all integers.
Input
The input is given from Standard Input in the following format:
N K X S_1 \vdots S_N
Output
Output the answer.
Sample Input 1
3 2 6 abc xxx abc
Sample Output 1
abcxxx
- f(1,1)=
abcabc - f(1,2)=
abcxxx - f(1,3)=
abcabc - f(2,1)=
xxxabc - f(2,2)=
xxxxxx - f(2,3)=
xxxabc - f(3,1)=
abcabc - f(3,2)=
abcxxx - f(3,3)=
abcabc
When these are sorted in lexicographical order: abcabc, abcabc, abcabc, abcabc, abcxxx, abcxxx, xxxabc, xxxabc, xxxxxx, the 6-th is abcxxx.
Sample Input 2
5 5 416 a aa aaa aa a
Sample Output 2
aaaaaaa
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
一辺の長さが 1 のマスからなる H 行 W 列のマス目と、N 枚のタイルがあります。
i (1\leq i\leq N) 枚目のタイルは A_i\times B_i の長方形です。
以下の条件をすべてみたすようにタイルをマス目に置くことができるか判定してください。
- 全てのマスがちょうど 1 枚のタイルで覆われている。
- 使用されないタイルがあっても良い。
- 使用するタイルは 回転したり裏返したりして置かれていても良い。ただし、各タイルはマスの線に合わせてマス目からはみ出ることがないように置かれていなければならない。
制約
- 1\leq N\leq 7
- 1 \leq H,W \leq 10
- 1\leq A_i,B_i\leq 10
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H W A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
問題文中の条件をみたすようにタイルをマス目に置くことができるならば Yes を、そうでないならば No を出力せよ。
入力例 1
5 5 5 1 1 3 3 4 4 2 3 2 5
出力例 1
Yes
2,4,5 枚目のタイルを使用して次のように置くと、マス目の各マスをちょうど 1 枚のタイルで覆うことができます。

よって、Yes を出力します。
入力例 2
1 1 2 2 3
出力例 2
No
マス目からはみ出さないようにタイルを置くことはできません。
よって、No を出力します。
入力例 3
1 2 2 1 1
出力例 3
No
全てのマスを覆うようにタイルを置くことができません。
よって、No を出力します。
入力例 4
5 3 3 1 1 2 2 2 2 2 2 2 2
出力例 4
No
全てのマスはちょうど 1 枚のタイルで覆われている必要があることに注意してください。
Score: 450 points
Problem Statement
There is a grid of H rows and W columns, each cell having a side length of 1, and we have N tiles.
The i-th tile (1\leq i\leq N) is a rectangle of size A_i\times B_i.
Determine whether it is possible to place the tiles on the grid so that all of the following conditions are satisfied:
- Every cell is covered by exactly one tile.
- It is fine to have unused tiles.
- The tiles may be rotated or flipped when placed. However, each tile must be aligned with the edges of the cells without extending outside the grid.
Constraints
- 1\leq N\leq 7
- 1 \leq H,W \leq 10
- 1\leq A_i,B_i\leq 10
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H W A_1 B_1 A_2 B_2 \ldots A_N B_N
Output
If it is possible to place the tiles on the grid so that all of the conditions in the problem statement are satisfied, print Yes; otherwise, print No.
Sample Input 1
5 5 5 1 1 3 3 4 4 2 3 2 5
Sample Output 1
Yes
Placing the 2-nd, 4-th, and 5-th tiles as shown below covers every cell of the grid by exactly one tile.

Hence, print Yes.
Sample Input 2
1 1 2 2 3
Sample Output 2
No
It is impossible to place the tile without letting it extend outside the grid.
Hence, print No.
Sample Input 3
1 2 2 1 1
Sample Output 3
No
It is impossible to cover all cells with the tile.
Hence, print No.
Sample Input 4
5 3 3 1 1 2 2 2 2 2 2 2 2
Sample Output 4
No
Note that each cell must be covered by exactly one tile.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
英小文字のみからなる文字列 X に対し、以下の手続きによって文字列を得ることを考えます。
- X を異なる文字が隣り合っている部分で分割する。
- 分割した各文字列 Y に対して、Y を Y を構成する文字と Y の長さを繋げた文字列に置き換える。
- 元の順番を保ったまま、置き換えた文字列をすべて繋げる。
例えば、aaabbcccc の場合、aaa,bb,cccc に分けられ、それぞれを a3,b2,c4 に置き換え、その順番のまま繋げることにより a3b2c4 を得ます。また、aaaaaaaaaa の場合、a10 になります。
長さ N の英小文字のみからなる文字列 S のうち、上記の手続きによって得られた文字列 T との長さを比べたとき、T の方が短いものの個数を P で割ったあまりを求めてください。
制約
- 1 \le N \le 3000
- 10^8 \le P \le 10^9
- N,P は整数
- P は素数
入力
入力は以下の形式で標準入力から与えられる。
N P
出力
答えを出力せよ。
入力例 1
3 998244353
出力例 1
26
1,2,3 文字目が全て等しい文字列のみが条件を満たします。
例えば、aaa は a3 となり条件を満たしますが、abc は a1b1c1 となり条件を満たしません。
入力例 2
2 998244353
出力例 2
0
aa → a2 のように、長さが等しいものは条件を満たさないことに注意してください。
入力例 3
5 998244353
出力例 3
2626
aaabb や aaaaa などが条件を満たします。
入力例 4
3000 924844033
出力例 4
607425699
条件を満たす文字列の個数を P で割ったあまりを求めてください。
Score : 500 points
Problem Statement
Consider the following procedure of, given a string X consisting of lowercase English alphabets, obtaining a new string:
- Split the string X off at the positions where two different characters are adjacent to each other.
- For each string Y that has been split off, replace Y with a string consisting of the character which Y consists of, followed by the length of Y.
- Concatenate the replaced strings without changing the order.
For example, aaabbcccc is divided into aaa,bb,cccc, which are replaced by a3,b2,c4, respectively, which in turn are concatenated without changing the order, resulting in a3b2c4.If the given string is aaaaaaaaaa , the new string is a10 .
Find the number, modulo P, of strings S of lengths N consisting of lowercase English alphabets, such that the length of T is smaller than that of S, where T is the string obtained by the procedure above against the string S.
Constraints
- 1 \le N \le 3000
- 10^8 \le P \le 10^9
- N and P are integers.
- P is a prime.
Input
Input is given from Standard Input in the following format:
N P
Output
Print the answer.
Sample Input 1
3 998244353
Sample Output 1
26
Those strings of which the 1-st, 2-nd, and 3-rd characters are all the same satisfy the condition.
For example, aaa becomes a3, which satisfies the condition, while abc becomes a1b1c1, which does not.
Sample Input 2
2 998244353
Sample Output 2
0
Note that if a string is transformed into another string of the same length, such as aa that becomes a2, it does not satisfy the condition.
Sample Input 3
5 998244353
Sample Output 3
2626
Strings like aaabb and aaaaa satisfy the condition.
Sample Input 4
3000 924844033
Sample Output 4
607425699
Find the number of strings satisfying the condition modulo P.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の正整数列 X=(X_1,X_2\ldots,X_N) に対して、f(X) を以下のように定めます。
- N 頂点の木であって、i\ (1 \leq i \leq N) 番目の頂点の次数が X_i であるようなものを良い木と呼ぶ。 良い木が存在するならば、f(X) は良い木の直径の最大値。良い木が存在しないならば、f(X)=0。
ただし、木の 2 頂点の間の距離は一方から他方へ移動するときに用いる辺の本数の最小値であり、 木の直径は任意の 2 頂点の間の距離の最大値として定められます。
長さ N の正整数列 X としてあり得るもの全てに対する f(X) の総和を 998244353 で割った余りを求めてください。 なお、f(X) の総和は有限値になることが証明できます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T \leq 2\times 10^5
- 2 \leq N \leq 10^6
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで、\mathrm{test}_i は i 番目のテストケースを意味する。
T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T
各テストケースは以下の形式で与えられる。
N
出力
T 行出力せよ。
i\ (1\leq i \leq T) 行目には、i 番目のテストケースに対する答えを出力せよ。
入力例 1
10 2 3 5 8 13 21 34 55 89 144
出力例 1
1 6 110 8052 9758476 421903645 377386885 881422708 120024839 351256142
N=3 の場合について、
例えば、
- X=(1,1,1) のとき、次数が 1,1,1 となる 3 頂点の木は存在しないため、f(X)=0 です。
- X=(2,1,1) のとき、良い木は以下の図のものに限られます。この木の直径は 2 であるため、f(X)=2 です。
X=(2,1,1),(1,2,1),(1,1,2) のとき f(X)=2 であり、それ以外の X のとき f(X)=0 であるため、答えは 6 です。
Score : 500 points
Problem Statement
For a sequence X=(X_1,X_2\ldots,X_N) of length N consisting of positive integers, we define f(X) as follows:
- A tree with N vertices is said to be good if and only if the degree of the i-th (1 \leq i \leq N) vertex is X_i. If a good tree exists, f(X) is the maximum diameter of a good tree; if it doesn't, f(X)=0.
Here, the distance between two vertices is the minimum number of edges that must be traversed to travel from one vertex to the other, and the diameter of a tree is the maximum distance between two vertices.
Find the sum, modulo 998244353, of f(X) over all possible sequences X of length N consisting of positive integers. We can prove that the sum of f(X) is a finite value.
Given T test cases, find the answer for each of them.
Constraints
- 1\leq T \leq 2\times 10^5
- 2 \leq N \leq 10^6
- All values in the input are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{test}_i denotes the i-th test case:
T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T
Each test case is given in the following format:
N
Output
Print T lines.
The i-th (1\leq i \leq T) line should contain the answer to the i-th test case.
Sample Input 1
10 2 3 5 8 13 21 34 55 89 144
Sample Output 1
1 6 110 8052 9758476 421903645 377386885 881422708 120024839 351256142
If N=3,
for example,
- when X=(1,1,1), there is no tree with three vertices whose degrees are 1,1, and 1, so f(X)=0.
- When X=(2,1,1), the only possible tree is illustrated below. The diameter of this tree is 2, so f(X)=2.
For X=(2,1,1),(1,2,1),(1,1,2), we have f(X)=2; for other X, we have f(X)=0. Thus, the answer is 6.