• B. Mahmoud and Ehab and the bipartiteness


    B. Mahmoud and Ehab and the bipartiteness
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 ≤ n, u ≠ 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).

    直接bfs遍历一遍就成了.

     1 #include <bits/stdc++.h>
     2 #define N 100005
     3 #define ll long long int
     4 using namespace std;
     5 int n;
     6 vector<ll> v[N];
     7 ll vis=0,p=0;
     8 void dfs(ll x,ll fa){
     9   if(vis%2==0){
    10     p++;
    11   }
    12   for(int i=0;i<v[x].size();i++){
    13     if(v[x][i]==fa)
    14       continue;
    15     vis++;
    16     dfs(v[x][i],x);
    17     vis--;
    18   }
    19 }
    20 int main(){
    21   scanf("%d",&n);
    22   ll q=0;
    23   for(int i=1;i<n;i++){
    24     ll x,y;
    25     scanf("%lld%lld",&x,&y);
    26     v[x].push_back(y);
    27     v[y].push_back(x);
    28     if(i==1)
    29       q=x;
    30   }
    31   dfs(q,-1);
    32   ll sum=p*(n-p)-n+1;
    33   printf("%lld
    ",sum);
    34   return 0;
    35 }
  • 相关阅读:
    python目录操作shutil
    python os.walk
    利用华为eNSP模拟器实现vlan之间的通信
    Python之道1-环境搭建与pycharm的配置django安装及MySQL数据库配置
    利用Excel做一些简单的数据分析
    Django中的枚举类型
    django使用model创建数据库表使用的字段
    ps 命令的十个简单用法
    goinception安装
    docker安装redis 指定配置文件且设置了密码
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/7562453.html
Copyright © 2020-2023  润新知