博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #435 (Div. 2) B (二分图) C(构造)
阅读量:6253 次
发布时间:2019-06-22

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

B. Mahmoud and Ehab and the bipartiteness
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.

A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.

Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?

A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .

Input

The first line of input contains an integer n — the number of nodes in the tree (1 ≤ n ≤ 105).

The next n - 1 lines contain integers u and v (1 ≤ u, v ≤ nu ≠ v) — the description of the edges of the tree.

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

Output

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

Examples
input
3 1 2 1 3
output
0
input
5 1 2 2 3 3 4 4 5
output
2
In the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is 
(2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.

In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).

 

题意:给一棵n个结点的树,问最多能加多少边使得其是二分图并且不能有重边和自环。

思路:直接统计两部分的结点数,求出两部分结点的乘积减去n - 1条边即可

代码:

1 #include
2 #define db double 3 #include
4 #define ll long long 5 #define vec vector
6 #define Mt vector
7 #define ci(x) scanf("%d",&x) 8 #define cd(x) scanf("%lf",&x) 9 #define cl(x) scanf("%lld",&x)10 #define pi(x) printf("%d\n",x)11 #define pd(x) printf("%f\n",x)12 #define pl(x) printf("%lld\n",x)13 const int N = 2e5 + 5;14 const int mod = 1e9 + 7;15 const int MOD = 998244353;16 const db eps = 1e-18;17 const db PI = acos(-1.0);18 using namespace std;19 vector
g[N];20 bool v[N];21 int cnt[2];22 void dfs(int u,int id)23 {24 v[u]=1;25 cnt[id]++;26 for(int i=0;i

 

 

 
 
C. Mahmoud and Ehab and the xor
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets.

Dr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to find a set of n distinct non-negative integers such the bitwise-xor sum of the integers in it is exactly x. Dr. Evil doesn't like big numbers, so any number in the set shouldn't be greater than 106.

Input

The only line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the set and the desired bitwise-xor, respectively.

Output

If there is no such set, print "NO" (without quotes).

Otherwise, on the first line print "YES" (without quotes) and on the second line print n distinct integers, denoting the elements in the set is any order. If there are multiple solutions you can print any of them.

Examples
input
5 5
output
YES 1 2 4 5 7
input
3 6
output
YES 1 2 5
Note

You can read more about the bitwise-xor operation here: 

For the first sample .

For the second sample .

 

题意

寻找 n 个不同的数,且这些数的异或值等于 x 。

 

思路

开个脑洞就可以想到

除了 n=2,x=0 时找不到结果,其他情况下都可以找到一组解。

当 n=1 时显然直接输出 x 即可, n=2 时解为 0,x 。

对于其他情况下,保留三个数,其中两个可以中和掉相应位,而另一个数对最终结果做出贡献。

我们令 pr=1<<17 ,代表一个大于 n 的数,最终结果中我们假设包含 1,2,3...n3 ,且这些数的异或值为 y 。

如果 x=y ,则说明这 n3 个数已经保证了答案,那剩下的三个数只要异或值等于 0 即可,于是很方便找到 pr(pr×2)(pr(pr×2))=0 。

对于 x!=y 时,剩下的三个数 0pr(prxy) 可以保证它与之前的 y 异或等于 x 。

代码:

1 #include
2 #define db double 3 #include
4 #define ll long long 5 #define vec vector
6 #define Mt vector
7 #define ci(x) scanf("%d",&x) 8 #define cd(x) scanf("%lf",&x) 9 #define cl(x) scanf("%lld",&x)10 #define pi(x) printf("%d\n",x)11 #define pd(x) printf("%f\n",x)12 #define pl(x) printf("%lld\n",x)13 const int N = 2e5 + 5;14 const int mod = 1e9 + 7;15 const int MOD = 998244353;16 const db eps = 1e-18;17 const db PI = acos(-1.0);18 using namespace std;19 int a[N];20 int main()21 {22 23 int n,x;24 ci(n),ci(x);25 if(n==1) puts("YES"),pi(x);26 else if(n==2){27 if(!x) puts("NO");28 else puts("YES"),printf("0 %d\n",x);29 }30 else{31 int ans=0;32 int xx=(1<<17);33 puts("YES");34 for(int i=1;i<=n-3;i++){35 printf("%d ",i);36 ans^=i;37 }38 if(ans==x) printf("%d %d %d\n",xx,xx*2,xx*3);39 else printf("0 %d %d\n",xx^ans,xx^x);40 }41 42 return 0;43 }

 

转载于:https://www.cnblogs.com/mj-liylho/p/7764519.html

你可能感兴趣的文章
2017.12.25
查看>>
react--1.创建项目
查看>>
11月20日学习内容整理:jquery插件
查看>>
预科班第四次考核总结
查看>>
html
查看>>
数据分析师到底在做什么?
查看>>
pt-heartbeat工具监控MySQL复制延迟
查看>>
指尖下的js —— 多触式web前端开发之三:处理复杂手势(转)
查看>>
spring boot项目配置文件集合
查看>>
cube-ui的用法
查看>>
2015.4.21 SetWindowPos函数用法
查看>>
2011-12-14 调用cmd并获得输入输出+网络访问
查看>>
TCP定时器详解
查看>>
if判断,switch语句
查看>>
Arduino入门之前
查看>>
zoj 1904 Beavergnaw 计算圆柱和圆台的体积
查看>>
整理了一份招PHP高级工程师的面试题(转)
查看>>
学习Raft算法的笔记
查看>>
第十一周编程总结
查看>>
darknet源码学习
查看>>