実行時間制限: 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
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
N 頂点の木が与えられます。頂点には 1,2,\dots,N の番号が、辺には 1,2,\dots,N-1 の番号がついています。辺 j は頂点 u_j,v_j を双方向に結び、重み w_j を持ちます。また、頂点 i には整数 x_i が与えられており、x_i > 0 なら x_i 個の陽電子が、x_i < 0 なら -x_i 個の電子が頂点 i に置かれています。x_i=0 ならば頂点 i には何もありません。ここで、\sum_{i=1}^N x_i = 0 が保証されます。
陽電子または電子 1 個を辺 j に沿って動かすと、エネルギー w_j がかかります。また、陽電子と電子が同じ頂点にあるとき、それらは同じ数だけ打ち消し合って消滅します。
すべての陽電子と電子を消滅させるために必要なエネルギーの最小値を求めてください。
制約
- 2 \leq N \leq 10^5
- |x_i| \leq 10^4
- \sum_{i=1}^N x_i = 0
- 1 \leq u_j < v_j \leq N
- 0 \leq w_j \leq 10^4
- 与えられるグラフは木である
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
x_1 x_2 \dots x_N
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_{N-1} v_{N-1} w_{N-1}
出力
答えを出力せよ。
入力例 1
4 -3 2 2 -1 1 2 2 1 3 1 1 4 3
出力例 1
9
はじめ、x=(x_1,x_2,x_3,x_4)=(-3,+2,+2,-1) です。 次のように操作することで、エネルギー 9 ですべての陽電子と電子を消滅させることができます。
- 頂点 1 にある電子を 1 つ、頂点 2 に移動させる。エネルギー 2 がかかり、x=(-2,+1,+2,-1) となる。
- 頂点 2 にある陽電子を 1 つ、頂点 1 に移動させる。エネルギー 2 がかかり、x=(-1,0,+2,-1) となる。
- 頂点 4 にある電子を 1 つ、頂点 1 に移動させる。エネルギー 3 がかかり、x=(-2,0,+2,0) となる。
- 頂点 1 にある電子を 1 つ、頂点 3 に移動させる。エネルギー 1 がかかり、x=(-1,0,+1,0) となる。
- 頂点 1 にある電子を 1 つ、頂点 3 に移動させる。エネルギー 1 がかかり、x=(0,0,0,0) となる。
8 以下のエネルギーですべての陽電子と電子を消滅させることはできないため、答えは 9 です。
入力例 2
2 0 0 1 2 1
出力例 2
0
はじめから条件が満たされている場合もあります。
入力例 3
5 -2 -8 10 -2 2 3 5 1 1 3 5 2 5 0 3 4 6
出力例 3
28
Score : 425 points
Problem Statement
You are given a tree with N vertices. The vertices are numbered 1,2,\dots,N, and the edges are numbered 1,2,\dots,N-1. Edge j bidirectionally connects vertices u_j and v_j and has weight w_j. Also, vertex i is given an integer x_i. If x_i > 0, then x_i positrons are placed at vertex i. If x_i < 0, then -x_i electrons are placed at vertex i. If x_i=0, then nothing is placed at vertex i. Here, it is guaranteed that \sum_{i=1}^N x_i = 0.
Moving one positron or electron along edge j costs energy w_j. Also, when a positron and an electron are at the same vertex, they annihilate each other in equal numbers.
Find the minimum energy required to annihilate all positrons and electrons.
Constraints
- 2 \leq N \leq 10^5
- |x_i| \leq 10^4
- \sum_{i=1}^N x_i = 0
- 1 \leq u_j < v_j \leq N
- 0 \leq w_j \leq 10^4
- The given graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
x_1 x_2 \dots x_N
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_{N-1} v_{N-1} w_{N-1}
Output
Output the answer.
Sample Input 1
4 -3 2 2 -1 1 2 2 1 3 1 1 4 3
Sample Output 1
9
Initially, x=(x_1,x_2,x_3,x_4)=(-3,+2,+2,-1). By operating as follows, all positrons and electrons can be annihilated with energy 9:
- Move one electron at vertex 1 to vertex 2. This costs energy 2, and x=(-2,+1,+2,-1).
- Move one positron at vertex 2 to vertex 1. This costs energy 2, and x=(-1,0,+2,-1).
- Move one electron at vertex 4 to vertex 1. This costs energy 3, and x=(-2,0,+2,0).
- Move one electron at vertex 1 to vertex 3. This costs energy 1, and x=(-1,0,+1,0).
- Move one electron at vertex 1 to vertex 3. This costs energy 1, and x=(0,0,0,0).
It is impossible to annihilate all positrons and electrons with energy 8 or less, so the answer is 9.
Sample Input 2
2 0 0 1 2 1
Sample Output 2
0
The condition may already be satisfied from the beginning.
Sample Input 3
5 -2 -8 10 -2 2 3 5 1 1 3 5 2 5 0 3 4 6
Sample Output 3
28
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
正整数 N と長さ N の正整数列 A=(A_1,A_2,\dots,A_N) と B=(B_1,B_2,\dots,B_N) が与えられます。
N \times N のマス目があります。上から i 行目、左から j 列目のマスをマス (i,j) と呼びます。1 \le i,j \le N を満たす整数の組 (i,j) に対し、マス (i,j) に A_i + B_j が書かれています。以下のクエリを Q 個処理してください。
- 1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N を満たす整数の組 h_1,h_2,w_1,w_2 が与えられる。左上隅が (h_1,w_1)、右下隅が (h_2,w_2) である矩形領域に含まれる整数の最大公約数を求めよ。
制約
- 1 \le N,Q \le 2 \times 10^5
- 1 \le A_i,B_i \le 10^9
- 1 \le h_1 \le h_2 \le N
- 1 \le w_1 \le w_2 \le N
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N Q
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
h_1 h_2 w_1 w_2
出力
Q 行出力せよ。i 行目には \mathrm{query}_i の答えを出力せよ。
入力例 1
3 5 3 5 2 8 1 3 1 2 2 3 1 3 1 3 1 1 1 1 2 2 2 2 3 3 1 1
出力例 1
2 1 11 6 10
マス (i,j) に書かれている整数を C_{i,j} とします。
1 個目のクエリについて、C_{1,2}=4,C_{1,3}=6,C_{2,2}=6,C_{2,3}=8 なのでこれらの最大公約数の 2 が答えとなります。
入力例 2
1 1 9 100 1 1 1 1
出力例 2
109
Score : 500 points
Problem Statement
You are given a positive integer N and sequences of N positive integers each: A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N).
We have an N \times N grid. The square at the i-th row from the top and the j-th column from the left is called the square (i,j). For each pair of integers (i,j) such that 1 \le i,j \le N, the square (i,j) has the integer A_i + B_j written on it. Process Q queries of the following form.
- You are given a quadruple of integers h_1,h_2,w_1,w_2 such that 1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N. Find the greatest common divisor of the integers contained in the rectangle region whose top-left and bottom-right corners are (h_1,w_1) and (h_2,w_2), respectively.
Constraints
- 1 \le N,Q \le 2 \times 10^5
- 1 \le A_i,B_i \le 10^9
- 1 \le h_1 \le h_2 \le N
- 1 \le w_1 \le w_2 \le N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is in the following format:
h_1 h_2 w_1 w_2
Output
Print Q lines. The i-th line should contain the answer to \mathrm{query}_i.
Sample Input 1
3 5 3 5 2 8 1 3 1 2 2 3 1 3 1 3 1 1 1 1 2 2 2 2 3 3 1 1
Sample Output 1
2 1 11 6 10
Let C_{i,j} denote the integer on the square (i,j).
For the 1-st query, we have C_{1,2}=4,C_{1,3}=6,C_{2,2}=6,C_{2,3}=8, so the answer is their greatest common divisor, which is 2.
Sample Input 2
1 1 9 100 1 1 1 1
Sample Output 2
109