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. MVC学习6 学习使用Code First Migrations功能 把Model的更新同步到DB中

     参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-th ...

  2. Devexpress之GridControl显示序列号

    先上图: 操作方法: 1.先设置一下gridview中属性:IndicatorWidth,一般为:40.如下图:(一般可以显示5位数字.如要更长显示,自己测试一下.) 2.找到gridview中的:C ...

  3. MySQL 返回指定长度的字符串

    今天在做 iblog 项目时,有一个需求是,从 MySQL 返回某个字段的值要时要指定长度,上网搜到的方法是使用 MySQL 的字符串处理函数,如 left(str, length),right(st ...

  4. 一些C/C++中的函数

    项目中使用到的C/C++中的一些函数,记录下来加以理解和掌握. 1.memset( ) memset是计算机中C/C++语言函数.将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASC ...

  5. FusionCharts使用教程:为JavaScript图表提供数据

    FusionCharts的JavaScript类提供了一系列的函数来提供图表数据. FusionCharts的JavaScript类支持XML或JSON格式的数据.这些数据可以是URL或字符串. 以X ...

  6. newCoder在线编程---(1)

    二维数组查找 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 1.暴 ...

  7. map侧连接

    两个数据集中一个非常小,可以让小数据集存入缓存.在作业开始这些文件会被复制到运行task的节点上. 一开始,它的setup方法会检索缓存文件. 与reduce侧连接不同,Map侧连接需要等待参与连接的 ...

  8. python3基础08(exec、bytearray使用等)

    #!/usr/bin/env python# -*- coding:utf-8 -*- str="test"print(ascii(str))a=bytearray("a ...

  9. python request下载文件时,显示进度以及网速

    import requests import time def downloadFile(name, url): headers = {'Proxy-Connection':'keep-alive'} ...

  10. Videos

    Videos 时间限制: 1 Sec  内存限制: 128 MB提交: 17  解决: 7[提交] [状态] [讨论版] [命题人:admin] 题目描述 C-bacteria takes charg ...