codeforces解题报告7.9

1.

The nation of Panel holds an annual show called The Number Games, where each district in the nation will be represented by one contestant.

The nation has n districts numbered from 1 to n, each district has exactly one path connecting it to every other district. The number of fans of a contestant from district i is equal to 2i.
This year, the president decided to reduce the costs. He wants to remove k contestants from the games. However, the districts of the removed contestants will be furious and will not allow anyone to cross through their districts.

The president wants to ensure that all remaining contestants are from districts that can be reached from one another. He also wishes to maximize the total number of fans of the participating contestants.

Which contestants should the president remove?

Input
The first line of input contains two integers n and k (1≤k<n≤106) — the number of districts in Panel, and the number of contestants the president wishes to remove, respectively.
The next n−1 lines each contains two integers a and b (1≤a,b≤n, a≠b), that describe a road that connects two different districts a and b in the nation. It is guaranteed that there is exactly one path between every two districts.
Output
Print k space-separated integers: the numbers of the districts of which the contestants should be removed, in increasing order of district number.
Examples
Input
6 3
2 1
2 6
4 2
5 6
2 3
Output
1 3 4
Input
8 4
2 6
2 7
7 8
1 2
3 1
2 4
7 5
Output
1 3 4 5
Note

In the first sample, the maximum possible total number of fans is 22+25+26=100. We can achieve it by removing the contestants of the districts 1, 3, and 4.

有一张图, 选择联通的n个点,编号为i的点的价值是2的i次方。由此我们可以很轻松地想出贪心,每次选编号最大的,就算放弃最大的,选择其他的所有点价值都没有只选择最大的一个点大。由最大点建树(好像任意的点都可以),用倍增跑本点与编号下面的一点的lca,把这条路径上的点都涂色,算出涂色的数量。如果不够,再选择次小编号的点。(因为每一个点都致多涂色一次 所以不会TLE)

2.

You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.


Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.


If a solution exists, you should print it.


Input
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.


Output
Print "NO" (without quotes), if there is no such way to remove some digits from number n.


Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.


If there are multiple possible answers, you may print any of them.


Examples
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output

NO

给一个100位的整数,去除几个位上的数使其可以被8整除。对于一个4位及以上的数,它能否被8整除取决于最后3位,所以暴力扫1,2,3位的子序列就行了。

```cpp

#include<bits/stdc++.h>
using namespace std;
char a[110];
int main()
{
    cin>>a;
    int len = strlen(a);
    for(int k = 0; k < len; k++)
            {
                int sum = a[k]-'0';
                if(sum%8 == 0){
                    printf("YES\n");
                    printf("%d", sum);
                    return 0;
                }
            }
    for(int j = 0; j < len - 1; j++)
            for(int k = j + 1; k < len; k++)
            {
                int sum = (a[j]-'0')*10 + a[k]-'0';
                if(sum%8 == 0){
                    printf("YES\n");
                    printf("%d", sum);
                    return 0;
                }
            }
    for(int i = 0; i < len - 2; i++)
    {
        for(int j = i + 1; j < len - 1; j++)
            for(int k = j + 1; k < len; k++)
            {
                int sum = (a[i]-'0')*100 + (a[j]-'0')*10 + a[k]-'0';
                if(sum%8 == 0){
                    printf("YES\n");
                    printf("%d", sum);
                    return 0;
                }
            }
    }
    printf("NO\n");
    return 0;

}

```

3.An undirected graph is called k-regular, if the degrees of all its vertices are equal k. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components.


Build a connected undirected k-regular graph containing at least one bridge, or else state that such graph doesn't exist.


Input
The single line of the input contains integer k (1 ≤ k ≤ 100) — the required degree of the vertices of the regular graph.


Output
Print "NO" (without quotes), if such graph doesn't exist.


Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines.


The description of the made graph must start with numbers n and m — the number of vertices and edges respectively.


Each of the next m lines must contain two integers, a and b (1 ≤ a, b ≤ n, a ≠ b), that mean that there is an edge connecting the vertices a and b. A graph shouldn't contain multiple edges and edges that lead from a vertex to itself. A graph must be connected, the degrees of all vertices of the graph must be equal k. At least one edge of the graph must be a bridge. You can print the edges of the graph in any order. You can print the ends of each edge in any order.


The constructed graph must contain at most 106 vertices and 106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most 106 vertices and at most 106 edges).


Examples
Input
1
Output
YES
2 1
1 2
Note

In the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge.

构造一个图,使其每一个点的度都为n。且有一个桥。

首先偶数n是不可能的。我们建立2两排点,每排n-1个,用第一排的每一个点向第二排的每一个点连边。这样第一排每个点的度为n-1.然后将第二排的每一个偶数点对每一个基数点连边。这样因为每一个第二排的点都与第一排的点连过边了,所以度为n-1,再两两连边所以度为n。 现在第一排的每个点的入度都少1, 我们用一个点去连第一排的每一个点。把这幅图对称生成一个一某一样的图,链接我们用来连接第一排的那两个点即可。

```cpp

#include<bits/stdc++.h>
using namespace std;
int k;
void dfs(int x)
{
    for(int i = 2; i <= k; i++){
        printf("%d %d\n", 1 + x, i + x);
    }
    for(int i = 2; i <= k; i++){
        for(int j = k + 1; j <= 2*k-1; j++){
            printf("%d %d\n", i + x, j + x);
        }
    }
    if(k != 1)
    for(int i = k + 1; i <= 2*k-1; i++)
    if(i%2==0) printf("%d %d\n", i+x, i+1+x);
}
int main()
{
    scanf("%d", &k);
    if(k % 2 == 0){
        printf("NO\n");
        return 0;
    }
    int n = (2*(k-1) + 1)*2;
    int m = n/2 *k;
        printf("YES\n");
        printf("%d %d\n", n, m);
    printf("%d %d\n", 1, 2*k);
    dfs(0);
    dfs(2 * k - 1);
    return 0;

}

```


猜你喜欢

转载自blog.csdn.net/gjc2561571372/article/details/80972211