luogu P2962 [USACO09NOV]灯Lights 高斯消元
目录
题目链接
luogu P2962 [USACO09NOV]灯Lights
题解
可以折半搜索
map合并
复杂度
2^(n / 2)*logn
高斯消元后得到每个点的翻转状态
爆搜自由元得到最优翻转状态
// luogu-judger-enable-o2
#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define gc getchar()
#define pc putchar
inline int read() {
    int x = 0,f = 1;
    char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
    return x * f;
}
void print(int x) {
    if(x < 0) {
        putchar('-');
        x = -x;
    }
    if(x >= 10) print(x / 10);
    putchar(x % 10 + '0');
}
#define LL long long
int n,m;
LL s[200];
int cnt,ans = 0x3f3f3f3f;
std::map<LL,int>b;
LL ed;
bool flag;
void dfs(int x,LL now,int used) {
    if(x == cnt + 1) {
        if(now == ed) ans = std::min(used,ans);
        if(!flag) {
            int t = b[now];
            if(!t || t > used) b[now] = used;
        } else {
            int t = b[ed ^ now] ;
            if(!t) return ;
            ans = std::min(ans,t + used);
        }
        return ;
    }
    dfs(x + 1,now,used);
    dfs(x + 1,now ^ s[x],used + 1);
}
int main() {
    //freopen("data.cpp","r",stdin);
    n = read(), m = read();
    for(int i = 1;i <= m;++ i) {
        int u = read(),v = read();
        s[u] |= (1ll << v - 1) ,s[v] |= (1ll << u - 1);
    }
    ed = (1ll << n) - 1;
    //print(ed); pc('\n');
    for(int i = 1;i <= n;++ i) s[i] |= (1ll << i - 1);
    cnt = n / 2;
    dfs(1,0,0);
    flag = 1; cnt = n;
    dfs(n / 2 + 1,0,0);
    print(ans);
    pc('\n');
}
#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(a,b,c) for(int a = b; a <= c;++ a)
#define per(a,b,c) for(int a = b; a >= c; -- a)
#define gc getchar()
#define pc putchar
inline int read() {
	int x = 0,f = 1;
	char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
	return x * f;
}
void print(int x) {
	if(x < 0) {
		putchar('-');
		x = -x;
	}
	if(x >= 10) print(x / 10);
	putchar(x % 10 + '0');
}
#define LL long long
int n,m;
const int maxn = 37;
int f[maxn][maxn];
bool flag;
void guass() {
	rep(i,1,n) {
		int j = i;
		while(!f[j][i] && j <= n) ++ j;
		if(j == n + 1) continue;
		if(i != j) swap(f[i],f[j]);
		rep(j,1,n)
			if(j != i && f[j][i])
				rep(k,1,n + 1)
					f[j][k] ^= f[i][k];
	}
}
int zy[maxn],tot = 0,ans = 0x3f3f3f3f;
void dfs(int now) {
	if(tot > ans) return;
	if(!now) {
		ans = std::min(ans,tot);
		return ;
	}
	if(f[now][now]) {
		int t = f[now][n + 1];
		rep(i,now + 1,n) if(f[now][i]) t ^= zy[i];
		zy[now] = t;
		if(t) tot ++;
		dfs(now - 1);
		if(t) tot --;
	} else {
		zy[now] = 0;
		dfs(now - 1);
		zy[now] = 1;
		tot ++;
		dfs(now - 1);
		tot --;
	}
}
int main() {
	n = read(),m = read();
	rep(i,1,n) f[i][i] = f[i][n + 1] = 1;
	rep(i,1,m) {
		int x = read(),y = read();
		f[x][y] = f[y][x] = 1;
	}
	guass();
	dfs(n);
	print(ans);
	pc('\n');
}
												
											luogu P2962 [USACO09NOV]灯Lights 高斯消元的更多相关文章
- LUOGU  P2962 [USACO09NOV]灯Lights
		
题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...
 - [Usaco2009 Nov]lights(高斯消元)
		
