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的更多相关文章

  1. 2019DX#10

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 Minimum Spanning Trees 22.22%(2/9)   1002 Lin ...

  2. 2019dx#9

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 Rikka with Quicksort 25.85%(38/147)   1002 Ri ...

  3. 2019dx#7

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 A + B = C 10.48%(301/2872)   1002 Bracket Seq ...

  4. 2019DX#6

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 Salty Fish 16.28%(7/43)  OK 1002 Nonsense Tim ...

  5. 2019DX#5

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 fraction 辗转相除 4.17%(7/168) ok  1002 three arr ...

  6. 2019dx#4

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 AND Minimum Spanning Tree 31.75%(1018/3206)   ...

  7. 2019DX#3

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 Azshara's deep sea 凸包 6.67%(6/90)  

  8. 2019DX#2

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 Another Chess Problem 8.33%(1/12)   1002 Beau ...

  9. 2019DX#1

    1001 Blank 题意 有一个长度为n(n<=100)的位子,填入四种颜色,有m个限制,某个区间的颜色个数要恰好等于x个.问颜色个数的方案数. 思路 DP 四维的DP,利用滚动数组优化一维空 ...

随机推荐

  1. Linux学习笔记06之DNS

    一.DNS概念:Domain Name System(域名系统) 是互联网上作为域名和IP地址相互映射的一个分布式数据库 二.DNS功能: 完成IP地址和域名之间的一个映射 三.DNS分类: 1.静态 ...

  2. codeforces 371A K-Periodic Array

    很简单,就是找第i位.i+k位.i+2*k位...........i+m*k位有多少个数字,计算出每个数出现的次数,找到出现最多的数,那么K-Periodic的第K位数肯定是这个了.这样的话需要替换的 ...

  3. wpf界面按钮自动点击

    Button Button = new Button();Button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));//在按钮生成时便会自动触 ...

  4. JMS入门简介

    一.JMS是什么 1.JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中 ...

  5. 1、JAVA的小白之路

    大学的时光过得很快,转眼我已经大二了,在大一时,学习了C\C++,对于语言有一定基础,在未来的道路上,我需要攒足干劲,积累足够的知识和技能,去走上社会. 我的第一任大学班主任告诉我:“作为程序员,你至 ...

  6. 从零开发一款自己的小程序UI组件库(二)

    写在前面:从零开发一款自己的小程序UI组件库(一) 上节我们讲到初始化组件库模板.模板文件概述.模板上传npm以及npm包文件下载至本地并运用到项目.这节我们继续,内容主要有基础UI组件库的搭建(bu ...

  7. Jmeter 01 Jmeter下载安装及入门

    jmeter简介 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域.--百度百科 下载 下载 ...

  8. 洛谷 P2024 [NOI2001]食物链

    题意简述 有人用两种说法对这 N 个动物所构成的食物链关系进行描述: 1."1 X Y",表示 X 和 Y 是同类. 2."2 X Y",表示 X 吃 Y . ...

  9. React 多副本问题

    Element ref was specified as a string (MySider) but no owner was set. This could happen for one of t ...

  10. 如何在onCreate中获取View的高度和宽度

    如何在onCreate中获取View的高度和宽度 原文链接:http://mp.weixin.qq.com/s?__biz=MzAwODE1NTI2MQ==&mid=2247483676&am ...