当前位置:网站首页>树与二分图【思维】
树与二分图【思维】
2022-07-17 20:46:00 【Codiplay】
题意
给定一颗树,可以在没有直接连边的两个节点上加一条边,满足加边后的图是二分图
思路
一张图是二分图的条件等价于没有奇数环,所以此题要你统计出所有节点个数为偶数的子树
从一个点出发,记录节点个数,注意到如果对于当前这个点来说是奇数个的话,去除掉此点数目一定为偶数,则这个“信息”可以继续被利用。
用异或给每个节点定性,1为奇数0为偶数
从节点1走到节点0所包含的节点一定是偶数个,则是一组为节点个数为偶数的子树,所以:统计一颗树的节点个数为偶数的子树,只需用节点标记为1的数目乘上节点标记为0的数目,得到的便是节点个数为偶数的子树。
题目中说要符合没有直接连边,又因为相邻的节点必一个为0一个为1,数目为2,符合为偶数个的条件,但是有直接连边,所以要去掉边的个数(n - 1)即为答案
代码
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 100000;
int n, cnt0, cnt1;
vector<int> G[N];
void dfs(int u, int fa, int s)
//这样写不需要再开个vis防止倒退。
//因为是棵树,只需要保证当前节点不会回到fa即可
{
if(s == 1)cnt1 ++;
else cnt0 ++;
for (int i = 0; i < (int)G[u].size(); i ++ )
{
if(G[u][i] == fa)continue;
dfs(G[u][i], u, s ^ 1);
}
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> n;
for (int i = 1, u, v; i < n; i ++ ) {
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 1, 1);
cout << (cnt1 * cnt0) - n + 1 << '\n';
return 0;
}
边栏推荐
- Use of Google browser developer tools (Master!)
- 微服务调用组件feign实战
- STL string find substring
- uniapp 高德地图定位功能
- Redis源码与设计剖析 -- 4.跳跃表
- Is it safe for Hongye futures to open an account online? Are there any account opening guidelines?
- Logu: p4516 [jsoi2018] stealth action (tree DP, tree grouping knapsack statistical scheme number)
- 4 a company has branches in six cities C1, C2, C3... C6. The connection between cities Ci and CJ (I, j=1,2,3,... 6) and the cost are listed in the following weighted adjacency matrix C
- Several small open source projects of mine over the years
- 【ACWing】2521. Count colors
猜你喜欢

FreeRTOS implementation of idle tasks and blocking delay

Robotics at google:laura Graesser | i-sim2real: strengthen the learning robot strategy in the close human-computer interaction cycle

Uniapp Gaode map positioning function

No.3汇编进阶

陶博士月线反转6.0

Huawei wireless devices are configured with static load balancing

华为无线设备配置用户CAC

After 2000, he was hired as a special associate researcher of Nanjing University. He went to primary school at the age of 4 and was admitted to Nanjing University at the age of 14!

【Acwing】第60场周赛 题解

Redis源码与设计剖析 -- 2.链表
随机推荐
AcWing 134. 双端队列
函数初认识-下
Create a new Eureka client
Go exceed API source code reading (III) -- openreader ()
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
No.3 advanced assembly
Redis源码与设计剖析 -- 1.简单动态字符串
ImportError: DLL load failed while importing win32api: 找不到指定的程序。
Huawei technologies:jonathan Krolikowski | from design to deployment, zero contact deep reinforcement learning WLANs
No.3汇编进阶
Compréhension initiale de la fonction - partie 2
Version announcement | Apache Doris 1.1 release version officially released!
陶博士月线反转6.0
[7.14] code source - [open box] [XOR inverse] [continuous subsequence] [trigonometric fruit count]
【6.15】Codeforces Round #798 (Div. 2)
Luogu p3522 [poi2011] TEM temperature solution
After 2000, he was hired as a special associate researcher of Nanjing University. He went to primary school at the age of 4 and was admitted to Nanjing University at the age of 14!
Take a look at try{}catch{}
How to prepare for the autumn recruitment for the graduate students of the second non science class of Graduate School
Introduction:Multiple DataFrames