Pig is visiting a friend.
Pig's house is located at point 0, and his friend's house is located at point m on an axis.
Pig can use teleports to move along the axis.
To use a teleport, Pig should come to a certain point (where the teleport is located) and choose where to move: for each teleport there is the rightmost point it can move Pig to, this point is known as the limit of the teleport.
Formally, a teleport located at point x with limit y can move Pig from point x to any point within the segment [x; y], including the bounds.
Determine if Pig can visit the friend using teleports only, or he should use his car.
The first line contains two integers n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of teleports and the location of the friend's house.
The next n lines contain information about teleports.
The i-th of these lines contains two integers ai and bi (0 ≤ ai ≤ bi ≤ m), where ai is the location of the i-th teleport, and bi is its limit.
It is guaranteed that ai ≥ ai - 1 for every i (2 ≤ i ≤ n).
Print "YES" if there is a path from Pig's house to his friend's house that uses only teleports, and "NO" otherwise.
You can print each letter in arbitrary case (upper or lower).
3 5 0 2 2 4 3 5
YES
3 7 0 4 2 5 6 7
NO
The first example is shown on the picture below:
Pig can use the first teleport from his house (point 0) to reach point 2, then using the second teleport go from point 2 to point 3, then using the third teleport go from point 3 to point 5, where his friend lives.
The second example is shown on the picture below:
You can see that there is no path from Pig's house to his friend's house that uses only teleports.
题解:这水题连WA两发,简直失了智,WA的数据是3,5,区间是(0,4)(1,2)(3,5)这组数据结果是YES。所以跑数据的时候,要更新下最右边的边界maxx,当下一组坐标的右边界小于maxx的时候,直接用maxx覆盖掉下组坐标的右边界,其他数据就用区间覆盖的代码就可以了。
代码
#include
#include
#define M 105
using namespace std;
struct node
{
int x;
int y;
}a[M];
int main()
{
int n, m;
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++)
{
scanf("%d %d", &a[i].x, &a[i].y);
}
/*if(a[n - 1].y < m)
{
printf("NO\n");
return 0;
}*/
if(a[0].x > 0)
{
printf("NO\n");
return 0;
}
int maxx = a[0].y;
for(int i = 0; i < n - 1; i++)
{
maxx = max(maxx, a[i].y);
if(a[i + 1].y < maxx)a[i + 1].y = maxx;
if(a[i].y < a[i + 1].x)
{
printf("NO\n");
return 0;
}
}
maxx = max(maxx, a[n - 1].y);
//printf("maxx = %d", maxx);
if(maxx < m)
{
printf("NO\n");
return 0;
}
printf("YES\n");
return 0;
}
You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1.
Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0.
You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x.
It is guaranteed that you have to color each vertex in a color different from 0.
You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory).
The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree.
The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi.
The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into.
It is guaranteed that the given graph is a tree.
Print a single integer — the minimum number of steps you have to perform to color the tree into given colors.
6 1 2 2 1 5 2 1 1 1 1 1
3
7 1 1 2 3 1 4 3 3 1 1 1 2 3
5
The tree from the first sample is shown on the picture (numbers are vetices' indices):
On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors):
On seond step we color all vertices in the subtree of vertex 5 into color 1:
On third step we color all vertices in the subtree of vertex 2 into color 1:
The tree from the second sample is shown on the picture (numbers are vetices' indices):
On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors):
On second step we color all vertices in the subtree of vertex 3 into color 1:
On third step we color all vertices in the subtree of vertex 6 into color 2:
On fourth step we color all vertices in the subtree of vertex 4 into color 1:
On fith step we color all vertices in the subtree of vertex 7 into color 3:
题解:把每个点都染成需要的颜色,每染一个点,它的子树都会被染成相同的颜色。dfs就可以过,搜到每个点的时候,比较该点需要染成的颜色和它父亲节点的颜色,如果不同,染色数++;
代码
#include
#include
#include
#define M 20010
using namespace std;
int a[M];
vector g[M];
bool vis[M];
int c[M];
int cnt;
void dfs(int node, int color)
{
//printf("1");
vis[node] = true;
for(int i = 0; i < g[node].size(); i++)
{
//printf("2");
if(!vis[g[node][i]])
{
if(c[g[node][i]] != color)
{
cnt++;
dfs(g[node][i], c[g[node][i]]);
}
else dfs(g[node][i], color);
}
}
return;
}
int main()
{
int n;
scanf("%d", &n);
memset(vis, false, sizeof(vis));
for(int i = 2; i <= n; i++)
{
scanf("%d", &a[i]);
g[a[i]].push_back(i);
g[i].push_back(a[i]);
}
/*for(int j = 1; j <= n; j++)
{
for(int i = 0; i < g[j].size(); i++)
printf("%d ", g[j][i]);
printf("\n");
}*/
for(int i = 1; i <= n; i++)
scanf("%d", &c[i]);
cnt = 1;
dfs(1, c[1]);
printf("%d\n", cnt);
}
Sasha is taking part in a programming competition. In one of the problems she should check if some rooted trees are isomorphic or not. She has never seen this problem before, but, being an experienced participant, she guessed that she should match trees to some sequences and then compare these sequences instead of trees. Sasha wants to match each tree with a sequence a0, a1, ..., ah, where his the height of the tree, and ai equals to the number of vertices that are at distance of i edges from root.
Unfortunately, this time Sasha's intuition was wrong, and there could be several trees matching the same sequence. To show it, you need to write a program that, given the sequence ai, builds two non-isomorphic rooted trees that match that sequence, or determines that there is only one such tree.
Two rooted trees are isomorphic, if you can reenumerate the vertices of the first one in such a way, that the index of the root becomes equal the index of the root of the second tree, and these two trees become equal.
The height of a rooted tree is the maximum number of edges on a path from the root to any other vertex.
The first line contains a single integer h (2 ≤ h ≤ 105) — the height of the tree.
The second line contains h + 1 integers — the sequence a0, a1, ..., ah (1 ≤ ai ≤ 2·105). The sum of all ai does not exceed 2·105. It is guaranteed that there is at least one tree matching this sequence.
If there is only one tree matching this sequence, print "perfect".
Otherwise print "ambiguous" in the first line. In the second and in the third line print descriptions of two trees in the following format: in one line print integers, the k-th of them should be the parent of vertex k or be equal to zero, if the k-th vertex is the root.
These treese should be non-isomorphic and should match the given sequence.
2 1 1 1
perfect
2 1 2 2
ambiguous 0 1 1 3 3 0 1 1 3 2
The only tree in the first example and the two printed trees from the second example are shown on the picture:
代码
#include
#define M 100010
int a[M];
int main()
{
int h;
scanf("%d", &h);
int sum = 0;
for(int i = 0; i <= h; i++)
{
scanf("%d", &a[i]);
sum += a[i];
}
bool flag = false;
for(int i = 0; i < h; i++)
{
if(a[i] > 1 && a[i + 1] > 1)
{
flag = true;
break;
}
}
if(!flag)
{
printf("perfect\n");
return 0;
}
printf("ambiguous\n");
int node = 0;
int pos = 1;
for(int i = 0; i <= h; i++)
{
for(int j = 1; j <= a[i]; j++)
{
printf("%d%c", node, pos == sum ? '\n' : ' ');
pos++;
}
node = pos - 1;
}
node = 0;
pos = 1;
int x = 0;
for(int i = 0; i <= h; i++)
{
for(int j = 1; j <= a[i]; j++)
{
printf("%d%c", j % 2 ? node : x, pos == sum ? '\n' : ' ');
pos++;
}
node = pos - 1;
x = pos - a[i];
}
return 0;
}