zoukankan      html  css  js  c++  java
  • E

    Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.

    A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.

    Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?

    A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .

    Input

    The first line of input contains an integer n — the number of nodes in the tree (1 ≤ n ≤ 105).

    The next n - 1 lines contain integers u and v (1 ≤ u, v ≤ nu ≠ v) — the description of the edges of the tree.

    It's guaranteed that the given graph is a tree.

    Output

    Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.

    Examples

    Input
    3
    1 2
    1 3
    Output
    0
    Input
    5
    1 2
    2 3
    3 4
    4 5
    Output
    2

    Note

    Tree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)

    Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graph

    In the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.

    In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).

    题意:给一个n个节点的,n-1条边,现在规定这个图拆成二分图,然后让你添加边,使他依然是二分图,问最多可以添加多少

    思路:首先我们要分二分图,我采用了黑白染色,算出分别两边的节点数,然后我们可以得知,要加的边肯定就是剩下的黑白没有连边的点

    公式: 总节点数-黑色节点数-当前黑色节点所连的白色节点数

    然后累加所有的黑色节点值

    #include<cstdio>
    #include<cmath>
    #include<vector>
    #include<cstring>
    using namespace std;
    vector<int> d[100001];
    int n;
    int c[100001];
    int vis[100001];
    void dfs(int x,int y)//进行黑白染色
    {
        for(int i=0;i<d[x].size();i++)
        {
            if(vis[d[x][i]]==0)
            {
                vis[d[x][i]]=1;
                c[d[x][i]]=y;
                dfs(d[x][i],!y);
            }
        }
    }
    int main()
    {
        scanf("%d",&n);
        int x,y;
        for(int i=1;i<=n-1;i++)
        {
            scanf("%d%d",&x,&y);
            d[x].push_back(y);
            d[y].push_back(x);
        }
        vis[1]=1;
        dfs(1,1);
        long long sum=0;
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(c[i]) 
                c[cnt++]=i;
        }
        for(int i=0;i<cnt;i++)//公式计算
        {
            sum+=n-cnt-d[c[i]].size();
        }
        printf("%lld",sum);
    }
  • 相关阅读:
    Centos7 下安装python3及卸载
    centos python3 的 卸载 删除
    Centos7安装python3和pip3(一)
    python升级带来的yum异常(解决错误File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:)
    python 错误信息是:sudo :apt-get:command not found
    优化页面访问速度(四) ——前端优化
    优化页面访问速度(三) ——服务端优化
    优化页面访问速度(二) ——数据库优化
    优化页面访问速度(一)——综述
    Redis专题——Redis管理工具
  • 原文地址:https://www.cnblogs.com/Lis-/p/9807433.html
Copyright © 2011-2022 走看看