zoukankan      html  css  js  c++  java
  • codeforces 862B B. Mahmoud and Ehab and the bipartiteness

    http://codeforces.com/problemset/problem/862/B

    题意:

    给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且此图还是一个二分图。

    思路:

    想得比较复杂了。。。。其实既然已经给出了二分图并且有n-1条边,那么我们就一定可以用染色法对每一个点进行染色,从而将点划分为两个集合,然后从两个集合中分别任意选择一个点连边就行了。

    一开始考虑二分图的基本属性是不存在奇数条边的环。。。并不需要这样,因为两个集合是分开的,从两个中分别任意选择一个连边,是肯定不会造成同一集合中的两点有边相连的。

    代码:

     1 #include <stdio.h>
     2 #include <vector>
     3 using namespace std;
     4 
     5 vector<int> v[100005];
     6 bool vis[100005];
     7 
     8 int color[100005];
     9 
    10 void dfs(int s)
    11 {
    12     vis[s] = 1;
    13 
    14     if (color[s] == 0) color[s] = 1;
    15 
    16     for (int i = 0;i < v[s].size();i++)
    17     {
    18         int to = v[s][i];
    19 
    20         if (!vis[to])
    21         {
    22             if (color[s] == 1) color[to] = 2;
    23             else color[to] = 1;
    24 
    25             vis[to] = 1;
    26             dfs(to);
    27         }
    28     }
    29 }
    30 
    31 int main()
    32 {
    33     int n;
    34 
    35     scanf("%d",&n);
    36 
    37     for (int i = 0;i < n - 1;i++)
    38     {
    39         int x,y;
    40 
    41         scanf("%d%d",&x,&y);
    42 
    43         v[x].push_back(y);
    44         v[y].push_back(x);
    45     }
    46 
    47     dfs(1);
    48 
    49     long long cnt1 = 0,cnt2 = 0;
    50 
    51     for (int i = 1;i <= n;i++)
    52     {
    53         if (color[i] == 1) cnt1++;
    54         else cnt2++;
    55     }
    56 
    57     printf("%I64d
    ",cnt1 * cnt2 - (n - 1));
    58 
    59     return 0;
    60 }

    PS:记得要用long long,要不会wa。

  • 相关阅读:
    Html禁止粘贴 复制 剪切
    表单标签
    自构BeanHandler(用BeansUtils)
    spring配置中引入properties
    How Subcontracting Cockpit ME2ON creates SD delivery?
    cascadia code一款很好看的微软字体
    How condition value calculated in sap
    Code in SAP query
    SO Pricing not updated for partial billing items
    Javascript learning
  • 原文地址:https://www.cnblogs.com/kickit/p/7559665.html
Copyright © 2011-2022 走看看