CodeForces - 1016D Vasya And The Matrix
很明显二进制每一位都是无关的,所以可以先把原问题简化:给矩阵中的每个位置填入0/1,使得特定行/列有奇数个1,其他行/列有偶数个1。
一个比较好想的方法是对行和列 列出 n+m 个异或方程,其中有 n*m 个变量,随便求出一组解就好了(如果有的话)。
但这个貌似并不是很好写。。。
可以把解异或方程转化成 在一个完全二分图(左n个点,右m个点)上选边,每个点可以是黑的(对应行/列要求有奇数个1)或者白的(反之),每选一条边就要把两端的点的黑白性颠倒。
然后发现这是一个经典问题,显然选的边可以只存在与随便一颗生成树上,因为加入后可以成环的边不会影响答案。
并且可以发现无解当且仅当有奇数个黑点,因为选一条边不会改变图中黑点个数的奇偶性,而我们要求最后所有点都消成白点。
其他情况都有解,这里只从操作意义上解释为什么(但其实方程组的证明也很简单,因为你考虑一下把所有n+m行异或起来,会得到一个全空行,也就是有一个方程其实是没有用的,于是总是有解的)。
考虑一种构造方案:我们对这颗随便找的生成树做一遍dfs,先把子树中的其他节点都处理成白的之后,如果当前节点是黑的,那么就选它到它father的边;否则不选。
显然这样做总能保证除了根的其他节点都会变成白色,但又因为选一条边不会改变图中黑点的奇偶性,并且已经保证有偶数个黑点了,所以最后根也一定是白色的。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define pb push_back
const int N=105; vector<int> g[N*2];
int n,m,a[N*2],ans[N][N],Xor[N*2],now; inline void pt(int x,int y){
if(x>y) swap(x,y);
ans[x][y-n]|=now;
} void dfs(int x,int fa){
for(int i:g[x]) if(i!=fa){
dfs(i,x);
if(Xor[i]) Xor[x]^=now,pt(x,i);
}
} int main(){
scanf("%d%d",&n,&m),m+=n;
for(int i=1;i<=m;i++) scanf("%d",a+i); for(int i=n+1;i<=m;i++) g[1].pb(i),g[i].pb(1);
for(int i=2;i<=n;i++) g[i].pb(n+1),g[n+1].pb(i); for(now=1;now<=1e9;now<<=1){
for(int i=1;i<=m;i++) Xor[i]=a[i]&now;
dfs(1,0);
if(Xor[1]){ puts("NO"); return 0;}
} puts("YES"),m-=n; for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++) printf("%d ",ans[i][j]);
puts("");
} return 0;
}
CodeForces - 1016D Vasya And The Matrix的更多相关文章
- Educational Codeforces Round 48 (Rated for Div. 2) D 1016D Vasya And The Matrix (构造)
D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 1016D.Vasya And The Matrix#矩阵存在
题目出处:http://codeforces.com/contest/1016/problem/D #include<iostream> #define ll long long int ...
- Vasya And The Matrix CodeForces - 1016D (思维+构造)
Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the ma ...
- D. Vasya And The Matrix(Educational Codeforces Round 48)
D. Vasya And The Matrix time limit per test2 seconds memory limit per test256 megabytes inputstandar ...
- CF 1042 E. Vasya and Magic Matrix
E. Vasya and Magic Matrix http://codeforces.com/contest/1042/problem/E 题意: 一个n*m的矩阵,每个位置有一个元素,给定一个起点 ...
- codeforces1016 D. Vasya And The Matrix(思维+神奇构造)
D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26
/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...
- 【Educational Codeforces Round 48 (Rated for Div. 2) D】Vasya And The Matrix
[链接] 我是链接,点我呀:) [题意] 告诉你每一行.每一列的异或和. 让你求出一个符合要求的原矩阵. [题解] 显然应该有 a1^a2^....^an = b1^b2^....^bn 也即两边同时 ...
- Educational Codeforces Round 48 D Vasya And The Matrix
EDU #48 D 题意:给定一个矩阵,已知每一行和每一列上数字的异或和,问矩阵上的数字是多少,不存在则输出NO. 思路:构造题,可以考虑只填最后一行,和最后一列,其中(n,m)要特判一下.其他格子给 ...
随机推荐
- laravel 模糊查询
模糊查询: Model::where('field_name','like','%'.$keywords.'%')->get() 转载:http://wenda.golaravel.com/qu ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
- inet_confirm_addr && confirm_addr_indev
确认给定参数范围的ip地址是否存在: /* * Confirm that local IP address exists using wildcards: * - net: netns to chec ...
- elasticsearch删除索引报错【原】
如果elasticsearch删除索引报错 curl -X DELETE 'http://10.73.26.66:9200/httpd-34-2017.08.15' {"error" ...
- 2016 ACM ICPC Asia Region - Tehran
2016 ACM ICPC Asia Region - Tehran A - Tax 题目描述:算税. solution 模拟. B - Key Maker 题目描述:给出\(n\)个序列,给定一个序 ...
- UVA题解二
UVA题解二 UVA 110 题目描述:输出一个Pascal程序,该程序能读入不多于\(8\)个数,并输出从小到大排好序后的数.注意:该程序只能用读入语句,输出语句,if语句. solution 模仿 ...
- {%csrf_token%}的作用
<form> {%csrf_token%} </form> 在django中我们需要在templates的form中加入{%csrf_token%}这串内容,它的作用是当我们g ...
- javascript当中的this详解
总结this的3个规则: this是调用上下文,上下文被创建或者初始化时才确定 非严格模式:this是全局对象:严格模式:this是undefined 函数调用 a. 以函数形式调用的函数通常不使用t ...
- 使用递归计算n的阶乘n!
计算n! 观察公式2可以直接使用递归求解 C++代码如下: #include <iostream> using namespace std; unsigned func(unsigned ...
- day4 正则表达式(regular)
正则(regular),要使用正则表达式需要导入Python中的re(regular正则的缩写)模块.正则表达式是对字符串的处理,我们知道,字符串中有时候包含很多我们想要提取的信息,掌握这些处理字符串 ...