Codeforces Round #435 (Div. 2) B (二分图) C(构造)
2 seconds
256 megabytes
standard input
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 .
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 ≤ n, u ≠ v) — the description of the edges of the tree.
It's guaranteed that the given graph is a tree.
Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
3
1 2
1 3
0
5
1 2
2 3
3 4
4 5
2
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 ;
}
2 seconds
256 megabytes
standard input
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.
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.
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.
5 5
YES
1 2 4 5 7
3 6
YES
1 2 5
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(构造)的更多相关文章
- Codeforces Round #435 (Div. 2)【A、B、C、D】
//在我对着D题发呆的时候,柴神秒掉了D题并说:这个D感觉比C题简单呀!,,我:[哭.jpg](逃 Codeforces Round #435 (Div. 2) codeforces 862 A. M ...
- 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 ...
- 【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& ...
- Codeforces Round #548 (Div. 2) E 二分图匹配(新坑) or 网络流 + 反向处理
https://codeforces.com/contest/1139/problem/E 题意 有n个学生,m个社团,每个学生有一个\(p_i\)值,然后每个学生属于\(c_i\)社团, 有d天,每 ...
- Codeforces Round #435 (Div. 2)
A. Mahmoud and Ehab and the MEX 题目链接:http://codeforces.com/contest/862/problem/A 题目意思:现在一个数列中有n个数,每个 ...
- D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)
http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 #include <cstdio ...
- E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)
http://codeforces.com/contest/862/problem/E 二分答案 一个数与数组中的哪个数最接近: 先对数组中的数排序,然后lower_bound #include &l ...
- Codeforces Round #383 (Div. 1) C(二分图)
一道很巧妙的二分图的题目 简单分析性质可知,一个合法序列一定是由12,21这样的子串构成的,所以相邻的每隔2个两两配对 然后BF和GF互相配对,思考一下,如果存在奇环,那么必定有一个BG有两个GF,或 ...
- Codeforces Round #360 (Div. 1)A (二分图&dfs染色)
题目链接:http://codeforces.com/problemset/problem/687/A 题意:给出一个n个点m条边的图,分别将每条边连接的两个点放到两个集合中,输出两个集合中的点,若不 ...
随机推荐
- 寻找jar包的方法
在项目开发中经常会遇到资源jar查找难的问题,一种使用maven ,另一种方法是: (1).使用下载地址:https://oss.sonatype.org/content/repositories/r ...
- vue 导出excel
1.安装三个依赖包 npm install -S file-saver npm install -S xlsx npm install -D script-loader 2.在项目中创建一个文件夹(比 ...
- A promise tomorrow is worth a lot less than trying today.
A promise tomorrow is worth a lot less than trying today.明日的承诺远不及今日的行动.
- 使用纯css实现波浪效果
有时候我们需要实现水晃动的效果,其实我们可以通过css旋转动画和圆角来实现. 首先来2个div,外层div相对定位,内层div绝对定位,内层div大致位于外层div上半部分.外层div设置一个颜色较深 ...
- linux命令模式下如何切换首行和尾行
G是到最后一行,gg是到第一行
- LeetCode Add Binary 两个二进制数相加
class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...
- 基于PowerShell的Lync Server管理 使用C#
这里所说的Lync Server管理,指通过C#管理Lync账号的启用,禁用,开启账户的语音功能. Lync服务器安装后,会自动创建一个用于远程管理的应用程序,通过IIS查看,其应用程序名为: Lyn ...
- Windows Profile的一些问题
电脑症状:桌面复制的文件重启后消失:新安装的软件重启后也自动消失.排查:使用autoruns观察,发现安装了麦咖啡和360两套“安全”软件,除此外并无异常,任务管理器內也无异常发现.过程:1.保险起见 ...
- 待解决问题:c++栈对象的析构、虚拟内存与内存管理的关系、内存管理的解决方案。
待解决问题:c++栈对象的析构.虚拟内存与内存管理的关系.内存管理的解决方案.
- javaweb基础(25)_jsp标签实例一
一.简单标签(SimpleTag) 由于传统标签使用三个标签接口来完成不同的功能,显得过于繁琐,不利于标签技术的推广, SUN公司为降低标签技术的学习难度,在JSP 2.0中定义了一个更为简单.便于编 ...