Codeforces Round #364 (Div. 1) (差一个后缀自动机)

B. Connecting Universities

大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值

单独考虑每条边$(u,v)$的贡献即可, 最大贡献显然是左右两侧点的最小值.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//headconst int N = 1e6+10;
int n, k;
int a[N], sz[N];
vector<int> g[N];
ll ans;void dfs(int x, int fa) {sz[x] = a[x];for (int y:g[x]) if (y!=fa) { dfs(y,x), sz[x]+=sz[y];ans += min(sz[y], k-sz[y]);}
}int main() {scanf("%d%d", &n, &k),k*=2;REP(i,1,k) { int t;scanf("%d", &t);a[t] = 1;}REP(i,2,n) {int u, v;scanf("%d%d", &u, &v);g[u].pb(v),g[v].pb(u);}dfs(1,0);printf("%lld\n", ans);
}

C. Break Up

大意: 无向有权图有重边自环, 求删除两条边使得s与t不连通, 且两条边的边权和最小.

先求出任意一条最短路径, 边数显然不超过$n$, 暴力枚举这$n$条边然后再tarjan即可, 复杂度O(n(m+n))

算是挺简单的了, 还是打了好久, 一直卡在怎么判断删除一条边后是否连通, 后来发现tarjan后从s->t经过的桥一定是一条链, 所以直接dfs就好了, 最后还要注意边权1e9+1e9爆掉0x3f3f3f3f了.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = ~0u>>1;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//headconst int N = 3e4+10;
int n, m, S, T;
int w[N];
struct _ {int to,id;} fa[N];
vector<_> g[N];
int dfn[N], low[N], isbridge[N], clk;
void tarjan(int x, int fa, int z) {dfn[x]=low[x]=++clk;for (auto &&e:g[x]) if (e.id!=z) {int y = e.to, id = e.id;if (!dfn[y]) {tarjan(y,id,z);low[x]=min(low[x],low[y]);if (low[e.to]>dfn[x]) isbridge[id]=1;} else if (dfn[y]<dfn[x]&&id!=fa) { low[x]=min(low[x],dfn[y]);}}
}
int vis[N], c[N];
int dfs(int x) {if (x==T) return 1;for (auto e:g[x]) if (!vis[e.id]) {vis[e.id] = 1;if (dfs(e.to)) return c[e.id] = 1;}return 0;
}int main() {scanf("%d%d%d%d", &n, &m, &S, &T);REP(i,1,m) { int u, v;scanf("%d%d%d", &u, &v, w+i);g[u].pb({v,i}), g[v].pb({u,i});}queue<int> q;fa[S].to=-1, q.push(S);while (q.size()) {int x = q.front(); q.pop();for (auto &&e:g[x]) if (!fa[e.to].to) {fa[e.to]={x,e.id}, q.push(e.to);}}if (!fa[T].to) return puts("0\n0"),0;int ans = INF;vector<int> vec;for (int x=T; x!=S; x=fa[x].to) {int id = fa[x].id;memset(vis,0,sizeof vis);memset(c,0,sizeof c);vis[id] = 1;if (!dfs(S)) {if (ans>w[id]) ans = w[id],vec.clear(),vec.pb(id);continue;}memset(dfn,0,sizeof dfn);memset(isbridge,0,sizeof isbridge);clk = 0;tarjan(S,0,id);REP(i,1,m) if (c[i]&&isbridge[i]&&ans>w[id]+w[i]) {ans=w[id]+w[i];vec.clear();vec.pb(id), vec.pb(i);}}if (ans==INF) return puts("-1"),0;printf("%d\n%d\n", ans, int(vec.size()));for (int t:vec) printf("%d ", t); hr;
}

D. Huffman Coding on Segment

