bzoj2466

高斯消元+搜索

很明显每个开关只能按一次,那么我们可以想到高斯消元,其实就是解异或方程组,但是最后会有一些自由元,也就是有x+y=z,x+y=z这种一样的方程就会产生自由元,那么我们爆搜自由元取值,每次把自由元回带入方程,因为形如x+y=z这样的方程就需要回带,然后就解出一组解,取最小值即可。这当然不是正解,100怎么能爆搜,正解是树形dp。

#include<bits/stdc++.h>
using namespace std;
const int Maxlen = 10000010, N = 110;
int n, ans;
int a[N][N], mark[N], val[N];
namespace IO 
{
    char buf[Maxlen], *C = buf;
    int Len;
    inline void read_in()
    {
        Len = fread(C, 1, Maxlen, stdin);
        buf[Len] = '\0';
    }
    inline void fread(int &x) 
    {
        x = 0;
        int f = 1;
        while (*C < '0' || '9' < *C) { if(*C == '-') f = -1; ++C; }
        while ('0' <= *C && *C <= '9') x = (x << 1) + (x << 3) + *C - '0', ++C;
        x *= f;
    }
    inline void read(int &x)
    {
        x = 0;
        int f = 1; char c = getchar();
        while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
        while(c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); }
        x *= f;
    }
} using namespace IO;
void dfs(int d, int tot)
{
    if(tot >= ans) return;
    if(d == 0)
    {
        ans = min(ans, tot);
        return;
    }
    if(mark[d])
    {
        int x = a[d][n + 1];
        for(int i = 1; i <= n; ++i) if(!mark[i] && a[d][i]) x ^= val[i];
        dfs(d - 1, tot + x);
    }
    else
    {
        val[d] = 0;
        dfs(d - 1, tot);
        val[d] = 1;
        dfs(d - 1, tot + 1);
    }
}
void gauss()
{
    ans = n;
    for(int now = 1; now <= n; ++now)
    {
        int pos = now;
        while(!a[pos][now] && pos <= n) ++pos;
        if(pos == n + 1) continue;
        swap(a[pos], a[now]);
        for(int i = 1; i <= n; ++i) if(a[i][now] && i != now)
            for(int j = 1; j <= n + 1; ++j) a[i][j] ^= a[now][j];
        mark[now] = 1;
    }
    dfs(n, 0);
    printf("%d\n", ans);
}
int main()
{
    while(1)
    {
        read(n);
        if(n == 0) break;
        memset(val, 0, sizeof(val));
        memset(a, 0, sizeof(a));
        memset(mark, 0, sizeof(mark));
        for(int i = 1; i <= n; ++i) 
        {
            a[i][i] = 1;
            a[i][n + 1] = 1;
        }
        for(int i = 1; i < n; ++i)
        {
            int u, v;
            read(u);
            read(v);
            a[u][v] = a[v][u] = 1;
        }
        gauss();
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/19992147orz/p/7414083.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值