実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 頂点 N-1 辺の木が与えられます。
頂点には 1,2,\ldots,N の番号がついており、i 本目の辺は頂点 a_i と頂点 b_i を結んでいます。
この木がスターであるか判定してください。
ただしスターとは、1 つの頂点から、他の全ての頂点に 1 本ずつ辺が出ている木のことです。
注記
「木」については、Wikipedia「木(数学)」 を参照してください。
制約
- 3 \leq N \leq 10^5
- 1 \leq a_i \lt b_i \leq N
- 与えられるグラフは木である
入力
入力は以下の形式で標準入力から与えられる。
N
a_1 b_1
\vdots
a_{N-1} b_{N-1}
出力
与えられたグラフがスターであるなら Yes と、スターでないなら No と出力せよ。
入力例 1
5 1 4 2 4 3 4 4 5
出力例 1
Yes
与えられたグラフはスターです。
入力例 2
4 2 4 1 4 2 3
出力例 2
No
与えられたグラフはスターではありません。
入力例 3
10 9 10 3 10 4 10 8 10 1 10 2 10 7 10 6 10 5 10
出力例 3
Yes
Score : 200 points
Problem Statement
You are given a tree with N vertices and N-1 edges.
The vertices are numbered 1,2,\ldots,N. The i-th edge connects Vertex a_i and Vertex b_i.
Determine whether this tree is a star.
Here, a star is a tree where there is a vertex directly connected to all other vertices.
Notes
For the definition of a tree, see Tree (graph theory) - Wikipedia.
Constraints
- 3 \leq N \leq 10^5
- 1 \leq a_i \lt b_i \leq N
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
\vdots
a_{N-1} b_{N-1}
Output
If the given graph is a star, print Yes; otherwise, print No.
Sample Input 1
5 1 4 2 4 3 4 4 5
Sample Output 1
Yes
The given graph is a star.
Sample Input 2
4 2 4 1 4 2 3
Sample Output 2
No
The given graph is not a star.
Sample Input 3
10 9 10 3 10 4 10 8 10 1 10 2 10 7 10 6 10 5 10
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
高橋君は改札機の利用履歴を集計しました。 しかし、高橋君はうっかりいくつかの入退場記録を消してしまいました。 高橋君は消してしまった記録の復元を試みようとしています。
i, o のみからなる文字列 S が与えられます。
S の任意の位置に文字を 0 文字以上挿入することで、変更後の文字列が以下の条件を満たすようにしたいです。
- 長さが偶数であり、奇数文字目が
iで偶数文字目がoである。
挿入する必要のある文字数の最小値を求めて下さい。なお、問題の制約下で、有限個の文字を適切に挿入することで、S が条件をみたすようにできることが証明できます。
制約
- S は
i,oからなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ioi
出力例 1
1
3 文字目のあとに o を挿入して ioio とすることで、条件を満たすことができます。0 文字以下の挿入で条件を満たすことはできません。
入力例 2
iioo
出力例 2
2
1 文字目のあとに o を、3 文字目のあとに i を挿入することで、条件を満たすことができます。1 文字以下の挿入で条件を満たすことはできません。
入力例 3
io
出力例 3
0
S がすでに条件を満たします。
Score : 250 points
Problem Statement
Takahashi aggregated usage records from ticket gates. However, he accidentally erased some records of entering and exiting stations. He is trying to restore the erased records.
You are given a string S consisting of i and o. We want to insert zero or more characters at arbitrary positions in S so that the resulting string satisfies the following conditions:
- Its length is even, and every odd-numbered (1st, 3rd, ...) character is
iwhile every even-numbered (2nd, 4th, ...) character iso.
Find the minimum number of characters that need to be inserted. It can be proved under the constraints of this problem that by inserting an appropriate finite number of characters, S can be made to satisfy the conditions.
Constraints
- S is a string of length between 1 and 100, consisting of
iando.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
ioi
Sample Output 1
1
We can insert o after the 3rd character to form ioio to satisfy the conditions. The conditions cannot be satisfied by inserting zero or fewer characters.
Sample Input 2
iioo
Sample Output 2
2
We can insert o after the 1st character and i after the 3rd character to satisfy the conditions. The conditions cannot be satisfied by inserting one or fewer characters.
Sample Input 3
io
Sample Output 3
0
S already satisfies the conditions.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
頂点に 1 から N の、辺に 1 から M の番号がついた N 頂点 M 辺の無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結ぶ辺です。
グラフから辺を取り除いてグラフを単純にするためには、少なくとも何本の辺を取り除く必要がありますか?
ここでグラフが単純であるとは、グラフが自己ループや多重辺を含まないことをいいます。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 5 \times 10^5
- 1 \leq u_i \leq N
- 1 \leq v_i \leq N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
グラフを単純にするために取り除く必要がある辺の本数の最小値を出力せよ。
入力例 1
3 5 1 2 2 3 3 2 3 1 1 1
出力例 1
2
辺 3 と辺 5 を取り除くとグラフを単純にすることが出来て、これが取り除く辺の本数が最小となる選び方の 1 つです。よって答えは 2 本です。
入力例 2
1 0
出力例 2
0
入力例 3
6 10 6 2 4 1 5 1 6 6 5 3 5 1 1 4 6 4 4 2 5 6
出力例 3
3
Score : 300 points
Problem Statement
You are given an undirected graph with N vertices and M edges, where the vertices are numbered 1 through N and the edges are numbered 1 through M. Edge i connects vertices u_i and v_i.
To make the graph simple by removing edges, what is the minimum number of edges that must be removed?
Here, a graph is called simple if and only if it does not contain self-loops or multi-edges.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 5 \times 10^5
- 1 \leq u_i \leq N
- 1 \leq v_i \leq N
- All input values are integers.
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 the minimum number of edges that must be removed to make the graph simple.
Sample Input 1
3 5 1 2 2 3 3 2 3 1 1 1
Sample Output 1
2
By removing edges 3 and 5, the graph becomes simple. This is one of the ways to remove the minimum number of edges, so the answer is 2.
Sample Input 2
1 0
Sample Output 2
0
Sample Input 3
6 10 6 2 4 1 5 1 6 6 5 3 5 1 1 4 6 4 4 2 5 6
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
6 個の整数 h_1, h_2, h_3, w_1, w_2, w_3 が与えられます。
縦横 3 \times 3 のマス目に、以下の条件をすべて満たすように各マスに正の整数を 1 つずつ書きこむことを考えます。
- i=1,2,3 について、上から i 行目に書きこんだ数の和が h_i になる。
- j=1,2,3 について、左から j 列目に書きこんだ数の和が w_j になる。
例えば (h_1, h_2, h_3) = (5, 13, 10), (w_1, w_2, w_3) = (6, 13, 9) のとき、以下の 3 通りの書きこみ方はすべて条件を満たしています。(条件を満たす書きこみ方は他にもあります)

