You are to write a program that receives an unweighted undirected graph and writes all its vertices in
order of increasing distance from given vertex S.
Distance between vertices A and B is the length of the shortest path from A to B.
If there are several vertices such that their distances to S are equal,
they may be printed in arbitrary order.
Input file format
Input file contains three integers N, M and S, where M is the number of edges,
S is the starting vertex.
Vertices are numbered with integer numbers from 1 to N.
Each of next M lines contains a pair of integers — numbers of vertices connected by an edge.
Output file format
Output file must contain sequence of vertex numbers sorted by increasing distance from S.
If some vertex is not reachable from S, output a single number − 1.
You are to write a program that performs a topological sorting of a directed graph.
Graph contains N vertices, numbered with integers from 1 to N, and M edges.
In other words, given a partial order on numbers from 1 to N,
your program must produce some linear order on these numbers
which does not conflict with the given partial order.
Input file format
Input file contains two integers N and M, followed by M pairs of integers.
Integers in each pair are different, and represent starting and ending vertex of a graph edge.
These pairs may also be considered comparisons where the first number precedes
the second one.
Output file format
Output file must contain a permutation of integers from 1 to N —
numbers of vertices sorted in topological order.
(That is, representing a linear order.)
If ordering is not possible, output file must contain a single number − 1.
You are to write a program that receives a weighted directed graph and finds
distances from source vertex S to all other vertices. Distance from S to
some vertex W is the minimal length of path going from S to W.
Length of path is the sum of weights of its edges.
Vertices are numbered with integers from 1 to N.
Input file format
First line of input file contains three integers NM and S,
where M is the number of edges. Next M lines contain three
integers each —
starting vertex number, ending vertex number and weight of some edge respectively.
All weights are positive.
There is at most one edge connecting two vertices in every direction.
Output file format
Output file must contain N integers — distances from
source vertex to all vertices.
If some vertices are not reachable from S, corresponding numbers must be −1.
Constraints
1 ≤ N ≤ 1000.
All weights are less or equal than 1000.
You are to write a program that receives a weighted directed graph and finds
all distances from fixed vertex S to all other vertices. Distance from S to
some vertex W is the minimal length of path going from S to W. Length of path
is the sum of weights of its arcs.
Input file format
Input file contains two integers N, M and S.
Vertices are numbered with integer numbers from 1 to N. S is the number of
source vertex. M is the number of arcs. Each of next M lines contain three
integers — numbers of starting and ending vertices of some arc and its weight
respectively. All weights are positive. There is at most one arc connecting
two vertices in every direction.
Output file format
Output file must contain N numbers. Each I-th number is the distance from
vertex S to vertex I. If some vertices are not reachable from S, corresponding
numbers must be −1.
Constraints
1 ≤ N, M ≤ 100000
All weights are less or equal 1000.
You are to write a program that receives a weighted undirected graph and finds length of its shortest spanning tree.
Input file format
Input file contains two integers N, M.
Vertices are numbered with integer numbers from 1 to N. M is the number of edges. Each of next M lines contain three
integers describing an edge — numbers of vertices, connected by an edge and its weight respectively. All weights are
positive. There is at most one edge connecting two vertices.
Output file format
Output file must contain a signle integer number — length of the SST. If it is impossible to construct spanning tree,
output file must contain −1.
You are to write a program that receives a weighted undirected graph and finds length of its shortest spanning tree.
Input file format
Input file contains two integers N, M.
Vertices are numbered with integer numbers from 1 to N. M is the number of edges. Each of next M lines contain three
integers describing an edge — numbers of vertices, connected by an edge and its weight respectively. All weights are
positive. There is at most one edge connecting two vertices.
Output file format
Output file must contain a signle integer number — length of the SST. If it is impossible to construct spanning tree,
output file must contain −1.
Constraints
1 ≤ N, M ≤ 100000
All weights are less or equal 1000.
You are to write a program that finds shortest distances from vertex S
to all other vertices in a given directed weighted graph.
Graph consists of N vertices, numbered from 1 to N, and M edges.
Input file format
Input file contains two integers NMS, followed my M triplets of integers
ui vi wi — starting vertex, ending vertex and weight or the edge.
There is at most one edge connecting any two vertices in every direction. There are no cycles of negative weight.
Output file format
Output file must contain a vector of N integers — distances from vertex S to other vertices.
The distance from any vertex to itself is considered to be 0.
If some vertex is not reachable from S, corresponding cell of the vector must contain empty space.
Constraints
0 ≤ N, M ≤ 1000.
All weights are less than 1000 by absolute value.
You are to write a program that finds shortest distances between all pairs
of vertices in a directed weighted graph.
Graph consists of N vertices, numbered from 1 to N, and M edges.
Input file format
Input file contains two integers N and M, followed my M triplets of integers
ui vi wi — starting vertex, ending vertex and weight or the edge.
There is at most one edge connecting two vertices in every direction. There are no cycles of negative weight.
Output file format
Output file must contain a matrix of size NxN.
Element in the j-th column of i-th row mush be the shortest distance between
vertices i and j.
The distance from the vertex to itself is considered to be 0.
If some vertex is not reachable from some other,
there must be empty space in corresponding cell of matrix.
Constraints
0 ≤ N ≤ 100.
All weights are less than 1000 by absolute value.
You are to write a program that receives a directed unweighted graph and finds all vertices of its strong-connected
component, containing given vertex S.
Input file format
Input file contains two integers N, M and S.
Vertices are numbered with integer numbers from 1 to N. M is the number of arcs.
Each of next M lines contain pair of integers — starting and ending vertices of some arc respectively.
There is at most one arc connecting two vertices in every direction.
Output file format
Output file must contain integer number T — amount of vertices in strong-connected component. After that, there must
be T integer numbers in ascending order — vertices of compontent themselves.
You are to write a program that receives a sequence of words and sorts it in lexicographical order.
Linear order on characters is given by ASCII codes.
Input file format
First line of input file contains integer N — the sequence length.
Following N lines contain one word per line.
Each word is exactly three letters long.
Output file format
Output file must consist of N lines,
each containing one word from sorted sequence.
A well known algorithm called heapsort is a
deterministic sorting algorithm taking O(n log n) time
and O(1) additional memory. Let us describe ascending sorting
of an array of different integer numbers.
The algorithm consists of two phases. In the first phase,
called heapification, the array of integers to be sorted
is converted to a heap. An array a[1…n] of integers
is called a heap if for all 1 ≤ i ≤ n the following heap conditions
are satisfied:
- if 2i ≤ n
then a[i] > a[2i];
- if 2i + 1 ≤ n then a[i] > a[2i + 1].
We can interpret an array as a binary tree, considering children
of element a[i] to be a[2i] and a[2i + 1]. In this case
the parent of a[i] is a[i div 2], where i div 2 = floor(i / 2).
In terms of trees
the property of being a heap means that for each node its value is greater
than the values of its children.
In the second phase the heap is turned into a sorted array.
Because of the heap condition the greatest element
in the heapified array is a[1]. Let us exchange it with a[n],
now the greatest element of the array is at its correct position
in the sorted array. This is called extract-max.
Now let us consider the part of the array a[1 ... n-1]. It may be
not a heap because the heap condition may fail for i=1.
If it is so (that is, either a[2] or a[3], or both are greater
than a[1]) let us exchange the greatest child of a[1] with it, restoring
the heap condition for i=1. Now it is possible that the heap condition
fails for the position that now contains the former value of a[1]. Apply the same
procedure to it, exchanging it with its greatest child. Proceeding so
we convert the whole array a[1 ... n-1] to a heap.
This procedure is called sifting down.
After converting the part a[1 ... n-1] to a heap by sifting, we
apply extract-max again, putting second greatest element of
the array to a[n-1], and so on.
For example, let us see how the heap a=(5, 4, 2, 1, 3) is converted to
a sorted array. Let us make the first extract-max. After that the array
turns to (3, 4, 2, 1, 5). Heap condition fails
for a[1] = 3 because its child a[2] = 4 is greater than it.
Let us sift it down, exchanging a[1] and a[2]. Now the array is
(4, 3, 2, 1, 5). The heap condition is satisfied
for all elements, so sifting is over. Let us make extract-max again.
Now the array turns to (1, 3, 2, 4, 5). Again
the heap condition fails for a[1]; exchanging it with its greatest
child we get the array (3, 1, 2, 4, 5) which is
the correct heap. So we make extract-max and get
(2, 1, 3, 4, 5). This time the heap condition
is satisfied for all elements, so we make extract-max, getting
(1, 2, 3, 4, 5). The leading
part of the array is a heap, and the last
extract-max finally gives (1, 2, 3, 4, 5).
It is known that heapification can be done in O(n) time. Therefore,
the most time consuming operation in heapsort algorithm is sifting,
which takes O(n * log (n)) time.
In this problem you have to find a heapified array containing
different numbers from 1 to n, such that when
converting it to a sorted array, the total number of exchanges in
all sifting operations is maximal possible. In the example above the
number of exchanges is 1+1+0+0+0 = 2, which is not the maximum.
(5, 4, 3, 2, 1) gives the maximal number of 4 exchanges for n=5.
Input file format
Input file contains n.
Output file format
Output the array containing n different integer
numbers from 1 to n, such that it is a heap,
and when converting it to a sorted array,
the total number of exchanges in sifting operations is
maximal possible. Separate numbers by spaces.
You are to write a program that receives an undirected connected graph and finds its Eulerian cycle.
Input file format
Input file contains two integers N, M.
Vertices are numbered with integer numbers from 1 to N. M is the number of edges. Each of following M lines contains a pair of
vertex numbers, connected by some edge. There is at most one edge connecting two vertices.
Output file format
Output file must contain a sequence of vertex numbers in order of traversal in an Eiler cycle.
If there does not exist any Eiler cycle, output file must contain − 1.
You are to write a program that receives two strings and finds position where the second string appears in the first
one as a substring.
Input file format
First and second lines of input file contain given strings. Each string is a sequence of lower-case Latin letters from 'a'
to 'z' and spaces.
Output file format
Output file must contain a single integer — position of the first occurrence of the substring in a string, or − 1 if there is none. Positions are numbered from 1.
Constraints
Length of each string does not exceed 100000 characters.
Дана последовательность из N целых чисел. Для каждого числа вывести ближайшее к нему справа в этой последовательности, которое будет больше него.
Для чисел, которым найти ближайшее большее не удалось, вывести сами эти числа.
Формат входного файла
Входной файл содержит целое число N за которым следует N целых чисел ai - исходная последовательность.
Формат выходного файла
В выходной файл необходимо вывести N целых чисел bi, таких что bi является ответом на задачу для числа ai.
Дана последовательность из N целых чисел. Найдите любую из ее подпоследовательностей,
сумма элементов которой равна w, либо установите,
что искомой подпоследовательности не существует.
Формат входного файла
Во входном файле находятся числа N и w, а за ними следует последовательность
из N целых чисел ai.
Формат выходного файла
Если искомая подпоследовательность существует, выведите N чисел 0 или 1, разделенных пробелами.
Единица на позиции i означает, что элемент последовательности ai принадлежит найденной
подпоследовательности, 0 означает обратное. В противном случае выведите − 1.
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ., aN) be any sequence (ai1, ai2, ., aiK), where 1 ≤ i1 < i2 < ... < iK ≤ N. For example, the sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences of this sequence are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input file format
The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces.
Output file format
Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.
Constraints
1 ≤ N ≤ 1000
Sample tests
No.
Input file (input.txt)
Output file (output.txt)
1
7
1 7 3 5 9 4 8
4
Задача S. Наибольшая общая подпоследовательность
≡