2019DX#8
Solved | Pro.ID | Title | Ratio(Accepted / Submitted) |
1001 | Acesrc and Cube Hypernet | 7.32%(3/41) | |
1002 | Acesrc and Girlfriend | 4.35%(2/46) | |
1003 | Acesrc and Good Numbers 暴力打表 | 24.80%(213/859) | |
![]() |
1004 | Acesrc and Hunting | 21.74%(90/414) |
1005 | Acesrc and String Theory | 23.46%(38/162) | |
![]() |
1006 | Acesrc and Travel 换根树形DP | 12.01%(123/1024) |
1007 | Andy and Data Structure | 0.85%(1/117) | |
![]() |
1008 | Andy and Maze 随机染色+状压DP | 15.71%(33/210) |
![]() |
1009 | Calabash and Landlord | 18.99%(613/3228) |
![]() |
1010 | Quailty and CCPC | 33.48%(1060/3166) |
![]() |
1011 | Roundgod and Milk Tea | 17.70%(776/4384) |
1006 Acesrc and Travel
换根树形DP
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
#include <unordered_map>
// #include<bits/extc++.h>
// using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a) typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f;
const int mod = ; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
/**********showtime************/
const int maxn = 1e5+;
int a[maxn],b[maxn];
vector<int>mp[maxn];
int n;
ll dpa[maxn][], dpb[maxn][];
///dpa[u][0]表示a从u开始选最大所能得,需要从dpb最小的转移过来,实际是个最小值
///dpa[u][1] 表示次小值。
///dpb[u][0]表示b的,它只能从dpa最大的转移过来,实际上是个最大值。
///dpb[u][1]表示次大值 int sona[maxn], sonb[maxn];
void dfs1(int u, int fa) {
dpa[u][] = dpa[u][] = inff;
dpb[u][] = dpb[u][] = -inff;
sona[u] = sonb[u] = ;
for(int v : mp[u]) {
if(v == fa) continue;
dfs1(v, u);
if(dpb[v][] + a[u] - b[u] <= dpa[u][]) {
dpa[u][] = dpb[v][] + a[u] - b[u]; if(dpa[u][] < dpa[u][]) {
swap(dpa[u][] , dpa[u][]);
sona[u] = v;
}
}
if(dpa[v][] + a[u] - b[u] >= dpb[u][]) {
dpb[u][] = dpa[v][] + a[u] - b[u];
if(dpb[u][] > dpb[u][]) {
swap(dpb[u][], dpb[u][]);
sonb[u] = v;
}
}
}
///度数为1的节点需要特殊判断,还要区分根节点和叶子节点
if(mp[u].size() <= && fa != ) {
dpa[u][] = dpb[u][] = a[u] - b[u];
}
}
ll ans;
ll cupa[maxn], cupb[maxn];
void dfs2(int u, int fa) { ll tmp = min(dpa[u][], cupa[u]); if(mp[u].size() == && fa) tmp = cupa[u];
ans = max(ans, tmp);
for(int v : mp[u]) {
if(v == fa) continue;
cupa[v] = max(cupb[u], dpb[u][ sonb[u] == v ? : ]) + a[v] - b[v];
cupb[v] = min(cupa[u], dpa[u][ sona[u] == v ? : ]) + a[v] - b[v];
dfs2(v, u);
}
}
int main(){
int T; scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i=; i<=n; i++) scanf("%d", &a[i]);
for(int i=; i<=n; i++) scanf("%d", &b[i]);
for(int i=; i<n; i++) {
int u,v;
scanf("%d%d", &u, &v);
mp[u].pb(v);
mp[v].pb(u);
} dfs1(, );
ans = dpa[][];
if(mp[].size() == ) cupa[] = cupb[] = a[] - b[];
else cupa[] = inff, cupb[] = -inff; dfs2(, );
printf("%lld\n", ans);
for(int i=; i<=n; i++) mp[i].clear();
}
return ;
}
1008 Andy and Maze
用到了color coding技巧。
/*
* @Author: chenkexing
* @Date: 2019-08-16 22:30:07
* @Last Modified by: chenkexing
* @Last Modified time: 2019-08-16 23:30:32
*/
// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize(4)
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <ctime>
#include <random>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
// #include<bits/extc++.h>
// using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a) typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, ll> p3;
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f;
const int mod = ; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /**********showtime************/
mt19937 rnd(time());
const int maxn = 1e4+;
struct E
{
int u,v,w;
}edge[maxn];
int col[maxn];
int n,m,k;
ll dp[maxn][];
ll randgao() {
for(int i=; i<=n; i++) col[i] = rnd() % k; for(int i=; i<=n; i++){
for(int j=; j<( << k) ; j++) {
dp[i][j] = -inff;
}
int j = ( << col[i]);
dp[i][j] = ;
}
for(int j = ; j < ( << k) ; j++ ) {
for(int i=; i<=m; i++) {
int u = edge[i].u, v = edge[i].v, w = edge[i].w;
if((j & ( << col[u])))
dp[u][j] = max(dp[u][j], dp[v][j ^ ( << col[u])] + w);
if((j & ( << col[v])))
dp[v][j] = max(dp[v][j], dp[u][j ^ ( << col[v])] + w); }
} int up = ( << k) - ; ll res = -inff; for(int i=; i<=n; i++) res = max(res, dp[i][up]); return res == -inff ? - : res;
}
int main(){
int T; scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &m, &k);
for(int i=; i<=m; i++) {
scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);
}
ll ans = -;
for(int rep = ; rep <= ; rep++){
ans = max(ans, randgao());
}
if(ans == -) puts("impossible");
else printf("%lld\n", ans);
} return ;
}
2019DX#8的更多相关文章
- 2019DX#10
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 Minimum Spanning Trees 22.22%(2/9) 1002 Lin ...
- 2019dx#9
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 Rikka with Quicksort 25.85%(38/147) 1002 Ri ...
- 2019dx#7
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 A + B = C 10.48%(301/2872) 1002 Bracket Seq ...
- 2019DX#6
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 Salty Fish 16.28%(7/43) OK 1002 Nonsense Tim ...
- 2019DX#5
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 fraction 辗转相除 4.17%(7/168) ok 1002 three arr ...
- 2019dx#4
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 AND Minimum Spanning Tree 31.75%(1018/3206) ...
- 2019DX#3
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 Azshara's deep sea 凸包 6.67%(6/90)
- 2019DX#2
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 Another Chess Problem 8.33%(1/12) 1002 Beau ...
- 2019DX#1
1001 Blank 题意 有一个长度为n(n<=100)的位子,填入四种颜色,有m个限制,某个区间的颜色个数要恰好等于x个.问颜色个数的方案数. 思路 DP 四维的DP,利用滚动数组优化一维空 ...
随机推荐
- 10w数组去重,排序,找最多出现次数(精华)
package cn.tedu.javaweb.test; import java.util.*; /* * @author XueWeiWei * @date 2019/6/11 8:19 */@S ...
- 史上最全存储引擎、索引使用及SQL优化的实践
史上最全存储引擎.索引使用及SQL优化的实践 1 MySQL的体系结构概述 2. 存储引擎 2.1 存储引擎概述 2.2 各种存储引擎特性 2.2.1 InnoDB 2.2.2 MyISAM 3. 优 ...
- Iterator-Java
在Java中,Iterator的作用就是为了方便处理集合中的元素.例如获取和删除集合中的元素. 在JDK8,Iterator接口提供了如下方法: 迭代器Iterator最基本的两个方法是next()和 ...
- 内容汇总(c语言)
一,内容 常量(整型,浮点型,字符型,字符串型,符号常量) 变量(基本类型:整形,浮点型,字符型,枚举型:构造类型:数组,结构体,共用体:另外还有指针类型和NULL) 顺序结构 分支结构 循环结构 当 ...
- 深入理解JVM-java字节码文件结构剖析(练习解读字节码)
public class MyTest2 { String str = "Welcome"; private int x = 5; public static Integer in ...
- ThreadLocal为什么会内存泄漏
1.首先看下ThreadLocal的原理图: 在ThreadLocal的生命周期中,都存在这些引用. 其中,实线代表强引用,虚线代表弱引用: 2.ThreadLocal的实现:每个Thread维护一个 ...
- 两个 github 账号混用,一个帐号提交错误
问题是这样,之前有一个github帐号,因为注册邮箱的原因,不打算继续使用了,换了一个新的邮箱注册了一个新的邮箱帐号.新账号提交 就会出现下图的问题,但是原来帐号的库还是能正常提交. 方法1:添加 ...
- Redis——基础使用
Redis总体介绍: Redis特性 Redis(REmote DIctionary Server),是一个开源的内存数据库 持久化:RDB.AOF 主备复制 丰富的数据结构 Lua脚本.事务 Red ...
- Oracle中的日期函数
(一)查询系统的当前日期用sysdate,用法如下: select sysdate from dual 日期操作的三个格式: 日期-数字=日期 日期+=日期 日期-日期=数字(天数) (二)常用的日期 ...
- 弹性布局(display:flex;)属性详解
Flexbox 是 flexible box 的简称(注:意思是“灵活的盒子容器”),是 CSS3 引入的新的布局模式.它决定了元素如何在页面上排列,使它们能在不同的屏幕尺寸和设备下可预测地展现出来. ...