P4151 [WC2011]最大XOR和路径线性基

news/2024/7/20 20:23:13 标签: 深度优先, 图论, 算法

link
注意insert的是 res ^ val[v] ^ w

vector <ull> B;
void insert(ull x) {
    for(auto b : B)
        x = min(x, x ^ b);
    for(auto &b : B)
        b = min(b, x ^ b);
    if(x)
        B.push_back(x);
}
int head[maxn], cnt;
struct Edge {
    int to, next;
    ull w;
}edge[maxm];
void add_edge(int u, int v, ull w) {
    cnt++;
    edge[cnt].to = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt;
}
int vis[maxn], val[maxn];
void dfs(int u, ull res) {
    vis[u] = 1;
    val[u] = res;
    for(int i = head[u]; i; i = edge[i].next) {
        int v = edge[i].to, w = edge[i].w;
        if(!vis[v]) dfs(v, res ^ w);
        else insert(res ^ val[v] ^ w);
    }
}
void solve() {
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        add_edge(u, v, w);
        add_edge(v, u, w);
    }
    dfs(1, 0ull);
    ull ans = val[n];
    for(auto b : B) {
        ans = max(ans, ans ^ b);
    }
    cout << ans << endl;
}

http://www.niftyadmin.cn/n/1764888.html

相关文章

720012指令

在不开心做的要价1000元的希捷专修软件中,所有F3T(即7200.11 7200.12 5400.6等指令模式下为F3T的)指令. 用CommMonitor软件跟踪出来的,每个都测试可用. 还原设置&#xff1a;F&#xff0c;&#xff0c;22 代修复通病&#xff1a;m0,2,2,0,0,0,0,22 重建译码表&#xff1a;m0,6,…

P4035 [JSOI2008]球形空间产生器 ——高斯消元

Link 一道高斯消元的题目&#xff0c;其实重点还是在如何得出方程组也就是增广矩阵。 代码 const double EPS1e-7; inline int gauss(double a[][N], bool l[], double ans[], const int& n) {int res 0, r 0;for(int i 0; i < n; i)l[i] false;for(int i 0; i …

Bootstrap使用后笔记

Bootstrap Modal 垂直居中 在 bootstrap.js中修改如下代码&#xff1a; Modal.prototype.adjustDialog function () { var modalIsOverflowing this.$element[0].scrollHeight > document.documentElement.clientHeight this.$element.css({ paddingLeft: !this.bodyIsOve…

208. 开关问题 —— 高斯消元

Link 高斯消元问题 其实就是对于每盏灯 xix_ixi​&#xff0c;均有一个数组aaa 如 aaa {0, 1, 1, 0}表示对第 iii 盏灯操作&#xff0c;则第2&#xff0c;3盏灯也会变化&#xff0c;于是构成一个二维矩阵a[i][j]a[i][j]a[i][j]&#xff0c;a[i][j]1a[i][j] 1a[i][j]1则表示操…

Java Web中Kaptcha实现验证码

首先进行导入相应的jar包&#xff1a; 1.如果是maven项目&#xff0c;在你的pom文件中进行添加如下代码&#xff0c;将自动下载jar包到你的工程中&#xff1a; <dependency> <groupId>com.github.penggle</groupId> <artifactId>…

210. 异或运算 ——线性基

Link 线性基板子&#xff0c;求一个数列可以得到的第 kkk 小异或值。 int t 0; int n; vector<ull> b; void insert(ull x) {for(auto i : b)x min(x, x ^ i);for(auto& i : b)i min(i, x ^ i);if(x)b.pb(x); } void solve() {printf("Case #%d:\n", t…

LeetCode Invert Binary Tree 反转二叉树

思路&#xff1a;递归解决&#xff0c;在返回root前保证该点的两个孩子已经互换了。注意可能给一个Null。 C 1 /**2 * Definition for a binary tree node.3 * struct TreeNode {4 * int val;5 * TreeNode *left;6 * TreeNode *right;7 * TreeNode(int x…

P3265 [JLOI2015]装备购买 —— 线性基

Link 实数下线性基 #define double long double double a[N][N]; struct node {double a[550];int w;bool operator < (const node &x) const {return w < x.w;} }p[550]; int n, m; double b[N][N]; bool insert(int m, double c[]) {for(int i m-1; i > 0; i-…