luogu 点灯游戏应该很多人都在小时候頽过吧 反正我直到现在也不会 很明显一个灯最多只需要点一次 然后高斯消元 解完肯定剩自由元(就是那些全是0的行) 然后这些都爆搜 由于剩下的自由元不会太多 所以 ...
 - [洛谷P2962] [USACO09NOV] 灯Lights
		
Description Bessie and the cows were playing games in the barn, but the power was reset and the ligh ...
 - P2962 [USACO09NOV]灯Lights
		
贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊恐,痛苦与绝望.她希望您能够帮帮她,把所 ...
 - 【Luogu】P3317重建(高斯消元+矩阵树定理)
		
题目链接 因为这个专门跑去学了矩阵树定理和高斯消元qwq 不过不是很懂.所以这里只放题解 玫葵之蝶的题解 某未知dalao的矩阵树定理 代码 #include<cstdio> #inclu ...
 - luogu 3389 【模板】高斯消元
		
大概就是对每一行先找到最大的减小误差,然后代入消元 #include<iostream> #include<cstdio> #include<cstring> #i ...
 - P2962 [USACO09NOV]灯Lights 对抗搜索
		
\(\color{#0066ff}{题目描述}\) 贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗 ...
 - 洛谷 P2962 [USACO09NOV]灯Lights
		
题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...
 - Luogu P2455 [SDOI2006]线性方程组 真•高斯消元板子
		
果然如Miracle学长所说...调了一天...qwq..还是过不了线下的Hack upd after 40min:刚刚过了 就是多了一个判无解的操作... 当系数都为0,且常数项不为0时,即为无解. ...
 
随机推荐
- 网络编程—udp
			
一.ip地址 1. 什么是地址 地址就是用来标记地点的 2. ip地址的作用 ip地址:用来在网络中标记一台电脑,比如192.168.1.1:在本地局域网上是唯一的. 3. ip地址的分类 每一个IP ...
 - sqlmap实例文档
			
sqlmap 手册参数整理文档 1.--data sqlmap -u "http://www.target.com/vuln.php" --data="id=1" ...
 - uva11916 bsgs算法逆元模板,求逆元,组合计数
			
其实思维难度不是很大,但是各种处理很麻烦,公式推导到最后就是一个bsgs算法解方程 /* 要给M行N列的网格染色,其中有B个不用染色,其他每个格子涂一种颜色,同一列上下两个格子不能染相同的颜色 涂色方 ...
 - hdu1565 用搜索代替枚举找可能状态或者轮廓线解(较优),参考poj2411
			
这题用直接枚举是超时的,必须要用搜索来搜索出所有可能的状态,然后再进行枚举 这是较慢的做法 /* 方格取数,相邻格子的数不可取,问最多取到的和是什么 有点类似炮兵布阵,先打出所有可能的状态,然后dp[ ...
 - node.js 框架express有关于router的运用
			
1.express 路由入门 const express = require('express'); let server = express(); server.listen(8087); //用户 ...
 - eclipse创建动态maven项目
			
需求表均同springmvc案例 此处只是使用maven 注意,以下所有需要建立在你的eclipse等已经集成配置好了maven了,说白了就是新建项目的时候已经可以找到maven了 没有的话需要安装m ...
 - java & python猜数字游戏对比
			
1.java版 package day03; import java.util.Random;import java.util.Scanner; /** * 猜数字游戏 * 随机生成一个1-100之间 ...
 - 基本 TCP 的回射服务器
			
实验一 代码:链接[01项目] 1. 先启动服务器,如图: 2. 然后启动客户端,如图: 3. 输出结果: [注意]:在服务器终止时,给父进程发送了一个SIGCHILD信号,这一点本例发生了,但是我们 ...
 - -webkit-,-moz-,-ms-,-o-具体指什么了?
			
-webkit-,-moz-,-ms-,-o-具体指什么了? -webkit-,-moz-,-ms-,-o-是指浏览器私有前缀名. 那为什么要有私有前缀呢? 因为制定HTML和CSS标准的组织W3C动 ...
 - 【CF662C】Binary Table(FWT)
			
[CF662C]Binary Table(FWT) 题面 洛谷 CF 翻译: 有一个\(n*m\)的表格(\(n<=20,m<=10^5\)), 每个表格里面有一个\(0/1\), 每次可 ...