bzoj4690 Never Wait for Weights

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4690

【题解】

带权并查集

fa[x]表示x的父亲,a[x]表示x到x的父亲多/少多少

那么找祖先的时候算一下到祖先多少,然后路径压缩。

合并的时候注意让fa[fx]=fy的时候,a[fx]是多少(这个可以算的)

然后查询直接做就行了。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, q;

int fa[M], a[M];
inline int getf(int x) {
    int i=x, j, jj, res = 0;
    while(fa[x] != x) {
        res = res + a[x];
        x = fa[x];
    }
    while(i != x) {
         j = fa[i]; jj = a[i];
        fa[i] = x;
        a[i] = res;
        res -= jj;
        i = j;
    }
    return x;
}

int main() {
    while(cin >> n >> q && (n+q)) {
        for (int i=1; i<=n; ++i) fa[i] = i, a[i] = 0;
        char opt[3]; int x, y, z;
        while(q--) {
            scanf("%s", opt);
            if(opt[0] == ‘!‘) {
                scanf("%d%d%d", &x, &y, &z);
                int fx = getf(x), fy = getf(y);
                if(fx == fy) continue;
                fa[fx] = fy;
                a[fx] = z-a[x]+a[y];
            } else {
                scanf("%d%d", &x, &y);
                int fx = getf(x), fy = getf(y);
                if(fx != fy) puts("UNKNOWN");
                else printf("%d\n", a[x]-a[y]);
            }
        }
    }

    return 0;
}

时间: 2024-10-20 17:07:22

bzoj4690 Never Wait for Weights的相关文章

bzoj4690: Never Wait for Weights 并查集

裸带权并查集 #include<cstdio> #define N 100005 int m,i,j,s,t,u,d[N],p[N]; char k; int find(int i){ if(p[i]^i&&find(p[i])) d[i]+=d[p[i]]; return p[i]=p[p[i]]; } int main(){ while(scanf("%d%d",&s,&m)&&s){ for(;s;--s) d[p[s]

【BZOJ-4690】Never Wait For Weights 带权并查集

4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][Status][Discuss] Description 在实验室中,Nathan Wada作为助手的职责是测定两个样品的重量差异.当样品的差异很小时,使用天平能比使用弹簧秤得到更精确的结果,所以他只使用天平来测得一些样品的重量差.他偶尔会被询问一些样品的重量差,而他能否回答这些问题取决于在回答相应

UVA 10154:Weights and Measures

Weights and Measures 题目链接: 题意: 给出一些乌龟,每只乌龟有两个值,重量a和力量b,每只乌龟可以承受b-a的重量,求把这些乌龟竖直叠在一起最多能叠多少只乌龟. 题解: 设乌龟X和乌龟Y都是答案所求的乌龟数中的两只,且Xb>Yb,可以发现min(Xb-Xa-Ya,Yb-Ya)恒大于min(Yb-Ya-Xa,Xb-Xa),因此力量大的乌龟一定在下边,可以将乌龟按重量排序.设dp[i][j]为前 i 只乌龟叠到第 j 层所剩可利用资源的最大值即可,由于数据比较大可采用滚动数组

uva 10154 - Weights and Measures【dp】qi

题意:uva 10154 - Weights and Measures 题意:有一些乌龟有一定的体重和力量,求摞起来的最大高度.力量必须承受其上面包括自己的全部的重量. 分析:先按其能举起来的力量从小到大排序 然后定义dp[i] 表示摞起来 i 只乌龟的最小质量. 然后转移就是每次用遍历O(n)的复杂度找最小的,然后记录,保存最大值即可. AC代码: #include<iostream> #include<cstdio> #include<cstring> #inclu

codeforces 339C Xenia and Weights(dp或暴搜)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Xenia and Weights Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts

codeforces339C - Xenia and Weights 暴搜

题意:给你无穷多个1-10的,从 1-m不停的放到天平两端,两次连续放置要在不同的天平和放不同的重量,使得每一次放置这边的天平都比对面的重量多. 解题思路: 1)暴搜,如果估算的话还是过不了的,但实际情况比估算好太多了   62ms 1 // File Name: 339c.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月03日 星期日 16时38分23秒 4 5 #include<vector> 6 #include<list

CS231n笔记4-Data Preprocessing, Weights Initialization与Batch Normalization

Data Preprocessing, Weights Initialization与Batch Normalization Data Preprocessing Weights Initialization与Batch Normalization 数据预处理Data Preprocessing 权重初始化Weights Initialization 让权重初始化为0 0方差1e-2标准差 0方差1标准差 Xavier Initialization 再改进 批归一化Batch Normaliza

Nested weights are bad for performance

警告信息“Nested weights are bad for performance”的消除方法 原因分析:在布局进行嵌套使用时,父布局与子布局都使用了android:layout_weight,但不是必须使用时,便会出现如题所示的警告信息. 解决方法:根据实际情况,去除子布局中非必须使用的android:layout_weight. Nested weights are bad for performance

笔记:Beyond sharing weights for deep domain adaptation

Based on deep learning, instead of sharing weights across source and target domains, this work proposed a two-stream architecture where different streams operate on different domains, with an additional loss function to imply the relationships across