さて、条件を満たす書きこみ方は全部で何通り存在しますか?
制約
- 3 \leq h_1, h_2, h_3, w_1, w_2, w_3 \leq 30
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
h_1 h_2 h_3 w_1 w_2 w_3
出力
条件を満たす書きこみ方が何通りあるかを出力せよ。
入力例 1
3 4 6 3 3 7
出力例 1
1
条件を満たす数の書きこみ方は次の 1 通りのみです。よって 1 を出力します。

入力例 2
3 4 5 6 7 8
出力例 2
0
条件を満たす書きこみ方が存在しないこともあります。
入力例 3
5 13 10 6 13 9
出力例 3
120
入力例 4
20 25 30 22 29 24
出力例 4
30613
Score : 300 points
Problem Statement
You are given six integers: h_1, h_2, h_3, w_1, w_2, and w_3.
Consider writing a positive integer on each square of a 3 \times 3 grid so that all of the following conditions are satisfied:
- For i=1,2,3, the sum of numbers written in the i-th row from the top is h_i.
- For j=1,2,3, the sum of numbers written in the j-th column from the left is w_i.
For example, if (h_1, h_2, h_3) = (5, 13, 10) and (w_1, w_2, w_3) = (6, 13, 9), then all of the following three ways satisfy the conditions. (There are other ways to satisfy the conditions.)

How many ways are there to write numbers to satisfy the conditions?
Constraints
- 3 \leq h_1, h_2, h_3, w_1, w_2, w_3 \leq 30
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
h_1 h_2 h_3 w_1 w_2 w_3
Output
Print the number of ways to write numbers to satisfy the conditions.
Sample Input 1
3 4 6 3 3 7
Sample Output 1
1
The following is the only way to satisfy the conditions. Thus, 1 should be printed.

