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。

  • 相关阅读:
    HTB-靶机-Charon
    第一篇Active Directory疑难解答概述(1)
    Outlook Web App 客户端超时设置
    【Troubleshooting Case】Exchange Server 组件状态应用排错?
    【Troubleshooting Case】Unable to delete Exchange database?
    Exchange Server 2007的即将生命周期,您的计划是?
    "the hypervisor is not running" 故障
    Exchange 2016 体系结构
    USB PE
    10 months then free? 10个月,然后自由
  • 原文地址:https://www.cnblogs.com/kickit/p/7559665.html
Copyright © 2011-2022 走看看