codeforces 862B. Mahmoud and Ehab and the bipartiteness
来源:互联网 发布:flash cc mac中文版 编辑:程序博客网 时间:2024/06/03 03:37
题目链接:
http://codeforces.com/contest/862/problem/B
题解:
二分图,染色法。
代码:
#include <map>#include <cmath>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define met(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3ftypedef long long ll;const int maxn = 1e5+10;vector<int> p[maxn];int color[maxn],visited[maxn];void dfs(int x,int c){ visited[x]=1; color[x]=c; for(int i=0;i<p[x].size();i++) { int y=p[x][i]; if(color[y]==0&&!visited[y]) { dfs(y,-c); } }}int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) p[i].clear(); int m=n-1; for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); p[x].push_back(y); p[y].push_back(x); } met(visited,0); met(color,0); dfs(1,1); ll ans1=0,ans2=0; for(int i=1;i<=n;i++) { if(color[i]==-1) ans1++; if(color[i]==1) ans2++; } ll ans=ans1*ans2-n+1; printf("%lld\n",ans);}
阅读全文
0 0
- codeforces 862B B. Mahmoud and Ehab and the bipartiteness
- codeforces 862B. Mahmoud and Ehab and the bipartiteness
- Mahmoud and Ehab and the bipartiteness CodeForces
- codeforces Mahmoud and Ehab and the bipartiteness
- codeforces 862B (二分图 dfs )之 Mahmoud and Ehab and the bipartiteness
- Codeforces 862 B Mahmoud and Ehab and the bipartiteness(二分图染色)
- CF 863.B Mahmoud and Ehab and the bipartiteness【DFS】
- Codeforces Round #435 (Div. 2)-B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(补)
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图,染色法)
- 862B Mahmoud and Ehab and the bipartiteness (黑白染色)
- Codeforce 862B Mahmoud and Ehab and the bipartiteness (二分图染色 + 链式前向星)
- Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图染色)
- Mahmoud and Ehab and the xor CodeForces
- Mahmoud and Ehab and the xor CodeForces
- Codeforces --- Mahmoud and Ehab and the MEX
- codeforces 862A Mahmoud and Ehab and the MEX
- 使用原生JavaScript实现ajax操作
- 链式有序表的合并
- 基础排序---冒泡排序
- js排序算法详解-计数排序
- 基于Selenium与图像识别的百度指数爬虫
- codeforces 862B. Mahmoud and Ehab and the bipartiteness
- 十进制小数:循环节等问题
- HDU 5333 [LCT][树状数组]
- 周中训练笔记+Billboard(9.21)
- TestDemo02
- js排序算法详解-桶排序
- sizeof(),strlen(),计算字节数;
- 日期类
- 错切变换