Sample Input 2
3 4 5 6 7 8
Sample Output 2
0
There may not be a way to satisfy the conditions.
Sample Input 3
5 13 10 6 13 9
Sample Output 3
120
Sample Input 4
20 25 30 22 29 24
Sample Output 4
30613
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
1 台のサーバーと N 台の PC があります。 サーバーおよび各 PC はそれぞれ 1 つずつ文字列を保持しており、最初は全て空文字列です。
Q 個のクエリが与えられます。各クエリは以下のいずれかの形式です。
1 p:PC p の文字列をサーバーの文字列で置き換える。2 p s:PC p の文字列の末尾に文字列 s を追加する。3 p:サーバーの文字列をPC p の文字列で置き換える。
全てのクエリを与えられた順に処理したときの最終的なサーバーの文字列を求めてください。
制約
- N,Q は整数
- 1\leq N,Q \leq 2\times 10^5
- 全てのクエリについて、p は整数であり、1 \leq p\leq N
- 全ての 2 種類目のクエリについて、s は英小文字からなる長さ 1 以上の文字列
- 全ての 2 種類目のクエリに対する s の長さの総和は 10^6 以下
入力
入力は以下の形式で標準入力から与えられる。
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
ここで \mathrm{query}_i は i 番目のクエリを表し、以下のいずれかの形式で与えられる。
1 p
2 p s
3 p
出力
答えを出力せよ。
入力例 1
2 6 2 1 at 3 1 2 2 on 1 2 2 2 coder 3 2
出力例 1
atcoder
- 最初、サーバーおよび PC 1,2 の文字列は全て空である。
- 1 番目のクエリ: PC 1 の文字列の末尾に
atを追加する。このとき、サーバー、PC 1,2 の文字列はそれぞれ空、at、空である。 - 2 番目のクエリ: サーバーの文字列を PC 1 の文字列で置き換える。このとき、サーバー、PC 1,2 の文字列はそれぞれ
at、at、空である。 - 3 番目のクエリ: PC 2 の文字列の末尾に
onを追加する。このとき、サーバー、PC 1,2 の文字列はそれぞれat、at、onである。 - 4 番目のクエリ: PC 2 の文字列をサーバーの文字列で置き換える。このとき、サーバー、PC 1,2 の文字列はそれぞれ
at、at、atである。 - 5 番目のクエリ: PC 2 の文字列の末尾に
coderを追加する。このとき、サーバー、PC 1,2 の文字列はそれぞれat、at、atcoderである。 - 6 番目のクエリ: サーバーの文字列を PC 2 の文字列で置き換える。このとき、サーバー、PC 1,2 の文字列はそれぞれ
atcoder、at、atcoderである。
よって、最終的なサーバーの文字列は atcoder です。
入力例 2
100000 3 1 100 2 300 abc 3 200
出力例 2
最終的なサーバーの文字列は空です。
入力例 3
10 10 2 7 ladxf 2 7 zz 2 7 kfm 3 7 1 5 2 5 irur 3 5 1 6 2 6 ptilun 3 6
出力例 3
ladxfzzkfmirurptilun
Score : 425 points
Problem Statement
There is one server and N PCs. The server and each PC each hold one string, and initially all strings are empty.
Q queries are given. Each query is in one of the following formats:
1 p: Replace the string of PC p with the string of the server.2 p s: Append string s to the end of the string of PC p.3 p: Replace the string of the server with the string of PC p.
Find the final string of the server after processing all queries in the given order.
Constraints
- N,Q are integers
- 1\leq N,Q \leq 2\times 10^5
- For every query, p is an integer and 1 \leq p\leq N.
- For every query of type 2, s is a string of length at least 1 consisting of lowercase English letters.
- The sum of the lengths of s over all queries of type 2 is at most 10^6.
Input
The input is given from Standard Input in the following format:
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Here, \mathrm{query}_i represents the i-th query and is given in one of the following formats:
1 p
2 p s
3 p
Output
Output the answer.
Sample Input 1
2 6 2 1 at 3 1 2 2 on 1 2 2 2 coder 3 2
Sample Output 1
atcoder
- Initially, the strings of the server and PCs 1,2 are all empty.
- 1st query: Append
atto the end of the string of PC 1. At this time, the strings of the server, PC 1,2 are empty,at, empty, respectively. - 2nd query: Replace the string of the server with the string of PC 1. At this time, the strings of the server, PC 1,2 are
at,at, empty, respectively. - 3rd query: Append
onto the end of the string of PC 2. At this time, the strings of the server, PC 1,2 areat,at,on, respectively. - 4th query: Replace the string of PC 2 with the string of the server. At this time, the strings of the server, PC 1,2 are
at,at,at, respectively. - 5th query: Append
coderto the end of the string of PC 2. At this time, the strings of the server, PC 1,2 areat,at,atcoder, respectively. - 6th query: Replace the string of the server with the string of PC 2. At this time, the strings of the server, PC 1,2 are
atcoder,at,atcoder, respectively.
Thus, the final string of the server is atcoder.
Sample Input 2
100000 3 1 100 2 300 abc 3 200
Sample Output 2
The final string of the server is empty.
Sample Input 3
10 10 2 7 ladxf 2 7 zz 2 7 kfm 3 7 1 5 2 5 irur 3 5 1 6 2 6 ptilun 3 6
Sample Output 3
ladxfzzkfmirurptilun