Codeforces Round #395 (Div. 2) 题解 C D

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34271269/article/details/54849273

C题 :

C. Timofey and a tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the i-th vertex gets color ci.

Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.

Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.

A subtree of some vertex is a subgraph containing that vertex and all its descendants.

Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed.

Input

The first line contains single integer n (2 ≤ n ≤ 105) — the number of vertices in the tree.

Each of the next n - 1 lines contains two integers u and v (1 ≤ u, v ≤ nu ≠ v), denoting there is an edge between vertices u and v. It is guaranteed that the given graph is a tree.

The next line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 105), denoting the colors of the vertices.

Output

Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him.

Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.

Examples
input
4
1 2
2 3
3 4
1 2 1 1
output
YES
2
input
3
1 2
2 3
1 2 3
output
YES
2
input
4
1 2
2 3
3 4
1 2 1 2
output
NO
 
   
思路 这个题目刚开始想到用暴力的方法做 有人在群里也说暴力可做 但是我们还有神奇的fst啊 怎么可能让你过呀 哈哈 

这个题目仔细想想 其实不难 只不过大家一想到树都想歪了 

思路:如果我们能够从边的角度考虑问题就好了 我们可以想 有一种特殊的边是两个点颜色不相同 如果符合题意 那么这种点只能和root相连 

如何高效呢? 那么我们遍历一遍边 找出这种特殊边的数量 然后再遍历每个节点 看看与其相连的特殊边有多少 如果正好相等 那么这个点就可以作为root了

ac代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
using namespace std;
const int maxn=1e5+10;
int tot=0,n;
vector<int> q[maxn];
int color[maxn];
int num[maxn];
int main()
{
    cin>>n;
    int u,v;
    for(int i=1;i<=n-1;i++)
    {
        cin>>u>>v;
        q[u].push_back(v);
        q[v].push_back(u);
    }
    for(int i=1;i<=n;++i)
    {
        cin>>u;
        color[i]=u;
    }
    for(int i=1;i<=n;++i)
    {
        for(int j=0;j<q[i].size();++j)
        {
            if(color[i]!=color[q[i][j]])
            {
                tot++;
            }
        }
    }
    tot/=2;
    for(int i=1;i<=n;++i)
    {
        for(int j=0;j<q[i].size();++j)
        {
            if(color[i]!=color[q[i][j]]) num[i]++;
        }
        if(num[i]==tot)
        {cout<<"YES"<<endl<<i;return 0;}
    }
    cout<<"NO";
    return 0;
}

D题:

D. Timofey and rectangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.

Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.

Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length

The picture corresponds to the first example
Input

The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of rectangles.

n lines follow. The i-th of these lines contains four integers x1y1x2 and y2 ( - 109 ≤ x1 < x2 ≤ 109 - 109 ≤ y1 < y2 ≤ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle.

It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.

Output

Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color.

Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≤ ci ≤ 4) — the color of i-th rectangle.

Example
input
8
0 0 5 3
2 -1 5 0
-3 -4 2 -1
-1 -1 2 0
-3 0 0 5
5 2 10 3
7 -3 10 2
4 -2 7 -1
output
YES
1
2
2
3
2
2
4
1

这个题目如果没有条件的话。。。那真做不了 但是他给了一个很好的条件

每条边边长是奇数 这样给了我们一个想法

我们知道了左下角然后又知道边长是奇数 那么我们就可以得出只有这几种矩形存在 

左下角 : x奇数 y奇数

x奇数 y偶数

x偶数 y奇数 

x偶数 y偶数

如果接触的话 第一种只能和第四种接触 第二种只能和第三种接触 第三种只能和第二种接触 第四种只能和第一种接触 

所以这个题目 右上角坐标根本没用。。。

ac代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int x[500005],y[500005];
int n,i,a,b;
int main()
{
    cin>>n;
    for(i=0;i<n;i++)
        cin>>x[i]>>y[i]>>a>>b;
    cout<<"YES"<<endl;
    for(i=0;i<n;i++)
    {
        if(x[i]&1&&y[i]&1)
            cout<<1<<endl;
        else if(x[i]&1&&!(y[i]&1))
            cout<<2<<endl;
        else if(!(x[i]&1)&&y[i]&1)
            cout<<3<<endl;
        else
            cout<<4<<endl;
    }
    return 0;  
}




猜你喜欢

转载自blog.csdn.net/qq_34271269/article/details/54849273