uoj#87. mx的仙人掌
- //Achen
- #include<bits/stdc++.h>
- #define For(i,a,b) for(int i=(a);i<=(b);i++)
- #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
- #define Formylove return 0
- const int N=2e6+,mod=;
- typedef long long LL;
- typedef double db;
- using namespace std;
- int n,m,Q;
- template<typename T> void read(T &x) {
- char ch=getchar(); T f=; x=;
- while(ch!='-'&&(ch<''||ch>'')) ch=getchar();
- if(ch=='-') f=-,ch=getchar();
- for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-''; x*=f;
- }
- LL ans,Hdis[N];
- int isnode[N];
- struct VMtree{
- int ecnt,fir[N],nxt[N],to[N]; LL val[N];
- void add(int u,int v,LL w) {
- nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
- nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u; val[ecnt]=w;
- //printf("%d %d %lld\n",u,v,w);
- }
- LL f[N];
- int que[N];
- void dfs(int x,int fa) {
- f[x]=;
- for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa)
- dfs(to[i],x);
- if(x<=n) {
- for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
- int y=to[i];
- ans=max(ans,f[y]+val[i]+f[x]); f[x]=max(f[x],f[y]+val[i]);
- }
- }
- else {
- int ql=,qr=;
- for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
- int y=to[i];
- f[x]=max(f[x],f[y]+val[i]);
- while(ql<=qr&&Hdis[y]-Hdis[que[ql]]>Hdis[x]-(Hdis[y]-Hdis[que[ql]])) ql++;
- if(ql<=qr) ans=max(ans,Hdis[y]-Hdis[que[ql]]+f[que[ql]]+f[y]);
- while(ql<=qr&&f[y]-Hdis[y]>=f[que[qr]]-Hdis[que[qr]]) qr--;
- que[++qr]=y;
- }
- for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
- int y=to[i];
- while(ql<=qr&&Hdis[x]-(Hdis[que[ql]]-Hdis[y])>Hdis[que[ql]]-Hdis[y]) ql++;
- if(ql<=qr) ans=max(ans,Hdis[x]-(Hdis[que[ql]]-Hdis[y])+f[que[ql]]+f[y]);
- }
- } if(isnode[x]) ans=max(ans,f[x]);
- }
- }V;
- int tot,dfn[N],a[N];
- bool cmp(const int &A,const int &B) { return dfn[A]<dfn[B]; }
- struct Tree {
- int ecnt,fir[N],nxt[N],to[N]; LL val[N];
- void add(int u,int v,LL w) {
- nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
- nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u; val[ecnt]=w;
- //printf("%d %d %lld\n",u,v,w);
- }
- int R[N],f[N][]; LL H[N];
- void lca(int &cc,int x,int y) {
- if(R[x]<R[y]) swap(x,y);
- Rep(i,,) if(R[f[x][i]]>=R[y])
- x=f[x][i];
- if(x==y) { a[++cc]=x; return ; }
- Rep(i,,) if(f[x][i]!=f[y][i])
- x=f[x][i],y=f[y][i];
- if(f[x][]>n) {
- a[++cc]=x; a[++cc]=y;
- }
- a[++cc]=f[x][]; return ;
- }
- int dfk,sz[N];
- int dfs(int x,int fa) {
- sz[x]=;
- f[x][]=fa;
- R[x]=R[fa]+;
- dfn[x]=++dfk;
- For(i,,) f[x][i]=f[f[x][i-]][i-];
- for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
- H[to[i]]=H[x]+val[i];
- dfs(to[i],x);
- sz[x]+=sz[to[i]];
- }
- }
- bool in(int a,int b) { return dfn[a]>=dfn[b]&&dfn[a]<dfn[b]+sz[b]; }
- int sta[N],top;
- void solve() {
- int cnt,sz,rt=;
- read(cnt); sz=cnt;
- For(i,,cnt) { read(a[i]); isnode[a[i]]=; }
- sort(a+,a+cnt+,cmp);
- For(i,,cnt-) {
- lca(sz,a[i],a[i+]);
- }
- sort(a+,a+sz+,cmp);
- cnt=unique(a+,a+sz+)-(a+);
- For(i,,cnt) {
- if(!rt||R[a[i]]<R[rt]) rt=a[i];
- while(top&&!in(a[i],sta[top])) top--;
- if(top) V.add(sta[top],a[i],H[a[i]]-H[sta[top]]);
- sta[++top]=a[i];
- } while(top) top--;
- ans=; V.dfs(rt,);
- printf("%lld\n",ans);
- For(i,,cnt) { isnode[a[i]]=; V.fir[a[i]]=; } V.ecnt=;
- }
- }T;
- struct Cactus {
- int ecnt,fir[N],nxt[N],to[N],val[N],vis[N];
- void add(int u,int v,int w) {
- nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
- nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u; val[ecnt]=w;
- }
- int dfk,dfn[N],low[N],sta[N],top;
- void tarjan(int x) {
- dfn[x]=low[x]=++dfk;
- for(int i=fir[x];i;i=nxt[i]) if(!vis[i]) {
- sta[++top]=i;
- vis[i]=vis[i^]=;
- if(!dfn[to[i]]) {
- tarjan(to[i]);
- low[x]=min(low[x],low[to[i]]);
- if(low[to[i]]>=dfn[x]) {
- LL hc=,nc=,cc=;
- T.add(x,++tot,);
- Rep(j,top,) {
- hc+=val[sta[j]]; cc++;
- if(sta[j]==i) break;
- }
- Hdis[tot]=hc;
- while(top) {
- int j=sta[top--];
- if(to[j]!=x) {
- if(cc==) T.add(tot,to[j],hc);
- else T.add(tot,to[j],min(hc-nc,nc));
- Hdis[to[j]]=nc;
- }
- nc+=val[j];
- if(j==i) break;
- }
- }
- }
- else low[x]=min(low[x],dfn[to[i]]);
- }
- }
- void init() { ecnt=; tot=n; }
- }C;
- struct hash {
- int ecnt;
- struct edge{ int u,v,w; }e[N];
- vector<int>vc[N];
- void ins(int u,int v,int w) {
- if(u>v) swap(u,v);
- int hs=(u*+v)%mod,up=vc[hs].size();
- For(i,,up-) {
- int ec=vc[hs][i];
- if(e[ec].u==u&&e[ec].v==v) { e[ec].w=min(e[ec].w,w); return; }
- }
- e[++ecnt]=(edge){u,v,w}; vc[hs].push_back(ecnt);
- }
- void add() {
- For(i,,ecnt) C.add(e[i].u,e[i].v,e[i].w);
- }
- }H;
- int main() {
- //freopen("1.in","r",stdin);
- //freopen("1.out","w",stdout);
- read(n); read(m);
- For(i,,m) {
- int u,v,w;
- read(u); read(v); read(w);
- C.add(u,v,w);
- H.ins(u,v,w);
- }
- C.init(); H.add();
- C.tarjan();
- T.dfs(,);
- read(Q);
- For(i,,Q) T.solve();
- Formylove;
- }
写完花了1.5h找bug发现是数组开小了。
其实是因为太长实在不想看一直各种磨蹭各种骚扰其他同学。
无脑圆方树+虚树+单调队列优化dp。
如果两个人的lca是方点要把方点的两个圆儿子加进去。
http://uoj.ac/problem/87
uoj#87. mx的仙人掌的更多相关文章
- UOJ.87.mx的仙人掌(圆方树 虚树)(未AC)
题目链接 本代码10分(感觉速度还行..). 建圆方树,预处理一些东西.对询问建虚树. 对于虚树上的圆点直接做:对于方点特判,枚举其所有儿子,如果子节点不在该方点代表的环中,跳到那个点并更新其val, ...
- SHOI2008 cactus仙人掌图 和 UOJ87 mx的仙人掌
cactus仙人掌图 题目描述 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在图上不重复经过任何一 ...
- Solution -「UOJ #87」mx 的仙人掌
\(\mathcal{Description}\) Link. 给出含 \(n\) 个结点 \(m\) 条边的仙人掌图.\(q\) 次询问,每次询问给出一个点集 \(S\),求 \(S\) 内 ...
- UOJ #86 mx的组合数 (数位DP+NTT+原根优化)
题目传送门 matthew99神犇的题解讲得非常清楚明白,跪烂Orzzzzzzzzzzzzz 总结一下,本题有很多重要的突破口 1.Lucas定理 看到n,m特别大但模数特别小时,容易想到$lucas ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- UOJ#23. 【UR #1】跳蚤国王下江南 仙人掌 Tarjan 点双 圆方树 点分治 多项式 FFT
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ23.html 题目传送门 - UOJ#23 题意 给定一个有 n 个节点的仙人掌(可能有重边). 对于所有 ...
- UOJ#290. 【ZJOI2017】仙人掌 仙人掌,Tarjan,计数,动态规划,树形dp,递推
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ290.html 题解 真是一道好题! 首先,如果不是仙人掌直接输出 0 . 否则,显然先把环上的边删光. ...
- uoj#158. 【清华集训2015】静态仙人掌
http://uoj.ac/problem/158 预处理dfs序,询问转为区间1的个数,用可持久化bitset预处理出所有可能的修改对应哪些位置,然后用一个bitset维护当前每个点的状态,修改时可 ...
- uoj#290. 【ZJOI2017】仙人掌(数数+仙人掌+树形dp)
传送门 这图可以说是非常形象了2333 模拟赛的时候打了个表发现为一条链的时候答案是\(2^{n-2}\)竟然顺便过了第一个点 然后之后订正的时候强联通分量打错了调了一个上午 首先不难发现我们可以去掉 ...
随机推荐
- Windows读写文件的猫腻
这里主要涉及对于回车换行的讨论. 回车:\r 换行:\n Windows读写文件分为普通文件读写和二进制文件读写. 如果以二进制的方式读写文件(如rb, wb),将会完全的把文件内容读出来,不做任何处 ...
- /usr/lib/update-notifier/updates-available
/usrb/update-notifier/update-motd-updates-available: 49: /usrb/update-notifier/update-motd-updates- ...
- 【Head First Servlets and JSP】笔记9:属性的作用域、线程安全
什么是属性? 属性和参数 属性的3个作用域 属性API 属性不好的一面 1.到底什么是属性(Attribute)? 属性就是一个对象,可以被设置(bound,也可以叫绑定)到另外三个servlet A ...
- 跨平台移动开发_PhoneGap API 事件类型
PhoneGap API Events backbuttondevicereadymenubuttonpauseresumeonlineofflinebatterycriticalbatterylow ...
- 生信基础概念之unique reads VS multi-mapping reads
unique reads:在参考组上只有一个匹配点 multi-mapping reads:在参考组上有多个匹配点 下面是tophat的一个结果案例: Reads: Input : Mapped : ...
- 记录python面试题
闲来无事,记录一下曾经以及深刻的面试题 记录一下我记忆比较深的面试题,以后若用到python相关还能细细把玩 搜狐面试题: 一.写一个缓存优化策略 解答:这个题主要考察对lru_cache的理解,所以 ...
- PHP实现链式操作
什么是链式操作 我们经常会在一些应用框架中看到如下代码: $db = new Database; $db->where('cid = 9')->order('aid desc')-> ...
- 吴恩达深度学习笔记(五) —— 优化算法:Mini-Batch GD、Momentum、RMSprop、Adam、学习率衰减
主要内容: 一.Mini-Batch Gradient descent 二.Momentum 四.RMSprop 五.Adam 六.优化算法性能比较 七.学习率衰减 一.Mini-Batch Grad ...
- FIND_IN_SET的简单使用
FIND_IN_SET(str,strlist)函数 str 要查询的字符串 strlist 字段名 参数以”,”分隔 如 (1,2,6,8) 查询字段(strlist)中包含(str)的结果,返回结 ...
- Python日期时间函数
所有日期.时间的api都在datetime模块内. 1. 日期输出格式化 datetime => string import datetime now = datetime.datetime.n ...