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+5;
bool color[N];
vector<int>g[N];
int c=0;
int n;
ll ans=0;void dfs(int u,int v)
{color[v]=!color[u];if(color[v]==true)c++;for(int i=0;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=0;i<g[v].size();i++){if(g[v][i]!=u)DFS(v,g[v][i]);}
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);int u,v;cin>>n;for(int i=0;i<n-1;i++){cin>>u>>v;g[u].pb(v);g[v].pb(u);}color[0]=false;dfs(0,1);DFS(0,1);cout<<ans<<endl;return 0;
}

转载于:https://www.cnblogs.com/widsom/p/7569445.html

Codeforces 862B - Mahmoud and Ehab and the bipartiteness相关推荐

  1. Codeforces.959E.Mahmoud and Ehab and the xor-MST(思路)

    题目链接 \(Description\) 有一张\(n\)个点的完全图,从\(0\)到\(n-1\)标号,每两点\(i,j\)间的边权为\(i\oplus j\).求其最小生成树边权之和. \(Sol ...

  2. Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)

    <题目链接> 题目大意: 有一个长度为n(n<1000)的01串,该串中至少有一个0和一个1,现在由你构造出一些01串,进行询问,然后系统会给出你构造的串与原串的   Hamming ...

  3. Codeforces round #628 C.Ehab and Path-etic MEXs

    Codeforces round #628 C.Ehab and Path-etic MEXs You are given a tree consisting of n nodes. You want ...

  4. D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 1 #include <cstd ...

  5. E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/E 二分答案 一个数与数组中的哪个数最接近: 先对数组中的数排序,然后lower_bound 1 #include ...

  6. Codeforces 959 E. Mahmoud and Ehab and the xor-MST 思路:找规律题,时间复杂度O(log(n))

    题目: 解题思路 这题就是0,1,2-n-1总共n个数字形成的最小生成树. 我们可以发现,一个数字k与比它小的数字形成的异或值,一定可以取到k与所有正整数形成的异或值的最小值. 要计算n个数字的情况我 ...

  7. Codeforces 766E Mahmoud and a xor trip(树形DP)

    题目链接 Mahmoud and a xor trip 树形DP.先考虑每个点到他本身的距离和,再算所有点两两距离和. 做的时候考虑二进制拆位即可. #include <bits/stdc++. ...

  8. [Codeforces Round #628]1325C - Ehab and Path-etic MEXs[思维][图]

    1325C - Ehab and Path-etic MEXs[思维][图] time limit per test memory limit per test input output 1 seco ...

  9. CodeForces - 766C - Mahmoud and a Message dp

    题意 给出一个字符串 和每个字符的限制数量ai 让我们随意的切割成多个字符串 使得每一个字符串中的特定字符所在的子串长度len<=ai 求一共有多少个符合题目要求的方案 求其中最长的一个串长度是 ...

最新文章

  1. Ubuntu 14.04安装mysql
  2. 刷题-CC150-Java实现
  3. 打开网站太慢linux如何检查,seo优化中网站访问慢的检测方法
  4. [转]最流行的android组件大全
  5. android 读取excel数据并保存为xml文件
  6. 多继承-概念、语法和基本演练
  7. (转)Sql Server 对锁的初步认识
  8. 龙芯团队完成CoreCLR MIPS64移植,在github开源
  9. 特斯拉一次把电池容量翻了5倍,新车2秒破百,续航超800公里,公司股价却跌了...
  10. 营口(熊岳)温泉旅游归来!
  11. (转)如何在Windows下使用OpenGL 2.0的API(包括GLSL)
  12. hibernate(二)annotation第一个示例
  13. 新年快乐@2008!
  14. 在UI设计中用什么样的字体?
  15. python下面代码是什么意思_python的入门,代码,什么意思?
  16. linux dolphin模拟器,Dolphin for Mac(GameCube模拟器)
  17. android三星定位闪退,三星手机闪退问题7种修复方法
  18. markdown编辑器。
  19. Hi3798CV200DMD_VER.B SCH原理图
  20. mysql 廖雪峰_关系模型 - 索引 - 《廖雪峰 SQL教程(MySQL)》 - IT Book

热门文章

  1. 用python操作mysql数据库(之“更新”操作)
  2. 每天一命令 git checkout
  3. 大型网站系统架构系列:分布式消息队列(二)
  4. P2P最易遭受的DDoS***以及防御手段
  5. 解决oracle11g安装导致数据库无法自动搜集统计信息-转
  6. MVC3+EF4.1学习系列(一)-------创建EF4.1 code first的第一个实例(强转)
  7. SQL 2008 安装资料及下载地址
  8. java简单的ID生成器
  9. go6---slice切片
  10. Equifax再陷风波:一门户网站管理员密码是admin/admin