862B - Mahmoud and Ehab and the bipartiteness

思路:先染色,然后找一种颜色dfs遍历每一个点求答案。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e6+;
bool color[N];
vector<int>g[N];
int c=;
int n;
ll ans=; void dfs(int u,int v)
{
color[v]=!color[u];
if(color[v]==true)c++;
for(int i=;i<g[v].size();i++)
if(g[v][i]!=u)dfs(v,g[v][i]);
} void DFS(int u,int v)
{
if(color[v]==true)ans+=(n-c-g[v].size());
for(int i=;i<g[v].size();i++)
{
if(g[v][i]!=u)DFS(v,g[v][i]);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int u,v;
cin>>n;
for(int i=;i<n-;i++)
{
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
} color[]=false;
dfs(,);
DFS(,);
cout<<ans<<endl; return ;
}

Codeforces 862B - Mahmoud and Ehab and the bipartiteness的更多相关文章

  1. CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)

    题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图. 分析: 1.通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2. 2.两个集 ...

  2. Coderfroces 862 B . Mahmoud and Ehab and the bipartiteness

     Mahmoud and Ehab and the bipartiteness Mahmoud and Ehab continue their adventures! As everybody in ...

  3. Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)

    Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...

  4. E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)

    Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipa ...

  5. Codeforces 862A Mahmoud and Ehab and the MEX

    传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...

  6. Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)

    Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...

  7. Codeforces 862C - Mahmoud and Ehab and the xor

    862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...

  8. Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

    题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...

  9. CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定

    \(\color{#0066ff}{题目描述}\) 给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立 \(\color{#0066ff}{输入格式}\) The first line ...

随机推荐

  1. Microsoft Interview第一轮

    上来随意交谈了一小会儿,开了点小玩笑,chat了一些关于他们recruter行程的话题,缓和了一下气氛. 进入正题,问了做的research的方向,我说是DLT,然后大概给他讲解了一下具体是什么, 跟 ...

  2. C语言进阶之路(三)----野指针的产生原因及解决办法

    1.会产生野指针的做法 #include <stdio.h> //这就是一种错误的写法 int main(){ int *p = NULL; p = (); //释放P所指向的内存空间,但 ...

  3. HTTP 头和 PHP header() 函数

    http://unifreak.github.io/translation/Http_headers_and_PHP_header()_function 引言 许多初级到中级的的 PHP 程序员把 h ...

  4. 转载的 Linux下chkconfig命令详解

    Linux下chkconfig命令详解 chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. ...

  5. php深入学习

    关于PHP程序员解决问题的能力 http://rango.swoole.com/archives/340 深入理解PHP内核 by xuhong大牛 http://www.php-internals. ...

  6. BufferedImage操作图片笔记(转)

    BufferedImage是Image的一个子类,BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便的操作这个图片,通常用来做图片修改操作如大小变换.图片变灰. ...

  7. js将时间戳转化为日期格式

    function getLocalTime(nS) {        var date = new Date(nS);        var Y = date.getFullYear() + '-'; ...

  8. Linux下Oracle常用命令

    1. 备份表 exp database_user/pass tables='(table1,table2)' file=filename.dmp(例如:exp ismrenbao/iflytek ta ...

  9. ACM题目————区间覆盖问题

    题目描述 设x1 , x2,... , xn是实直线上的n个点.用固定长度的闭区间覆盖这n个点,至少需要多少个这样的固定长度闭区间?设计解此问题的有效算法,并证明算法的正确性.编程任务:对于给定的实直 ...

  10. MySQL Crash Course #14# Chapter 22. Using Views

    索引 视图是啥 为什么需要视图 使用视图的规则 如何使用视图 视图应用实例 别用视图更新数据! 视图是啥 理解视图的最佳方式就是看下面这个例子. SELECT cust_name, cust_cont ...