洛谷

Codeforces


这题怎么一个中文题解都没有,是不是你们都认为太水了……


思路

显然可以用dfs序把每个节点变成给一个区间的叶子节点加上某个数。

显然把叶子序列差分一下变为\(a_1,a_2,...,a_n,a_{n+1}\)之后\([l,r]\)区间加相当于\(l\)加,\(r+1\)减。

然后这就可以变成一个带权无向图。

为了让它可以搞出任意序列,我们需要使这个图连通,也就是求出最小生成树以及每条边是否可以在最小生成树内。

\(kruskal\)搞一搞即可。

为什么这样一定合法呢?

考虑以\(n+1\)为根,把树变成一棵有根树。

每个点可以加上任意数,同时父亲减去这个数。

显然最后可以弄出任意和为0的差分序列,也就是任意原数列。


代码

#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define templ template<typename T>
#define sz 202020
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char __sr[1<<21],__z[20];int __C=-1,__zz=0;
inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
inline void print(register int x)
{
if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
while(__z[++__zz]=x%10+48,x/=10);
while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifndef ONLINE_JUDGE
cout<<(clock()-t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std; int n;
ll c[sz]; namespace getans
{
int fa[sz];
void init(){rep(i,1,n) fa[i]=i;}
int getfa(int x){return x==fa[x]?x:fa[x]=getfa(fa[x]);}
bool ok[sz];
struct hh{int f,t;ll w;int id;}edge[sz];
inline bool operator < (const hh &x,const hh &y){return x.w<y.w;}
ll ans;
void work(int L,int R)
{
rep(j,L,R)
{
int x=getfa(edge[j].f),y=getfa(edge[j].t);
if (x==y) continue;
ok[edge[j].id]=1;
}
rep(j,L,R)
{
int x=getfa(edge[j].f),y=getfa(edge[j].t);
if (x==y) continue;
ans+=edge[j].w;
fa[x]=y;
}
}
int MAIN()
{
init();
sort(edge+1,edge+n+1);
int lst=1;
rep(i,1,n)
{
if (edge[i].w==edge[lst].w) continue;
work(lst,i-1);
lst=i;
}
work(lst,n);
cout<<ans<<' ';
int cnt=0;
rep(i,1,n) if (ok[i]) ++cnt;
printf("%d\n",cnt);
rep(i,1,n) if (ok[i]) printf("%d ",i);
return 0;
}
} namespace build
{
struct hh{int t,nxt;}edge[sz<<1];
int head[sz],ecnt;
void make_edge(int f,int t)
{
edge[++ecnt]=(hh){t,head[f]};
head[f]=ecnt;
edge[++ecnt]=(hh){f,head[t]};
head[t]=ecnt;
}
int dfn[sz],low[sz],pre[sz],cnt;
int a[sz],cc;
#define v edge[i].t
void dfs(int x,int fa)
{
pre[dfn[x]=++cnt]=x;
bool flg=0;
go(x) if (v!=fa) flg=1,dfs(v,x);
low[x]=cnt;
if (!flg) a[++cc]=dfn[x];
}
#undef v
void init()
{
int x,y;
rep(i,1,n-1) read(x,y),make_edge(x,y);
dfs(1,0);
rep(i,1,n)
{
int L=lower_bound(a+1,a+cc+1,dfn[i])-a;
int R=upper_bound(a+1,a+cc+1,low[i])-a;
getans::edge[i]=(getans::hh){L,R,c[i],i};
}
}
} int main()
{
file();
read(n);
rep(i,1,n) read(c[i]);
build::init();
return getans::MAIN();
}

Codeforces 1120D Power Tree [最小生成树]的更多相关文章

  1. [Codeforces 1245D] Shichikuji and Power Grid (最小生成树)

    [Codeforces 1245D] Shichikuji and Power Grid (最小生成树) 题面 有n个城市,坐标为\((x_i,y_i)\),还有两个系数\(c_i,k_i\).在每个 ...

  2. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

  3. [CodeForces - 1225D]Power Products 【数论】 【分解质因数】

    [CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...

  4. [Codeforces 1246B] Power Products (STL+分解质因数)

    [Codeforces 1246B] Power Products (STL+分解质因数) 题面 给出一个长度为\(n\)的序列\(a_i\)和常数k,求有多少个数对\((i,j)\)满足\(a_i ...

  5. Codeforces 1120D (树形DP 或 最小生成树)

    题意看这篇博客:https://blog.csdn.net/dreaming__ldx/article/details/88418543 思路看这篇:https://blog.csdn.net/cor ...

  6. Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid 最小生成树

    D. Shichikuji and Power Grid</centerD.> Shichikuji is the new resident deity of the South Blac ...

  7. CF1120D Power Tree(构造题,差分,最小生成树)

    很有趣的一道题. 首先可以对每个叶子进行编号.按照DFS到的顺序即可.(假设从 $1$ 到 $k$) 然后对每个点求出它管辖的所有叶子的编号.因为是DFS序所以这一定是个区间.设点 $u$ 的这个区间 ...

  8. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

  9. HDU 4408 Minimum Spanning Tree 最小生成树计数

    Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. Java this与super的仇与恨

    刚开始学习面向对象的时候,看到this和super觉得this就是指当前对象,super指最近的父级对象.觉得用处不大. 后来,随着学习的深入,看别人的代码越来越多,发现this和super的使用率贼 ...

  2. LoadRunner 压力测试使用基础步骤

    一.新建脚本 二.新建脚本-选择协议,这里选择Web (HTTP/HTML) 三.开始录制(指定程序与URL) 四.场景设计(设计虚拟用户访问场景) 五.运行情况(可以看到运行结果) 六.分析报告(总 ...

  3. 复杂度定义 The Definition of Complexity

    The upper bound   Big-O: Definition: f(n) is in O(g(n)) if there are constants c0 and N0 such that f ...

  4. iOS WebView 加载本地资源(图片,文件等)

    https://www.cnblogs.com/dhui69/p/5596917.html iOS WebView 加载本地资源(图片,文件等) NSString *path = [[NSBundle ...

  5. 清明培训 清北学堂 DAY1

    今天是李昊老师的讲授~~ 总结了一下今天的内容: 1.高精度算法 (1)   高精度加法 思路:模拟竖式运算 注意:进位 优化:压位 程序代码: #include<iostream>#in ...

  6. 修改帝国cms栏目后,如何更新

    修改栏目后,要依次做如下更新: 1.    2.  3.    如果只是修改了栏目里的属性,只需要做第三步就行了

  7. [python]python3.7中文手册

    https://pythoncaff.com/docs/tutorial/3.7.0

  8. pm2自动部署

    配置pm2自动部署前,请确保已经能够ssh免密登录服务器. 一.创建ecosystem.json { "apps" : [{ "name" : "HT ...

  9. idea搭建springboot

     1.创建新项目 2.继续项目配置 Name:项目名称Type:我们是Maven构建的,那么选择第一个Maven ProjectPackaging:打包类型,打包成Jar文件Java Version: ...

  10. 第十九节、基于传统图像处理的目标检测与识别(词袋模型BOW+SVM附代码)

    在上一节.我们已经介绍了使用HOG和SVM实现目标检测和识别,这一节我们将介绍使用词袋模型BOW和SVM实现目标检测和识别. 一 词袋介绍 词袋模型(Bag-Of-Word)的概念最初不是针对计算机视 ...