Codeforces Round #453 (Div. 2)(ABC)

A. Visiting a Friend
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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).

Output

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).

Examples
Input
3 5
0 2
2 4
3 5
Output
YES
Input
3 7
0 4
2 5
6 7
Output
NO
Note

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.

这个样例讲的很清楚,直接走一遍顺便标记一下就行了。

 1 #include <iostream>
 2 
 3 using namespace std;
 4 int n,m;
 5 int k[11000];
 6 
 7 int main(){
 8     k[0]=1;
 9     cin>>n>>m;
10     while(n--){
11         int x,y;
12         cin>>x>>y;
13         if(k[x]){
14             for(int i=x;i<=y;i++)
15                 k[i]=1;
16         }
17     }
18     if(k[m]){
19         cout<<"YES"<<endl;
20     }else{
21         cout<<"NO"<<endl;
22     }
23     return 0;
24 }
B. Coloring a Tree
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

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.

Output

Print a single integer — the minimum number of steps you have to perform to color the tree into given colors.

Examples
Input
6
1 2 2 1 5
2 1 1 1 1 1
Output
3
Input
7
1 1 2 3 1 4
3 3 1 1 1 2 3
Output
5
Note

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:

下面是借了别人的题意,我不想写了。

最开始我看到题的时候,题都读的懵逼的,思路也没有。看了博客都是写的搜索,搜索树的层次然后颜色的改变,又不想写那么长,看了q神代码,还有点不能理解,就去问了学长,就明白了,用数组处理代码更简短,用到搜索的思想。开2个数组,a数组保存给的路径,也就是当前节点的父节点,b数组保存每个节点的颜色。在扫一遍,判断当前节点是不是和当前节点的父节点颜色相同,不同的话,就代表当前节点需要涂色,cnt++,简化了搜索过程中每一个父节点改变,子节点就要变的过程。

然后就直接加上我的代码。

C++

 1 #include <iostream>
 2 #include <vector>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 int k[10005];
 7 int n;
 8 vector<int> v[10005];
 9 int vis[10005],over[10005];
10 int cnt=0;
11 void dfs(int x){
12     vis[x]=1;
13     if(over[x]!=k[x]){
14         over[x] = k[x];
15         cnt++;
16     }
17     for(int i = 0; i < v[x].size(); i++){
18         if(!vis[v[x][i]]){
19             over[v[x][i]] = over[x];
20             dfs(v[x][i]);
21         }
22     }
23 }
24 int main(){
25     cin>>n;
26     int x;
27     for(int i=2;i<=n;i++){
28         cin>>x;
29         v[x].push_back(i);
30     }
31     memset(vis,0,sizeof(vis));
32     for(int i=1;i<=n;i++)
33         cin>>k[i];
34     dfs(1);
35     cout<<cnt<<endl;
36     return 0;
37 }

java写的是另一种思路,仔细体会。

JAVA

 1 import java.util.Scanner;
 2 
 3 public class Main{
 4     public static void main(String []args){
 5         int k[] = new int[10005];
 6         int ans[]= new int [10005];
 7         int n,cnt=0;
 8         Scanner in = new Scanner(System.in);
 9         n = in.nextInt();
10         for(int i=2;i<=n;i++){
11             k[i] = in.nextInt();
12         }
13         for(int i=1;i<=n;i++){
14             ans[i] = in.nextInt();
15         }
16         for(int i=1;i<=n;i++){
17             if(ans[i]!=ans[k[i]]){
18                 cnt++;
19             }
20         }
21         System.out.println(cnt);
22     }
23 }
C. Hashing Trees
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 h is 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.

Input

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.

Output

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.

Examples
Input
2
1 1 1
Output
perfect
Input
2
1 2 2
Output
ambiguous
0 1 1 3 3
0 1 1 3 2
Note

The only tree in the first example and the two printed trees from the second example are shown on the picture:

这题只要看懂了,就好做,一开始给你树的高度,然后给你每层树有多少个节点,

显然只要有两个连续大于一的节点个数,就不是完美的。

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 int n;
 6 int k[200005];
 7 int vis[200005];
 8 int main(){
 9 
10     while(cin>>n){
11         bool prime = false;
12         int ans,cnt;
13         memset(k,0,sizeof(k));
14         memset(vis,0,sizeof(vis));
15         for(int i=0;i<=n;i++){
16             cin>>k[i];
17         }
18         for(int i=0;i<n;i++){
19             if(k[i]>1&&k[i+1]>1){
20                 prime = true;
21                 ans = i+1;
22                 break;
23             }
24         }
25         if(prime){
26             cout<<"ambiguous"<<endl;
27         }else{
28             cout<<"perfect"<<endl;
29             continue;
30         }
31         int x=0;
32         for(int i=0;i<=n;i++){
33             for(int j=x;j<=(x+k[i]-1);j++){
34                 vis[j]=x;
35             }
36             x+=k[i];
37         }
38         for(int i=0;i<x;i++)
39             cout<<vis[i]<<" ";
40         cout<<endl;
41         int sum = 0;
42         for(int i=0;i<=ans;i++)
43             sum+=k[i];
44         vis[sum-1] = sum-k[ans]-k[ans-1]+1;
45         for(int i=0;i<x;i++)
46             cout<<vis[i]<<" ";
47         cout<<endl;
48     }
49     return 0;
50 }
原文地址:https://www.cnblogs.com/zllwxm123/p/8116534.html