Codeforces Round #453 (Div. 2) A-C


A;

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.


123

【思路】

flag标记起点0 是否涵盖, IN控制能到达的最远距离

int main()
{
    int n,m;
    int a,b;
    scanf("%d %d",&n,&m);int flag=1;
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&a,&b);
        if(a==0&&b>=sum)
        {
            flag=0;
            sum=b;
        }
        if(b>=sum&&a<=sum)
            sum=b;
    }
    if(flag)
        cout<<"NO"<<endl;
    else
    {
        if(sum>=m)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}


B:

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:


【思路】

循环节,找树的分支,从底部扫描到顶部,如果节点与父节点不同ans++

int a[MAXN];
int b[MAXN],vis[MAXN];
int main()
{
    int n;
    map<int,int>mp;
    scanf("%d",&n);
    mem(vis,0);
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&b[i]);
    }
    int ans=1;
    for(int i=n;i>=2;i--)
    {
        if(!vis[i])
        {
            int pre=i;
            vis[pre]=1;
            while(pre!=1)
            {
               if(b[pre]!=b[a[pre]]&&!mp[pre])
                {
                    ans++;
                   // cout<<pre<<endl;
                    mp[pre]=pre;
                }
                pre=a[pre];
                vis[pre]=1;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


C;

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

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:


【思路】

特例:

int a[MAXN];
int b[maxn][2];
int main()
{
    int h;
    scanf("%d",&h);
    int flag=1;
    int sum=0;
    for(int i=1;i<=h+1;i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    int cont=0;
    for(int i=1;i<=h+1;i++)
    {
    	if(a[i]>=2)
    		cont++;
    	else
    		cont=0;
    	if(cont>=2)
    	{
    		flag=0;
    		break;
		}
	}
    if(flag)
    {
        cout<<"perfect"<<endl;
        return 0;
    }
    cout<<"ambiguous"<<endl;

    mem(b,0);
    int k=0;
    for(int i=1;i<=h+1;i++)
    {
        for(int j=1;j<=a[i];j++)
        {
            cout<<k<<" ";

        }
         k+=a[i];
    }
    cout<<endl;
    k=0;
    int flag2=1,fflag;
    for(int i=1;i<=h+1;i++)
    {
        for(int j=1;j<=a[i];j++)
        {
            if(a[i-1]>=2&&flag2&&a[i]>=2)
            {
                cout<<k-1<<" ";
                flag2=0;
            }
            else
            {
                cout<<k<<" ";
            }
        }
        k+=a[i];
    }
    cout<<endl;

    return 0;
}


posted @ 2017-12-20 07:45  Sizaif  阅读(253)  评论(0编辑  收藏  举报