莫队一下, 然后将出现次数小于等于$\sqrt{n}$的暴力合, 其余的用堆合, 复杂度$O(m\sqrt{n}logn)$, 看了下最优解, 好像可以排序一下省去堆从而优化掉一个log

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head#ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endifint n, m, sqn;
int blo[N], cnt[N], sum[N], s[N], a[N];
struct _ {int l,r,id;bool operator < (const _ & rhs) const {return blo[l]^blo[rhs.l]?l<rhs.l:blo[l]&1?r<rhs.r:r>rhs.r;}
} e[N];
ll ans[N];
vector<int> q;void upd(int x, int d) {--sum[cnt[x]];cnt[x]+=d;++sum[cnt[x]];
}ll calc() {ll ans = 0;REP(i,1,sqn) s[i] = sum[i];priority_queue<int,vector<int>,greater<int> > Q;int pre = 0;REP(i,1,sqn) if (s[i]) {if (pre) {int x = pre+i;ans += x;if (x>sqn) Q.push(x);else ++s[x];--s[i], pre = 0;}if (s[i]&1) --s[i], pre = i;ans += s[i]*i;if (i*2<=sqn) s[i*2]+=s[i]/2;else {REP(j,1,s[i]/2) Q.push(i*2);}}if (pre) Q.push(pre);for (auto i:q) if (cnt[i]>sqn) Q.push(cnt[i]);while (Q.size()>1) {int x = Q.top(); Q.pop();x += Q.top(); Q.pop();ans += x, Q.push(x);}return ans;
}int main() {scanf("%d", &n), sqn = sqrt(n);REP(i,1,n) scanf("%d",a+i),++cnt[a[i]],blo[i]=i/sqn;REP(i,1,N-1) if (cnt[i]>sqn) q.pb(i);memset(cnt,0,sizeof cnt);scanf("%d", &m);REP(i,1,m) scanf("%d%d",&e[i].l,&e[i].r),e[i].id=i;sort(e+1,e+1+m);int ql=1,qr=0;REP(i,1,m) {while (ql<e[i].l) upd(a[ql++],-1);while (qr>e[i].r) upd(a[qr--],-1);while (ql>e[i].l) upd(a[--ql],1);while (qr<e[i].r) upd(a[++qr],1);ans[e[i].id]=calc();}REP(i,1,m) printf("%lld\n", ans[i]);
}

E. Cool Slogans

后缀自动机还没学, 以后补了

转载于:https://www.cnblogs.com/uid001/p/10574977.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394652.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python黑魔法

1. 赋值 In [1]: x 1...: y 21...: print x, y...: ...: x, y y, x...: print x, y 1 21 21 1 2. 列表合并 In [2]: a1 [(2,3),(3,4)]...: a2 [(4,5)]...: a a1 a2...: print a [(2, 3), (3, 4), (4, 5)] 3. 字典合并 方式1: In [3]: d1 {a: 1}...: d2 {b: 2}...: ...…

python时间差怎么转换为数字_pandas进行时间数据的转换和计算时间差并提取年月日...

#pd.to_datetime函数 #读取数据 import pandas as pd data pd.read_csv(police.csv) #将stop_date转化为datetime的格式的dataframe&#xff0c;存到stop_datetime data[stop_datetime] pd.to_datetime(data.stop_date) #自定义一个时间&#xff0c;计算时间差 data_new pd.…

人脸识别html5效果,用HTML5实现人脸识别

注&#xff1a;今天 HTML5 小组沙龙《论道 HTML5 》分享时有朋友问到一个问题&#xff0c; getUserMedia 是否会支持人脸识别&#xff0c;我当时的答案是这应该是应用来实现的功能&#xff0c;而不是规范要完成的工作。而我之前在网上看到过一篇关于 getUserMedia 和人脸识别的…

企业如何寻找最合适的托管数据中心,以维持IT和业务的增长运营

想象一下&#xff0c;当您兴奋地拿了钥匙&#xff0c;走进您刚买的新家时&#xff0c;才突然意识到新家还没通电&#xff0c;互联网宽带也还没有通&#xff0c;而想要找个电工或者别的相关技术支持人员也不见踪影。而且&#xff0c;更糟糕的是&#xff0c;您似乎还听到您附近的…

gt爵士变形步骤_代码广播简介:您可以编码为24/7的爵士节拍

gt爵士变形步骤阅读本文时&#xff0c;您可以继续阅读Code Radio。 (You can go ahead and start listening to Code Radio while you read this) Most developers I know listen to music while they code. When the meetings are over, the headphones come out.我认识的大多…

python3中format方法_[翻译]python3中新的字符串格式化方法-----f-string

从python3.6开始,引入了新的字符串格式化方式,f-字符串. 这使得格式化字符串变得可读性更高,更简洁,更不容易出现错误而且速度也更快. 在本文后面,会详细介绍f-字符串的用法. 在此之前,让我们先来复习一下python中字符串格式化的方法. python中传统的字符串格式化方法. 在pytho…

华为mate40会不会有鸿蒙系统,鸿蒙OS系统正式推送,拿华为Mate40更新后,发现了优缺点...

自从鸿蒙系统正式推送之后&#xff0c;笔者一直都带着好奇心在体验着HarmonyOS 2带来的变化&#xff0c;生怕错过惊喜&#xff0c;也担心系统本身会出现不足。因为鸿蒙系统就像是年轻人一样&#xff0c;才刚刚出炉&#xff0c;需要时间去磨练&#xff0c;然后才能发挥出真正强大…

jstack使用

jstack主要用来查看某个Java进程内的线程堆栈信息&#xff0c;根据堆栈信息我们可以定位到具体代码&#xff0c;所以它在JVM性能调优中使用得非常多&#xff0c;语法格式如下&#xff1a; jstack [option] pid jstack [option] executable core jstack [option] [server-id]rem…

