A - Kefa and Park
Kefa decided to celebrate his first big salary by going to the restaurant.
He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa’s house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.
The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats.
Your task is to help Kefa count the number of restaurants where he can go.
Input
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa.
The second line contains n integers a1, a2, …, an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat).
Next n - 1 lines contains the edges of the tree in the format “xi yi” (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge.
It is guaranteed that the given set of edges specifies a tree.
Output
A single integer — the number of distinct leaves of a tree the path to which from Kefa’s home contains at most m consecutive vertices with cats.
Examples
Input
4 1
1 1 0 0
1 2
1 3
1 4
Output
2
Input
7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7
Output
2
Note
Let us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.
Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can’t go only to the restaurant located at vertex 2.
Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can’t go to restaurants 6, 7.
题目大意:给一棵树,从根结点(1)出发,要到叶子结点去,路上有猫,如果一条路上连续有m只猫,那么这条路就不能走,问有多少条路可以走。
解题思路:暴力dfs,然后记录下连续猫的个数,超过m就重新遍历下一条路。
#include<iostream>
#include<vector>
using namespace std;
const int maxn=1e5+10;
int n,m,ans=0;
vector<int> G[maxn];
int cat[maxn];
bool vis[maxn];
void dfs(int x,int y)
{
if(y>m) return;
vis[x]=1;
int flag=0;
for(int i=0;i<G[x].size();i++)
{
int v=G[x][i];
if(!vis[v])
{
flag=1;
if(cat[v])
{
dfs(v,y+1);
}
else dfs(v,0);
}
}
if(!flag) ans++;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>cat[i];
for(int i=0;i<n-1;i++)
{
int x,y;
cin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1,cat[1]);
cout<<ans<<endl;
return 0;
}
D - Alex and a Rhombus
While playing with geometric figures Alex has accidentally invented a concept of a n-th order rhombus in a cell grid.
A 1-st order rhombus is just a square 1×1 (i.e just a cell).
A n-th order rhombus for all n≥2 one obtains from a n−1-th order rhombus adding all cells which have a common side with it to it (look at the picture to understand it better).
Alex asks you to compute the number of cells in a n-th order rhombus.
Input
The first and only input line contains integer n (1≤n≤100) — order of a rhombus whose numbers of cells should be computed.
Output
Print exactly one integer — the number of cells in a n-th order rhombus.
Examples
Input
1
Output
1
Input
2
Output
5
Input
3
Output
13
Note
Images of rhombus corresponding to the examples are given in the statement.
题目大意:给你一个n,问第n个图的时候有几个正方形。
解题思路:找规律的题,通过找规律发现ans=2*n*n-2*n+1.
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
int main()
{
int n;
cin>>n;
cout<<2*n*n-2*n+1<<endl;
return 0;
}
E - Nick and Array
Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product a1⋅a2⋅…an of its elements seemed to him not large enough.
He was ready to throw out the array, but his mother reassured him. She told him, that array would not be spoiled after the following operation: choose any index i (1≤i≤n) and do ai:=−ai−1.
For example, he can change array [3,−1,−4,1] to an array [−4,−1,3,1] after applying this operation to elements with indices i=1 and i=3.
Kolya had immediately understood that sometimes it’s possible to increase the product of integers of the array a lot. Now he has decided that he wants to get an array with the maximal possible product of integers using only this operation with its elements (possibly zero, one or more times, as many as he wants), it is not forbidden to do this operation several times for the same index.
Help Kolya and print the array with the maximal possible product of elements a1⋅a2⋅…an which can be received using only this operation in some order.
If there are multiple answers, print any of them.
Input
The first line contains integer n (1≤n≤105) — number of integers in the array.
The second line contains n integers a1,a2,…,an (−106≤ai≤106) — elements of the array
Output
Print n numbers — elements of the array with the maximal possible product of elements which can be received using only this operation in some order from the given array.
If there are multiple answers, print any of them.
Examples
Input
4
2 2 2 2
Output
-3 -3 -3 -3
Input
1
0
Output
0
Input
3
-3 -3 2
Output
-3 -3 2
题目大意:给你n个数,每个数字可以进行若干次的变换,变换规则为:ai= - ai -1.经过若干次变换后可以得到的n个数的乘积最大,输出其中一种变换后的结果。
解题思路:首先如果是n=1,那么正数就是最大的,负数的话就变换一次,输出。n>1:如果正数变为负数的时候绝对值会增大,所以可以让所有的都变为负数,如果n的个数为偶数,那么直接输出,如果n的个数为奇数,那么最后乘积会是个负数,所以需要将最小的那个负数再变换一次,然后输出。(我试了变最小和最大,发现最小的负数最后称出来的结果大。这个道理也很容易明白!)
#include<iostream>
using namespace std;
const int maxn=1e5+10;
int a[maxn];
int main()
{
int n,flag,min=1234567;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]>=0)
{
a[i]=-a[i]-1;
}
if(a[i]<min) //zx
{
min=a[i];
flag=i;
}
}
if(n%2==1)
{
a[flag]=-a[flag]-1;
}
for(int i=1;i<=n;i++)
{
if(i!=n) cout<<a[i]<<" ";
else cout<<a[i]<<endl;
}
return 0;
}
这道题当时卡了很久,原因是我一直想从最大的负数变,而最大的负数又出现一个问题就是-1的问题,如果最大的负数是-1,变了变成了0,那么乘积就不是最大了,然后想了很久才发现要从最小的变就直接是正确的。
G - Circle Metro
The circle line of the Roflanpolis subway has n stations.
There are two parallel routes in the subway. The first one visits stations in order 1→2→…→n→1→2→… (so the next stop after station x is equal to (x+1) if x<n and 1 otherwise). The second route visits stations in order n→(n−1)→…→1→n→(n−1)→… (so the next stop after station x is equal to (x−1) if x>1 and n otherwise). All trains depart their stations simultaneously, and it takes exactly 1 minute to arrive at the next station.
Two toads live in this city, their names are Daniel and Vlad.
Daniel is currently in a train of the first route at station a and will exit the subway when his train reaches station x.
Coincidentally, Vlad is currently in a train of the second route at station b and he will exit the subway when his train reaches station y.
Surprisingly, all numbers a,x,b,y are distinct.
Toad Ilya asks you to check if Daniel and Vlad will ever be at the same station at the same time during their journey. In other words, check if there is a moment when their trains stop at the same station. Note that this includes the moments when Daniel or Vlad enter or leave the subway.
Input
The first line contains five space-separated integers n, a, x, b, y (4≤n≤100, 1≤a,x,b,y≤n, all numbers among a, x, b, y are distinct) — the number of stations in Roflanpolis, Daniel’s start station, Daniel’s finish station, Vlad’s start station and Vlad’s finish station, respectively.
Output
Output “YES” if there is a time moment when Vlad and Daniel are at the same station, and “NO” otherwise. You can print each letter in any case (upper or lower).
Examples
Input
5 1 4 3 2
Output
YES
Input
10 2 1 9 10
Output
NO
Note
In the first example, Daniel and Vlad start at the stations (1,3). One minute later they are at stations (2,2). They are at the same station at this moment. Note that Vlad leaves the subway right after that.
Consider the second example, let’s look at the stations Vlad and Daniel are at. They are:
initially (2,9),
after 1 minute (3,8),
after 2 minutes (4,7),
after 3 minutes (5,6),
after 4 minutes (6,5),
after 5 minutes (7,4),
after 6 minutes (8,3),
after 7 minutes (9,2),
after 8 minutes (10,1),
after 9 minutes (1,10).
After that, they both leave the subway because they are at their finish stations, so there is no moment when they both are at the same station.
题目大意:在地铁站,一个人从a往n走,走到n+1就重新回到1开始往n走,另一个人从b往0走,走到0就重新回到n开始往0走,问他们两个人在走的途中能否相遇,能的话输出YES,否则当a走到终点站x或者b走到终点站y的时候输出NO。
解题思路:直接一个for循环遍历就可以了,加上判断条件。
#include<iostream>
using namespace std;
int main()
{
int n,a,x,b,y,flag=0;
cin>>n>>a>>x>>b>>y;
for(int i=a,j=b;;i++,j--)
{
if(i==n+1) i=1;
if(j==0) j=n;
if(i==j)
{
flag=1;
break;
}
if(i==x||j==y) break;
}
if(flag==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
H - Pairs
Toad Ivan has m pairs of integers, each integer is between 1 and n, inclusive. The pairs are (a1,b1),(a2,b2),…,(am,bm).
He asks you to check if there exist two integers x and y (1≤x<y≤n) such that in each given pair at least one integer is equal to x or y.
Input
The first line contains two space-separated integers n and m (2≤n≤300000, 1≤m≤300000) — the upper bound on the values of integers in the pairs, and the number of given pairs.
The next m lines contain two integers each, the i-th of them contains two space-separated integers ai and bi (1≤ai,bi≤n,ai≠bi) — the integers in the i-th pair.
Output
Output “YES” if there exist two integers x and y (1≤x<y≤n) such that in each given pair at least one integer is equal to x or y. Otherwise, print “NO”. You can print each letter in any case (upper or lower).
Examples
Input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
Output
NO
Input
5 4
1 2
2 3
3 4
4 5
Output
YES
Input
300000 5
1 2
1 2
1 2
1 2
1 2
Output
YES
Note
In the first example, you can’t choose any x, y because for each such pair you can find a given pair where both numbers are different from chosen integers.
In the second example, you can choose x=2 and y=4.
In the third example, you can choose x=1 and y=2.
题目大意:给你m对整数,每个数字都在1-n之间,问能否找到两个数字,使得这m对数字中每对都含有这两个数字至少一个。
解题思路:哇哇哇哇哇,这道题我早早都弄明白了怎么写,而且也没有什么难度,但是写着写着就把自己写晕了。刚开始是先找到a[1]和b[1],然后如果是yes那么这两个数至少有一个在a[1]和b[1]里,然后全部遍历,找到一组和这两个数字一个也不一样的组,然后从这四个数字遍历,如果是yes那么一定在这四个数字里,后来发现这样做是不对的。原因是结果是在这四个里边,但是只有两个,所以每次只能用两个去判断,如果四个一块判断,那么我就是找四个和它一样的数字了。所以应该是这四个数a[1]=x1,b[1]=y1,和又找到的两个a[i]=x2,b[i]=y2这四个x1,x2,y1,y2数,两两结合check一次,x2和y2不用check,因为他们就是从上边找到的,check完之后,如果至少有一组都符合就是每组都有至少一个数和它们相等,那么就是YES,否则就是NO.
#include<iostream>
using namespace std;
const int maxn=300010;
int a[maxn],b[maxn];
int n,m,x,y,a1,b1;
bool cmp(int x,int y)
{
for(int i=1;i<=m;i++)
{
if(a[i]!=x&&b[i]!=y&&a[i]!=y&&b[i]!=x) return 0;
}
return 1;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
cin>>a[i]>>b[i];
a1=a[1];
b1=b[1];
for(int i=1;i<=m;i++)
{
if(a[i]!=a1&&a[i]!=b1&&b[i]!=a1&&b[i]!=b1)
{
x=a[i];
y=b[i];
break;
}
}
if(cmp(x,y)||cmp(x,a1)||cmp(x,b1)||cmp(y,a1)||cmp(y,b1)||cmp(a1,b1)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}