Description
One day Vasya came across three Berland coins. They didn't have any numbers that's why Vasya didn't understand how their denominations differ. He supposed that if one coin is heavier than the other one, then it should be worth more. Vasya weighed all the three pairs of coins on pan balance scales and told you the results. Find out how the deminations of the coins differ or if Vasya has a mistake in the weighting results. No two coins are equal.
Input
The input data contains the results of all the weighting, one result on each line. It is guaranteed that every coin pair was weighted exactly once. Vasya labelled the coins with letters «A», «B» and «C». Each result is a line that appears as (letter)(> or < sign)(letter). For example, if coin "A" proved lighter than coin "B", the result of the weighting is A<B.
Output
It the results are contradictory, print Impossible. Otherwise, print without spaces the rearrangement of letters «A», «B» and «C» which represent the coins in the increasing order of their weights.
Sample Input
A>B C<B A>C
CBA
A<B B>C C>A
ACB
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MaxN = 10001;
bool v[4][4];
int main(){
int k , j , i , x , y;
char a[4];
memset(v,0,sizeof(v));
cin >> a;
x = a[0] - 64; y = a[2] - 64;
if (a[1] == '>') v[x][y] = 1; else v[y][x] = 1;
cin >> a;
x = a[0] - 64; y = a[2] - 64;
if (a[1] == '>') v[x][y] = 1; else v[y][x] = 1;
cin >> a;
x = a[0] - 64; y = a[2] - 64;
if (a[1] == '>') v[x][y] = 1; else v[y][x] = 1;
if (v[1][2] && v[2][3] && (!v[2][1]) && (!v[3][1]) && (!v[3][2])) cout << "CBA" << endl; else
if (v[1][3] && v[3][2] && (!v[3][1]) && (!v[2][1]) && (!v[2][3])) cout << "BCA" << endl; else
if (v[2][1] && v[1][3] && (!v[1][2]) && (!v[3][2]) && (!v[3][1])) cout << "CAB" << endl; else
if (v[2][3] && v[3][1] && (!v[3][2]) && (!v[1][2]) && (!v[1][3])) cout << "ACB" << endl; else
if (v[3][1] && v[1][2] && (!v[1][3]) && (!v[2][3]) && (!v[2][1])) cout << "BAC" << endl; else
if (v[3][2] && v[2][1] && (!v[2][3]) && (!v[1][3]) && (!v[1][2])) cout << "ABC" << endl; else
cout << "Impossible" << endl;
cin >> a;
return 0;
}
Description
Sometimes one has to spell email addresses over the phone. Then one usually pronounces a dot as dot, an at sign as at. As a result, we get something like vasyaatgmaildotcom. Your task is to transform it into a proper email address (vasya@gmail.com).
It is known that a proper email address contains only such symbols as . @ and lower-case Latin letters, doesn't start with and doesn't end with a dot. Also, a proper email address doesn't start with and doesn't end with an at sign. Moreover, an email address contains exactly one such symbol as @, yet may contain any number (possible, zero) of dots.
You have to carry out a series of replacements so that the length of the result was as short as possible and it was a proper email address. If the lengths are equal, you should print the lexicographically minimal result.
Overall, two variants of replacement are possible: dot can be replaced by a dot, at can be replaced by an at.
Input
The first line contains the email address description. It is guaranteed that that is a proper email address with all the dots replaced by dot an the at signs replaced by at. The line is not empty and its length does not exceed 100 symbols.
Output
Print the shortest email address, from which the given line could be made by the described above replacements. If there are several solutions to that problem, print the lexicographically minimal one (the lexicographical comparison of the lines are implemented with an operator < in modern programming languages).
In the ASCII table the symbols go in this order: . @ ab...z
Sample Input
vasyaatgmaildotcom
vasya@gmail.com
dotdotdotatdotdotat
dot..@..at
aatt
a@t
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MaxN = 10001;
char a[1001];
char b[1001];
int main(){
int k , j , i , len;
bool flag = 0;
cin >> a;
len = strlen(a); i = -1;
while (1){
i++;
if (i == len) break;
if (i > 0 && i+2 < len && a[i] == 'a' && a[i+1] == 't' && !flag){
flag = 1;
i++;
cout << '@';
} else
if (i > 0 && i+3 < len && a[i] == 'd' && a[i+1] == 'o' && a[i+2] == 't'){
i += 2;
cout << '.';
} else cout << a[i];
}
cout << endl;
cin >> a;
return 0;
}
Description
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Sample Input
)((())))(()())
6 2
))(
0 1
#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
stack <pair<char,long long> > t;
char a[1000010];
int main()
{
long long maxn = 0,maxnum = 1;
long long n = 0;
scanf("%s", a);
long long aaa = strlen(a);
for (long long i = 0; i < aaa; i ++)
{
if (a[i] == '(')
{
t.push(make_pair(a[i], n));
n = 0;
}
else
{
if (t.empty())
n = 0;
else
{
long long u = t.top().second;
t.pop();
n += (u + 2);
if (n > maxn)
{
maxn = n;
maxnum = 1;
}
else if (n != 0 && n == maxn)
maxnum++;
}
}
}
printf("%I64d %I64d\n", maxn, maxnum);
return 0;
}
Description
In a very ancient country the following game was popular. Two people play the game. Initially first player writes a string s1, consisting of exactly nine digits and representing a number that does not exceed a. After that second player looks at s1 and writes a string s2, consisting of exactly nine digits and representing a number that does not exceed b. Here a and b are some given constants, s1 and s2 are chosen by the players. The strings are allowed to contain leading zeroes.
If a number obtained by the concatenation (joining together) of strings s1 and s2 is divisible by mod, then the second player wins. Otherwise the first player wins. You are given numbers a, b, mod. Your task is to determine who wins if both players play in the optimal manner. If the first player wins, you are also required to find the lexicographically minimum winning move.
Input
The first line contains three integers a, b, mod (0 ≤ a, b ≤ 109, 1 ≤ mod ≤ 107).
Output
If the first player wins, print "1" and the lexicographically minimum string s1 he has to write to win. If the second player wins, print the single number "2".
Sample Input
1 10 7
2
4 0 9
1 000000001
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define LL long long
int n;
LL a,b,mod;
int main()
{
int i,j,T;
while (scanf("%I64d%I64d%I64d",&a,&b,&mod)!=EOF)
{
if(b>=mod) printf("2\n");
else
{
if(a>mod) a=mod;
bool ok = true;
for (i=0; i<=a; i++)
{
LL temp;
temp = i*1000000000LL;
temp%=mod;
if(temp==0) temp = mod;
temp = (mod-temp);
if(temp>b)
{
ok = false;
break;
}
}
if(ok) printf("2\n");
else
{
printf("1 ");
int ans[10];
for (j=1; j<=9; j++)
{
ans[j] = i%10;
i/=10;
}
for (j=9; j>=1; j--)
printf("%d",ans[j]);
printf("\n");
}
}
}
}
Description
...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu...
Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster's behavior and preparing the secret super weapon. Due to high seismic activity and poor weather conditions the satellites haven't yet been able to make clear shots of the monster. The analysis of the first shot resulted in an undirected graph with n vertices and m edges. Now the world's best minds are about to determine whether this graph can be regarded as Cthulhu or not.
To add simplicity, let's suppose that Cthulhu looks from the space like some spherical body with tentacles attached to it. Formally, we shall regard as Cthulhu such an undirected graph that can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycle.
It is guaranteed that the graph contains no multiple edges and self-loops.
Input
The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ ).
Each of the following m lines contains a pair of integers x and y, that show that an edge exists between vertices x and y (1 ≤ x, y ≤ n, x ≠ y). For each pair of vertices there will be at most one edge between them, no edge connects a vertex to itself.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Sample Input
6 6 6 3 6 4 5 1 2 5 1 4 5 4
FHTAGN!
6 5 5 6 4 6 3 1 5 1 1 2
NO
#include <stdio.h>
#include <iostream>
#include <cstring>
#define MAXN 100 + 5
using namespace std;
int n,m;
int MATEMP[MAXN][MAXN];
bool SHUZU1[MAXN]= {0};
int sum=0;
int TEMP[MAXN]= {0};
int JULI[MAXN]= {0};
bool dfs(int i,int side);
void len_dfs(int i);
bool look_for(int i,int side);
int main()
{
memset(JULI, -1, sizeof(JULI));
cin>>n>>m;
for (int i=1; i<=m; i++)
{
int x,y;
cin>>x>>y;
MATEMP[x][y]=MATEMP[y][x]=1;
}
TEMP[sum++]=1;
memset(SHUZU1, 0, sizeof(SHUZU1));
bool ret=look_for(1,0);
if (ret)
{
JULI[TEMP[sum-1]]=0;
for (int i=sum-2; i>=0; i--)
{
if (TEMP[i]==TEMP[sum-1]) break;
JULI[TEMP[i]]=0;
}
for (int i=1;i<=n;i++)
{
if (JULI[i]==0)
{
memset(SHUZU1,0,sizeof(SHUZU1));
if (dfs(i,0))
{
ret=false;
break;
}
}
}
}
memset(SHUZU1 , 0 , sizeof(SHUZU1));
len_dfs(1);
for (int i=1;i<=n;i++)
{
if (!SHUZU1[i]) ret=false;
}
if (ret) cout<<"FHTAGN!"<<endl;
else cout<<"NO"<<endl;
return 0;
}
bool dfs(int i,int side)
{
SHUZU1[i]=true;
for (int j=1;j<=n;j++)
{
if (i!=j&&j!=side&&MATEMP[i][j])
{
if (side==0 && JULI[j]==0) continue;
if (SHUZU1[j] || JULI[j]==0)
{
return true;
}
else
{
if (dfs(j,i)) return true;
}
}
}
return false;
}
bool look_for(int i,int side)
{
SHUZU1[i]=true;
for (int j=1; j<=n; j++)
{
if (j!=side&&i!=j&&MATEMP[i][j]>0)
{
if (!SHUZU1[j])
{
TEMP[sum++]=j;
if (look_for(j,i)) return true;
sum--;
}
else
{
TEMP[sum++]=j;
return true;
}
}
}
return false;
}
void len_dfs(int i)
{
SHUZU1[i]=true;
for (int j=1;j<=n;j++)
{
if (!SHUZU1[j]&&MATEMP[i][j]>0) len_dfs(j);
}
}
Description
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right.
To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100".
Determine the minimum number of moves needed to make some table column consist only of numbers 1.
Input
The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table.
It is guaranteed that the description of the table contains no other characters besides "0" and "1".
Output
Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1.
Sample Input
3 6 101010 000100 100000
3
2 3 111 000
-1
这题直接打一张表。
#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
#define K 111
char map[101][10010];
int save[K+1][10010];
int tmp[K+1][10010];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int cnt=0;
for(int i=1;i<=n;i++)
{
cnt=0;
for(int j=1;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]=='1')
{
tmp[i][cnt++]=j;
}
else save[i][j]=cnt-1;
}
if(cnt==0)
{
printf("-1");
return 0;
}
save[i][0]=cnt;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(map[i][j]=='1')
save[i][j]=0;
else
{
if(save[i][j]==-1)
{
save[i][j] = min(tmp[i][ save[i][j]+1 ]- j , m- tmp[i][save[i][0]-1]+j);
}
else
{
if(save[i][j]==save[i][0]-1)
save[i][j]=min(j-tmp[i][ save[i][j] ] ,tmp[i][0] + m - j);
else
save[i][j]=min(j-tmp[i][save[i][j]],tmp[i][ save[i][j]+1 ]- j);
}
}
}
int mi=100000000;
for(int j=1;j<=m;j++)
{
int sum=0;
for(int i=1;i<=n;i++) sum+=save[i][j];
if(sum<mi) mi=sum;
}
printf("%d",mi);
return 0;
}
Description
The Hedgehog likes to give presents to his friend, but no less he likes to receive them.
Having received another present today, the Hedgehog suddenly understood that he has no place to put it as there was no room left on the special shelf in the cupboard. He will have to choose another shelf, but which one should he choose, how large should it be?
In order to get to know this, the Hedgehog asks you to write him a program that will count the estimated number of presents that he will receive during the following N days. Besides, he is guided by the principle:
- on each holiday day the Hedgehog will necessarily receive a present,
- he receives presents at least every K days (i.e., if he received a present on the i-th day, he will receive the next present no later than on the i + K-th day).
For the given N and K, as well as the list of holidays among the following N days count the minimal number of presents that could be given to the Hedgehog. The number of today's day is zero, and you should regard today's present as already given (i.e., you shouldn't count it in the answer).
Input
The first line contains integers N and K (1 ≤ N ≤ 365, 1 ≤ K ≤ N).
The second line contains a number C which represents the number of holidays (0 ≤ C ≤ N). Then in the same line follow C numbers ranging from 1 to N which are the numbers of holiday days. The numbers are given in the increasing order, without repeating numbers among them.
Output
Print a single number — the minimal number of presents the Hedgehog will receive over the following N days.
Sample Input
5 2 1 3
3
10 1 3 6 7 8
10
#include<stdio.h>
int day[370];
int main()
{
int i;
int n, k, c;
day[0] = 0;
scanf("%d%d%d", &n, &k, &c);
for(i = 1; i <= c; i++)
{
scanf("%d", &day[i]);
}
day[i] = n;
int sum = c;
for(i = 1; i <= c; i++)
{
sum += ((day[i] - day[i - 1] - 1) / k);
}
sum = sum + ((day[i] - day[i - 1]) / k);
printf("%d\n", sum);
return 0;
}
Description
Petya and Gena play a very interesting game "Put a Knight!" on a chessboard n × n in size. In this game they take turns to put chess pieces called "knights" on the board so that no two knights could threat each other. A knight located in square (r, c) can threat squares (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r - 2, c + 1), (r - 2, c - 1), (r + 2, c + 1) and (r + 2, c - 1) (some of the squares may be located outside the chessboard). The player who can't put a new knight during his move loses. Determine which player wins considering that both players play optimally well and Petya starts.
Input
The first line contains integer T (1 ≤ T ≤ 100) — the number of boards, for which you should determine the winning player. Next Tlines contain T integers ni (1 ≤ ni ≤ 10000) — the sizes of the chessboards.
Output
For each ni × ni board print on a single line "0" if Petya wins considering both players play optimally well. Otherwise, print "1".
Sample Input
2 2 1
1 0
#include<stdio.h>
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int a;
scanf("%d", &a);
if(a % 2)
printf("0\n");
else printf("1\n");
}
return 0;
}
Description
devtang在某一天无聊至极,突然萌生了一种想法:devtang想到了一些特殊的字符串,现在他需要你来帮助他构造这些特殊的字符串,比如说:这个字符串(长度为n,最多由m种字符组成)其中任意长度为k的子串必须是回文串。那么这样的串你能构造出多少个呢?这个数可能很大,所以结果必须mod1000000007,小心不要遗漏任何字符串。
Input
输入共有三个数据:n, m and k (1 ≤ n, m, k ≤ 2000).
Output
输出只有一个整数(mod1000000007),表示字符串的数目.
Sample Input
1 1 1
1
5 2 4
2
Hint
在第一组数据里,这样的字符串只有一个,比如: "a"。
在第二组数据里,这样的字符串只有2个,比如:"aaaaa","bbbbb"。
#include <iostream>
#include <math.h>
#define MOD 1000000007
using namespace std;
long long ppow(long long x,long long y){
long long res=1;
while(y--){
res=res*x%MOD;
}
return res;
}
int main(){
long long n,m,k;
while(cin >> n >> m >> k){
if(k==1 || k>n){
cout << ppow(m,n) <<endl;
}
else if(k==n){
if(n%2==0){
cout << ppow(m,n/2) << endl;
}
else{
cout << ppow(m,n/2+1) <<endl;
}
}
else{
if(k%2==0){
cout << m <<endl;
}
else{
cout << m*m <<endl;
}
}
}
return 0;
}
Description
One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "x y" are there, such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. Two pairs are considered different, if they contain different substrings of string s or different subsequences of string t. Read the whole statement to understand the definition of different substrings and subsequences.
The length of string s is the number of characters in it. If we denote the length of the string s as |s|, we can write the string as s = s1s2...s|s|.
A substring of s is a non-empty string x = s[a... b] = sasa + 1... sb (1 ≤ a ≤ b ≤ |s|). For example, "code" and "force" are substrings or "codeforces", while "coders" is not. Two substrings s[a... b] and s[c... d] are considered to be different if a ≠ c or b ≠ d. For example, if s="codeforces", s[2...2] and s[6...6] are different, though their content is the same.
A subsequence of s is a non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 ≤ p1 < p2 < ... < p|y| ≤ |s|). For example, "coders" is a subsequence of "codeforces". Two subsequences u = s[p1p2... p|u|] and v = s[q1q2... q|v|] are considered different if the sequencesp and q are different.
Input
The input consists of two lines. The first of them contains s (1 ≤ |s| ≤ 5000), and the second one contains t (1 ≤ |t| ≤ 5000). Both strings consist of lowercase Latin letters.
Output
Print a single number — the number of different pairs "x y" such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. As the answer can be rather large, print it modulo 1000000007 (109 + 7).
Sample Input
aa aa
5
codeforces forceofcode
60
Hint
Let's write down all pairs "x y" that form the answer in the first sample: "s[1...1] t[1]", "s[2...2] t[1]", "s[1...1] t[2]","s[2...2] t[2]", "s[1...2] t[1 2]".
#include <string>
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = 5050;
const long long MOD = 1000000007;
long long dp[MAXN][MAXN];
string s, t;
int main() {
int n, m;
long long ans = 0;
cin >> s >> t;
n = s.length();
m = t.length();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (s[i] == t[j]) {
++dp[i][j];
if (i > 0 && j > 0) {
dp[i][j] += dp[i - 1][j - 1];
}
}
if (j > 0) {
dp[i][j] += dp[i][j - 1];
}
dp[i][j] %= MOD;
}
ans += dp[i][m - 1];
}
cout << ans % MOD << endl;
return 0;
}
Description
Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described).
This priority factor will be affected by three types of actions:
- 1. "X posted on Y's wall" (15 points),
- 2. "X commented on Y's post" (10 points),
- 3. "X likes Y's post" (5 points).
X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X).
You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you.
Input
The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters.
Output
Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order.
Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor.
The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently.
Sample Input
ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post
fatma mona
aba 1 likes likes posted's post
likes posted
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
string me;
map<string,int>m;
map<string,int>p;
int main()
{
p["posted"]=15;
p["commented"]=10;
p["likes"]=5;
cin>>me;
int t;
cin>>t;
while(t--)
{
string ss,tt,gg,ww;
cin>>ss>>tt;
if(tt[0]=='l')
cin>>ww;
else cin>>ww>>ww;
gg=ww.substr(0,ww.length()-2);
m[ss]+=0;
m[gg]+=0;
if(ss==me||gg==me){
m[ss]+=p[tt];
m[gg]+=p[tt];
}
cin>>ww;
}
vector<pair<int, string> > ans;
for(map<string,int>::iterator itr = m.begin(); itr != m.end(); itr++)
if(itr->first != me)
ans.push_back( make_pair(-(itr->second), itr->first));
sort(ans.begin(), ans.end());
for(int i=0; i<ans.size(); i++)
cout << ans[i].second << endl;
return 0;
}
Description
A subway scheme, classic for all Berland cities is represented by a set of n stations connected by n passages, each of which connects exactly two stations and does not pass through any others. Besides, in the classic scheme one can get from any station to any other one along the passages. The passages can be used to move in both directions. Between each pair of stations there is no more than one passage.
Berland mathematicians have recently proved a theorem that states that any classic scheme has a ringroad. There can be only one ringroad. In other words, in any classic scheme one can find the only scheme consisting of stations (where any two neighbouring ones are linked by a passage) and this cycle doesn't contain any station more than once.
This invention had a powerful social impact as now the stations could be compared according to their distance from the ringroad. For example, a citizen could say "I live in three passages from the ringroad" and another one could reply "you loser, I live in one passage from the ringroad". The Internet soon got filled with applications that promised to count the distance from the station to the ringroad (send a text message to a short number...).
The Berland government decided to put an end to these disturbances and start to control the situation. You are requested to write a program that can determine the remoteness from the ringroad for each station by the city subway scheme.
Input
The first line contains an integer n (3 ≤ n ≤ 3000), n is the number of stations (and trains at the same time) in the subway scheme. Thenn lines contain descriptions of the trains, one per line. Each line contains a pair of integers xi, yi (1 ≤ xi, yi ≤ n) and represents the presence of a passage from station xi to station yi. The stations are numbered from 1 to n in an arbitrary order. It is guaranteed that xi ≠ yi and that no pair of stations contain more than one passage. The passages can be used to travel both ways. It is guaranteed that the given description represents a classic subway scheme.
Output
Print n numbers. Separate the numbers by spaces, the i-th one should be equal to the distance of the i-th station from the ringroad. For the ringroad stations print number 0.
Sample Input
4 1 3 4 3 4 2 1 2
0 0 0 0
6 1 2 3 4 6 4 2 3 1 3 3 5
0 0 0 1 1 2
#include<iostream>
using namespace std;
#define MAXN 10000
#define INF 999999
struct Edge { int v, next; };
Edge edge[MAXN];
int dfn[MAXN], low[MAXN];
int stk[MAXN], top, id;
int head[MAXN], E;
int dis[MAXN], block[MAXN];
void add_edge ( int u, int v )
{
E++;
edge[E].v = v;
edge[E].next = head[u];
head[u] = E;
}
void Tarjan ( int u, int father )
{
stk[++top] = u;
dfn[u] = low[u] = ++id;
for ( int i = head[u]; i != -1; i = edge[i].next )
{
if ( edge[i].v == father ) continue;
if ( dfn[edge[i].v] == 0 )
{
Tarjan ( edge[i].v, u );
low[u] = min ( low[u], low[edge[i].v] );
if ( low[edge[i].v] > dfn[u] )
top--;
else if ( low[edge[i].v] == dfn[u] )
{
int t, cnt = 0;
do
{
t = stk[top--];
block[++cnt] = t;
} while ( t != u );
}
}
else
low[u] = min ( low[u], dfn[edge[i].v] );
}
}
int main()
{
int n, u, v;
cin >> n;
for ( int i = 0; i < n * 2 + 1; i++ )
{
head[i] = block[i] = -1;
dfn[i] = low[i] = 0;
dis[i] = INF;
E = top = id = 0;
}
for ( int i = 1; i <= n; i++ )
{
cin >> u >> v;
add_edge ( u, v );
add_edge ( v, u );
}
Tarjan ( 1, 0 );
for ( int i = 1; i <= n; i++ )
dis[block[i]] = 0;
int que[MAXN];
bool vis[MAXN] = { 0 };
int front = 0, rear = 0;
que[rear++] = block[1];
vis[block[1]] = true;
while ( front < rear )
{
int u = que[front++];
for ( int i = head[u]; i != -1; i = edge[i].next )
{
if ( ! vis[edge[i].v] )
{
if ( dis[edge[i].v] == INF )
dis[edge[i].v] = dis[u] + 1;
que[rear++] = edge[i].v;
vis[edge[i].v] = true;
}
}
}
for ( int i = 1; i < n; i++ )
cout << dis[i] << ' ';
cout << dis[n] << endl;
return 0;
}
Description
Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had n1 footmen and n2 horsemen. Caesar thought that an arrangement is not beautiful if somewhere in the line there are strictly more that k1 footmen standing successively one after another, or there are strictly more than k2 horsemen standing successively one after another. Find the number of beautiful arrangements of the soldiers.
Note that all n1 + n2 warriors should be present at each arrangement. All footmen are considered indistinguishable among themselves. Similarly, all horsemen are considered indistinguishable among themselves.
Input
The only line contains four space-separated integers n1, n2, k1, k2 (1 ≤ n1, n2 ≤ 100, 1 ≤ k1, k2 ≤ 10) which represent how many footmen and horsemen there are and the largest acceptable number of footmen and horsemen standing in succession, correspondingly.
Output
Print the number of beautiful arrangements of the army modulo 100000000 (108). That is, print the number of such ways to line up the soldiers, that no more than k1 footmen stand successively, and no more than k2 horsemen stand successively.
Sample Input
2 1 1 10
1
2 3 1 2
5
2 4 1 1
0
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long ans;
long long dp1[110][110][12];
long long dp2[110][110][12];
int main()
{
int n1,n2,k1,k2,i,j,k;
while(scanf("%d%d%d%d",&n1,&n2,&k1,&k2)!=EOF){
memset(dp1,0,sizeof(dp1)); memset(dp2,0,sizeof(dp2));
dp1[1][0][1]=1; dp2[0][1][1]=1;
for(i=0;i<=n1;i++){
for(j=0;j<=n2;j++){
for(k=1;k<=k1;k++){
if(dp1[i][j][k]){
if(k!=k1){
dp1[i+1][j][k+1]+=dp1[i][j][k];
dp1[i+1][j][k+1]%=100000000;
}
dp2[i][j+1][1]+=dp1[i][j][k];
dp2[i][j+1][1]%=100000000;
}
}
for(k=1;k<=k2;k++){
if(dp2[i][j][k]){
if(k!=k2){
dp2[i][j+1][k+1]+=dp2[i][j][k];
dp2[i][j+1][k+1]%=100000000;
}
dp1[i+1][j][1]+=dp2[i][j][k];
dp1[i+1][j][1]%=100000000;
}
}
}
}
ans=0;
for(i=1;i<=k1;i++){
ans+=dp1[n1][n2][i];
ans%=100000000;
}
for(i=1;i<=k2;i++){
ans+=dp2[n1][n2][i];
ans%=100000000;
}
printf("%I64d\n",ans);
}
return 0;
}
Description
Meg the Rabbit decided to do something nice, specifically — to determine the shortest distance between two points on the surface of our planet. But Meg... what can you say, she wants everything simple. So, she already regards our planet as a two-dimensional circle. No, wait, it's even worse — as a square of side n. Thus, the task has been reduced to finding the shortest path between two dots on a square (the path should go through the square sides). To simplify the task let us consider the vertices of the square to lie at points whose coordinates are:(0, 0), (n, 0), (0, n) and (n, n).
Input
The single line contains 5 space-separated integers: n, x1, y1, x2, y2 (1 ≤ n ≤ 1000, 0 ≤ x1, y1, x2, y2 ≤ n) which correspondingly represent a side of the square, the coordinates of the first point and the coordinates of the second point. It is guaranteed that the points lie on the sides of the square.
Output
You must print on a single line the shortest distance between the points.
Sample Input
2 0 0 1 0
1
2 0 1 2 1
4
100 0 0 100 100
200
#include<stdio.h>
int abs(int a)
{
return a > 0 ? a : (0 - a);
}
int min(int a, int b)
{
return a < b ? a : b;
}
int main()
{
int n, x1, y1, x2, y2;
double midx, midy, centre;
scanf("%d%d%d%d%d", &n, &x1, &y1, &x2, &y2);
if(((x1 == 0 || x1 == n) && (y2 == 0 || y2 == n)) || ((x2 == 0 || x2 == n) && (y1 == 0 || y1 == n)) || ( (x1 == x2 && (x2 == 0 || x2 == n)) || (y1 == y2 && (y2 == 0 || y2 == n)) ))
{
printf("%d\n", abs(x1 - x2) + abs(y1 - y2));
}
else
{
if(x1 == 0 || x2 == 0)
{
printf("%d\n", min(x1 + x2 + y1 + y2 , x1 + x2 + (n - y1) + (n - y2)));
}
else
{
printf("%d\n", min(x1 + x2 + y1 + y2 , y1 + y2 + (n - x1) + (n - x2)));
}
}
//goto aa;
return 0;
}
Description
Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.
We assume that Bajtek can only heap up snow drifts at integer coordinates.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.
Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.
Output
Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.
Sample Input
2 2 1 1 2
1
2 2 1 4 1
0
#include<stdio.h>
int x[110],y[110], n, a;
bool u[110];
void f(int t)
{
int i;
u[t] = true;
for(i = 0;i < n; i++)
if(!u[i]&&(x[i]==x[t]||y[i]==y[t]))
f(i);
}
int main()
{
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
scanf("%d %d",&x[i],&y[i]);
for(i=0;i<n;i++)
if(!u[i])
{
a++;
f(i);
}
printf("%d\n",a-1);
return 0;
}