如何使用TensorFlow对象检测API播放Quidditch

by Bharath Raj巴拉斯拉吉(Bharath Raj) 如何使用TensorFlow对象检测API播放Quidditch (How to play Quidditch using the TensorFlow Object Detection API) Deep Learning never ceases to amaze me. It has had a profound impact on several domains, beating benchmarks …

删除目录软链接注意事项

2019独角兽企业重金招聘Python工程师标准>>> 实验环境&#xff1a; 在root 目录下创建一个目录 1 ,并在该目录下创建一个2.txt 的文件&#xff0c;写入内容 1.txt: [rootserver ~]# mkdir 1 [rootserver ~]# echo 1.txt > 1/2.txt [rootserver ~]# tree 1 1 └─…

html如何模拟点击,Javascript 模拟点击事件(点击链接与html点击) 兼容IE/Firefox

一把情况下模拟点击一般两个方面&#xff0c;模拟点击超级连接事件firefox的兼容的函数为对HTMLAnchorElement 加入onclick事件try {// create a element so that HTMLAnchorElement is accessibledocument.createElement(a);HTMLElement.prototype.click function () {if (ty…

mvn编写主代码与测试代码

maven编写主代码与测试代码 3.2 编写主代码 项目主代码和测试代码不同&#xff0c;项目的主代码会被打包到最终的构件中&#xff08;比如jar&#xff09;&#xff0c;而测试代码只在运行测试时用到&#xff0c;不会被打包。默认情况下&#xff0c;Maven假设项目主代码位于src/…

打印速度快点的打印机_SLM推出了功能强大的新型金属3D打印机,速度快20倍

德国金属3D打印机制造商SLM Solutions在Formnext Connect贸易展览会上推出了功能强大的新系统NXG XII 600。SLM的大幅面机器配备了十二个可同时运行的1 KW激光器&#xff0c;使其速度比该公司自己的单激光SLM 280快20倍。NXG XII 600经过定制设计&#xff0c;可大量生产大型零件…

把转变为json_如何使用7行JSON将您的网站转变为移动应用程序

把转变为jsonby Ethan通过伊桑 将Web引擎融合到本机应用程序的新方法 (A New Approach for Blending Web Engine into Native Apps) What if I told you the 7 lines of JSON above, colored in orange is all you need to turn a website into a mobile app? No need to rew…

1.7Oob 继承关系中构造方法的使用

1&#xff1a;父类中最好要有一个空参数的构造方法&#xff0c;因为默认的构造方法在自定义了构造方法后就不存在了&#xff0c;需要显示的写出来。 若父类中没有空参数的构造方法&#xff0c;则子类必须有自定义的构造方法&#xff0c;且用super&#xff08;&#xff09;调用父…

JavaScript浮点运算0.2+0.1 !== 0.3

浮点运算JavaScript 本文主要讨论JavaScript的浮点运算&#xff0c;主要包括 JavaScript number基本类型二进制表示十进制浮点数的精度number 数字类型 在JavaScript中&#xff0c;数字只有number这一种类型; var intS 2,floatA 0.1; typeof intS; // number typeof floatA…

html获取data-*值,html5 获取和设置data-*属性值的四种方法讲解

1、获取id的对象2、需要获取的就是data-id 和 dtat-vice-id的值一&#xff1a;getAttribute()方法const getId document.getElementById(getId);// //getAttribute()取值属性console.log(getId.getAttribute("data-id"));//console.log(getId.getAttribute("da…

三菱模拟量输入与输出程序_初学PLC是学习西门子还是三菱?

PLC的种类繁多&#xff0c;品牌大多分为欧系、日系、美系。德系PLC以西门子为主&#xff0c;日系有三菱、欧姆龙、松下……&#xff0c;美系有罗克韦尔(A-B)通用电气(GE)公司、莫迪(MODICON)公司等。美国和欧洲的PLC技术是在相互隔离情况下独立研究开发的&#xff0c;因此美国和…

性能测试十四:Xshell链接linux虚拟机

一、先装一个linux虚拟机 VBoxcentos1、先下载Linux镜像文件的ovf或者OVA文件2、打开vbox&#xff0c;点击菜单栏“管理”-“导入虚拟电脑3、选择解压路径中的ovf或者OVA文件&#xff0c;点击下一步 4、点击“导入”&#xff0c;等待完成5、导入成功后&#xff0c;选择新导入的…

代码编写工具_我希望在开始编写代码时就已经知道的工具:已复习

代码编写工具by Mario Hoyos通过马里奥霍约斯(Mario Hoyos) 我希望在开始编写代码时就已经知道的工具&#xff1a;已复习 (Tools I wish I had known about when I started coding: Revisited) A few days ago, I wrote this article for freeCodeCamp which has since gone o…