zoukankan      html  css  js  c++  java
  • Edge Weight Assignment CodeForces 1339D

    You have unweighted tree of nn vertices. You have to assign a positive weight to each edge so that the following condition would hold:

    • For every two different leaves v1v1 and v2v2 of this tree, bitwise XOR of weights of all edges on the simple path between v1v1 and v2v2 has to be equal to 00.

    Note that you can put very large positive integers (like 10(1010)10(1010)).

    It's guaranteed that such assignment always exists under given constraints. Now let's define ff as the number of distinct weights in assignment.

     In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is 00. ff value is 22 here, because there are 22 distinct edge weights(44 and 55).

     In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex 11 and vertex 66(3,4,5,43,4,5,4) is not 00.

    What are the minimum and the maximum possible values of ff for the given tree? Find and print both.

    Input

    The first line contains integer nn (3n1053≤n≤105) — the number of vertices in given tree.

    The ii-th of the next n1n−1 lines contains two integers aiai and bibi (1ai<bin1≤ai<bi≤n) — it means there is an edge between aiai and bibi. It is guaranteed that given graph forms tree of nn vertices.

    Output

    Print two integers — the minimum and maximum possible value of ff can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints.

    Examples

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

    Note

    In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.

    In the second example, possible assignments for each minimum and maximum are described in picture below. The ff value of valid assignment of this tree is always 33.

    In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.

    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int in[100010], c[100010];
    vector<int> e[100010];
    
    void dfs(int x, int z) {
        c[x] = z;
        for(int i = 0; i < e[x].size(); i++) {
            int y = e[x][i];
            if(c[y] != 0) continue;
            dfs(y, 3 - z);
        }
    }
    
    int main() {
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n - 1; i++) {
            int a, b;
            scanf("%d%d", &a, &b);
            e[a].push_back(b);
            e[b].push_back(a);
            in[a]++;
            in[b]++;
        }
        for(int i = 1; i <= n; i++) {
            if(in[i] == 1) {
                c[e[i][0]]++;
            }
        }
        int ans = n - 1;
        for(int i = 1; i <= n; i++) {
            ans -= max(0, c[i] - 1);
        }
        memset(c, 0, sizeof(c));
        dfs(1, 1);
        int f1 = 0, f2 = 0;
        for(int i = 1; i <= n; i++) {
            if(in[i] == 1) {
                if(c[i] == 1) f1 = 1;
                else f2 = 1;
            }
        }
        int a1;
        if(f1 + f2 == 2) a1 = 3;
        else a1 = 1;
        printf("%d %d\n", a1, ans);
    
        return 0;
    }
  • 相关阅读:
    Spring Boot 的常用 API 说明
    错误:No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?
    [剑指offer] 2. 替换空格
    [剑指offer] 1. 二维数组中的的查找
    [leetcode] 300. Longest Increasing Subsequence (Medium)
    [leetcode] 929. Unique Email Addresses (easy)
    微信小程序post 服务端无法获得参数问题
    python虚拟环境管理 Pipenv 使用说明
    [leetcode] 87. Scramble String (Hard)
    [leetcode] 456. 132 Pattern (Medium)
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12703193.html
Copyright © 2011-2022 走看看