博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定
阅读量:4619 次
发布时间:2019-06-09

本文共 2136 字,大约阅读时间需要 7 分钟。

\(\color{#0066ff}{题目描述}\)

给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立

\(\color{#0066ff}{输入格式}\)

The first line of input contains an integer n — the number of nodes in the tree ( \(1<=n<=10^{5}\) ).

The next n−1 lines contain integers u and v ( \(1<=u,v<=n u≠v u≠v\) ) — the description of the edges of the tree.

It's guaranteed that the given graph is a tree.

\(\color{#0066ff}{输出格式}\)

Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.

\(\color{#0066ff}{输入样例}\)

51 22 33 44 5

\(\color{#0066ff}{输出样例}\)

2

\(\color{#0066ff}{题解}\)

先二分图染色(原图一定是二分图)

分别统计颜色为1和0的数量

开数组记录每个节点的度

显然du就是它已经连的和它颜色相反的节点的个数

因为要求做多连多少边,为了保证性质,只要颜色不同就行,所以剩下的总数-du个点都可以连边

每条边会被算两次,最后/2

#include
#include
#include
#include
#include
#include
#include
#include
#define _ 0#define LL long long#define Space putchar(' ')#define Enter putchar('\n')#define fuu(x,y,z) for(int x=(y),x##end=z;x<=x##end;x++)#define fu(x,y,z) for(int x=(y),x##end=z;x
=x##end;x--)#define fd(x,y,z) for(int x=(y),x##end=z;x>x##end;x--)#define mem(x,y) memset(x,y,sizeof(x))#ifndef olinrinline char getc(){ static char buf[100001],*p1=buf,*p2=buf; return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100001,stdin),p1==p2)? EOF:*p1++;}#else#define getc() getchar()#endiftemplate
inline void in(T &x){ int f=1; char ch; x=0; while(!isdigit(ch=getc()))(ch=='-')&&(f=-f); while(isdigit(ch)) x=x*10+(ch^48),ch=getc(); x*=f;}struct node{ int to; node *nxt;};typedef node* nod; nod head[105005];int n;int col[105005],du[105050],num1,num0;LL ans;inline void dfs(int x,int c){ col[x]=c; for(nod i=head[x];i;i=i->nxt) { if(~col[i->to]) continue; dfs(i->to,c^1); } }inline void add(int from,int to){ nod t=new node(); t->to=to; t->nxt=head[from]; head[from]=t;}int main(){ int x,y; in(n); fuu(i,1,n) col[i]=-1; fuu(i,1,n-1) in(x),in(y),du[x]++,du[y]++,add(x,y),add(y,x); dfs(1,0); fuu(i,1,n) col[i]? num1++:num0++; fuu(i,1,n) ans+=(col[i]? num0:num1)-du[i]; printf("%lld\n",ans>>1); return ~~(0^_^0);}

转载于:https://www.cnblogs.com/olinr/p/10045932.html

你可能感兴趣的文章
Vijos P1243 生产产品 (单调队列优化DP)
查看>>
iOS常用第三方库 -转
查看>>
Android布局学习
查看>>
python的沙盒环境--virtualenv
查看>>
软件自动化测试——入门、进阶与实战
查看>>
BZOJ1878 [SDOI2009]HH的项链 树状数组 或 莫队
查看>>
BZOJ3675 [Apio2014]序列分割 动态规划 斜率优化
查看>>
2016.10.24 继续学习
查看>>
产品功能对标 - 服务授权管理
查看>>
各地IT薪资待遇讨论
查看>>
splay入门
查看>>
带CookieContainer进行post
查看>>
C语言学习笔记--字符串
查看>>
关于七牛进行图片添加文字水印操作小计
查看>>
DataSource数据库的使用
查看>>
Luogu4069 SDOI2016 游戏 树链剖分、李超线段树
查看>>
Java的内部类真的那么难以理解?
查看>>
一文搞懂Java环境,轻松实现Hello World!
查看>>
hash实现锚点平滑滚动定位
查看>>
也谈智能手机游戏开发中的分辨率自适应问题
查看>>