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条边即可

代码:

 #include<bits/stdc++.h>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
const int N = 2e5 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
vector<int> g[N];
bool v[N];
int cnt[];
void dfs(int u,int id)
{
v[u]=;
cnt[id]++;
for(int i=;i<g[u].size();i++){
int vv=g[u][i];
if(v[vv]) continue;
dfs(vv,id^);
}
}
int main()
{
int n;
ci(n);
int x,y;
for(int i=;i<n;i++) ci(x),ci(y),g[x].push_back(y),g[y].push_back(x);
dfs(,);
pl(1ll*cnt[]*cnt[]-n+);
return ;
}
 
 
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: https://en.wikipedia.org/wiki/Bitwise_operation#XOR

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...n−3 ,且这些数的异或值为 y 。

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

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

代码:

 #include<bits/stdc++.h>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
const int N = 2e5 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
int a[N];
int main()
{ int n,x;
ci(n),ci(x);
if(n==) puts("YES"),pi(x);
else if(n==){
if(!x) puts("NO");
else puts("YES"),printf("0 %d\n",x);
}
else{
int ans=;
int xx=(<<);
puts("YES");
for(int i=;i<=n-;i++){
printf("%d ",i);
ans^=i;
}
if(ans==x) printf("%d %d %d\n",xx,xx*,xx*);
else printf("0 %d %d\n",xx^ans,xx^x);
} return ;
}

Codeforces Round #435 (Div. 2) B (二分图) C(构造)的更多相关文章

  1. Codeforces Round #435 (Div. 2)【A、B、C、D】

    //在我对着D题发呆的时候,柴神秒掉了D题并说:这个D感觉比C题简单呀!,,我:[哭.jpg](逃 Codeforces Round #435 (Div. 2) codeforces 862 A. M ...

  2. Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

    Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 ht ...

  3. 【Codeforces Round #435 (Div. 2) A B C D】

    CF比赛题目地址:http://codeforces.com/contest/862 A. Mahmoud and Ehab and the MEX ·英文题,述大意:      输入n,x(n,x& ...

  4. Codeforces Round #548 (Div. 2) E 二分图匹配(新坑) or 网络流 + 反向处理

    https://codeforces.com/contest/1139/problem/E 题意 有n个学生,m个社团,每个学生有一个\(p_i\)值,然后每个学生属于\(c_i\)社团, 有d天,每 ...

  5. Codeforces Round #435 (Div. 2)

    A. Mahmoud and Ehab and the MEX 题目链接:http://codeforces.com/contest/862/problem/A 题目意思:现在一个数列中有n个数,每个 ...

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

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

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

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

  8. Codeforces Round #383 (Div. 1) C(二分图)

    一道很巧妙的二分图的题目 简单分析性质可知,一个合法序列一定是由12,21这样的子串构成的,所以相邻的每隔2个两两配对 然后BF和GF互相配对,思考一下,如果存在奇环,那么必定有一个BG有两个GF,或 ...

  9. Codeforces Round #360 (Div. 1)A (二分图&dfs染色)

    题目链接:http://codeforces.com/problemset/problem/687/A 题意:给出一个n个点m条边的图,分别将每条边连接的两个点放到两个集合中,输出两个集合中的点,若不 ...

随机推荐

  1. <Linux系统minfo命令用法>

    minfo命令——ms-dos文件系统的各项参数的显示 minfo这个指令主要用于显示MS-DOS文件系统的各项参数,包括扇区数.磁头数.柱面数等. 参数主要有以下几个: -v 显示详细的参数 dri ...

  2. 实现多行文字居中方法(兼容IE6)

    <p class="mulit_line"> <span style="font-size:12px;">这里是高度为150像素的标签内 ...

  3. SpringMVC 返回自定义属性名

    SpringMVC 返回的属性名默认是小写驼峰形式的实体对象中的属性名,如 userID 属性名它会返回 userId. 如果接口方式之前已经定下来,这样前端按原来的方式取数据会读取失败的,那有没有方 ...

  4. 一、Spring-Data-Jpa 初体验(基于SpringBoot)

    闲话少说,首先动起来(基于springboot+gradle): 1.引入依赖 dependencies { compile 'org.springframework.boot:spring-boot ...

  5. hibernate课程 初探单表映射3-3 对象类型

    本节简介: 1 简介对象类型(重点是音视频blob类型) 2 demo(对图片的写入数据库与读取) 1 简介对象类型 映射类型 java类型 标准sql类型 mysql类型 oracle类型 bina ...

  6. Life here can be a dream come true!

    Life here can be a dream come true!美梦迟早会成真的!

  7. LAMP Stack 5.7.16 (Ubuntu 16.04.1)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: apache2.4 mysql5.7 php7 phpmyadmin4.5 apache application server basic soft ...

  8. linux常用工具

    命令 功能应用 用法举例 free 查看内存使用情况,包括物理内存和虚拟内存 free -h或free -m vmstat 对系统的整体情况进行统计,包括内核进程.虚拟内存.磁盘.陷阱和 CPU 活动 ...

  9. C#之razor

    学习的文章在这里:http://www.cnblogs.com/yang_sy/archive/2013/08/26/ASPNET_MVC_RAZOR_ENGINE.html 1.视图开始文件_Vie ...

  10. 爬虫技术-httpClent+jsoup

    技术:httpClent+jsoup 任务:利用httpClent爬去网站信息,在利用jsoup解析 方法说明: parseUrl(String url):传入相应的url返回该网页内容,网页必须是h ...