Codeforces Round # 633 (Div. 2) D. Edge Weight Assignment thinking or guessing

https://codeforces.ml/contest/1339/problem/D

Given an unweighted tree of n vertices, you now need to assign a positive weight to each edge, satisfying the condition: the simple path between every two different leaf nodes in the tree. All edge weights have a bitwise XOR value of 0.

You can put a large (infinite) number, for example: 10 (1010) ,,,

You can ensure that such assignments exist under the given constraints.

Define f as the number of different edge weights on a simple path between leaf nodes.

Find the minimum and maximum values ​​of f.

E.g:

Example 1.

 

 The number of different edge weights between the first tree leaf node 1-> 6 is 1 (1), all f = 1.

 The number of different edge weights between the leaf nodes 1-> 6 of the second tree is 4 (1,2,4,7), all f = 4.

f The minimum value is 1 and the maximum value is 4.

Example 2.

 

f The minimum and maximum values ​​are 3.

 Idea: First think about the minimum value, the even number of the same number XOR is 0, then the edge weights are the same when the number of edges between the two leaf nodes is even, so the minimum value of f is 1; when the number of edges appears odd , The XOR value of three identical numbers must not be 0, and the XOR of two identical numbers and a different number must also not be 0, then at least three XORs of different numbers, such as 5 : Then the XOR of two identical numbers is 0, and the XOR of three different numbers is XOR, so that the XOR value is 0, such as: 1 ^ 1 ^ 1 ^ 2 ^ 3. So when the number of edges is odd, f is minimum The value is 3 (impossible number of edges is 1).

Think about the maximum value: if the number of edges is 2, these two numbers must be equal. If the number of edges is not 2, f is equal to the number of edges (I do n’t know why, guess, although the tree may grow very complicated and very strange , But you should always be able to find out how many XORs are equal to 0 ?? The question specifically stated that the weight can be very large, hhhhhh ,, if there is a big brother who can prove it, please tell me).

Summary: The minimum value only needs to determine whether there are odd number of edges between leaf nodes, all even number is 1, and odd number is 3;

The maximum value only needs to find the leaf node with the number of edges 2, and then subtract, first set f to the total number of edges (n-1), the number of leaf nodes connected to the same root node is sum, then f = f- (sum-1) (sum≥2).

#include <bits/stdc++.h>
using namespace std;
const int MAXN=4e5+5;
const int mod=1e9+7;
typedef long long ll;
//typedef __int128 LL;
const int inf=0x3f3f3f3f;
const long long INF=0x3f3f3f3f3f3f3f3f;
vector<int>g[MAXN];
int vis[MAXN];
int len=0;//The depth of the tree, as long as the distance between the vertex and the leaf is odd, then the number of edges of the two leaf nodes must be odd. Similarly, if all are even, there must be no odd number of edges, so there is no need to calculate every two leaves The distance of the node is 
int ansmax, ansmin = 1 ;
 void dfs ( int u) 
{ 
    int sum = 0 ; // Number of leaf nodes with the same root 
    for ( int i = 0 ; i <g [u] .size (); i ++ ) 
    { 
        int v = g [u] [i];
         if (vis [v] == 1 ) continue ; 
        len ++ ; 
        vis [v] = 1 ;
         if (g [v] .size () == 1  )
        {
            sum++;
            if(len%2&&len>=3)ansmin=3;
        }
        dfs(v);
        len--;
    }
    if(sum>=2||len==1)
    {
        if(len==1)ansmax=ansmax-sum;
        else ansmax=ansmax-sum+1;
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    int u,v;
    for(int i=1;i<=n-1;i++)
    {
        v,u;
        scanf("%d%d",&v,&u);
        g[v].push_back(u);
        g[u].push_back(v);
    }
    for(int i=1;i<=n;i++)
    {
        if(g[i].size()==1)
        {
            u=i;//以叶子节点作为顶点
            break;
        }
    }
    ansmax=n-1;
    vis[u]=1;
    dfs(u);
    printf("%d %d\n",ansmin,ansmax);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/MZRONG/p/12690971.html