洛谷 Codeforces 这题怎么一个中文题解都没有,是不是你们都认为太水了-- 思路 显然可以用dfs序把每个节点变成给一个区间的叶子节点加上某个数. 显然把叶子序列差分一下变为\(a_1,a_2,...,a_n,a_{n+1}\)之后\([l,r]\)区间加相当于\(l\)加,\(r+1\)减. 然后这就可以变成一个带权无向图. 为了让它可以搞出任意序列,我们需要使这个图连通,也就是求出最小生成树以及每条边是否可以在最小生成树内. \(kruskal\)搞一搞即可. 为什么这样一定合法呢?…
[Codeforces 1245D] Shichikuji and Power Grid (最小生成树) 题面 有n个城市,坐标为\((x_i,y_i)\),还有两个系数\(c_i,k_i\).在每个城市建立发电站需要费用\(c_i\).如果不建立发电站,要让城市通电,就需要与有发电站的城市连通.i与j之间连一条无向的边的费用是\((k_i+k_j)\)*两个城市之间的曼哈顿距离.求让每个城市都通电的最小费用,并输出任意一个方案. 分析 把选每个点的代价转成虚拟原点到这个点的边,这个套路很常见,…
Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作数. 如果有环和线,环被打开的同时,接入到线上.那就是线和环的总数-1. 如果只有环的话,把所有的环打开,互相接入,共需n次操作. #include <cstdio> #include <algorithm> using namespace std; ; int cur[maxn];…
[CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 524288 kB Source Technocup 2020 - Elimination Round 2 Tags hashing math number theory *1900 Site https://codeforces.com/problemset/problem/1225…
[Codeforces 1246B] Power Products (STL+分解质因数) 题面 给出一个长度为\(n\)的序列\(a_i\)和常数k,求有多少个数对\((i,j)\)满足\(a_i \times a_j = x^k (x \in \mathbb{N}^+)\).即这两个数乘起来恰好为一个正整数的\(k\)次方 \(a_i,n \leq 10^5\) 分析 考虑\(x^k\)的质因数分解式 , 那么每一项的指数一定是k的倍数,即 \(k|x_i\). 因此对于每个 \(a_i\)…
题意看这篇博客:https://blog.csdn.net/dreaming__ldx/article/details/88418543 思路看这篇:https://blog.csdn.net/corsica6/article/details/88115948 有个坑点,不能深搜去找具体方案,不然 test14 会 MLE(或许是本蒟蒻写丑了) 代码: #include <bits/stdc++.h> #define LL long long #define pii pair<int,…
D. Shichikuji and Power Grid</centerD.> Shichikuji is the new resident deity of the South Black Snail Temple. Her first job is as follows: There are…
很有趣的一道题. 首先可以对每个叶子进行编号.按照DFS到的顺序即可.(假设从 $1$ 到 $k$) 然后对每个点求出它管辖的所有叶子的编号.因为是DFS序所以这一定是个区间.设点 $u$ 的这个区间是 $[l_u,r_u]$. 区间加操作,考虑差分,那么每个点的操作就变成了 $l_u$ 加一个数,$r_u+1$ 减一个数.(此时也要考虑 $k+1$) 那么题目要求就变成了所有数都变成 $0$. 感受一下,把 $(l_u,r_u+1,c_u)$ 看做一条带权边,那么当且仅当选择的边构成连通图时满…
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Minimum Spanning Tree).       1.prim版本的算法   .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", co…
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX…
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation.Given a…
题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度只有一条,显然可以合并成这一条的长度. 3.长度不同的路径长度有两条,并且这个点为根,显然可以合并成这两条的长度和. 4.其他的都不合法. 注意:事实上如果第一次$DP$之后不合法要考虑换根再做一次 #include<iostream> #include<cstdio> #includ…
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If…
[BZOJ1977][BeiJing2010组队]次小生成树 Tree Description 小 C 最近学了很多最小生成树的算法,Prim 算法.Kurskal 算法.消圈算法等等. 正当小 C 洋洋得意之时,小 P 又来泼小 C 冷水了.小 P 说,让小 C 求出一个无向图的次小生成树,而且这个次小生成树还得是严格次小的,也就是说: 如果最小生成树选择的边集是 EM,严格次小生成树选择的边集是 ES,那么需要满足:(value(e) 表示边 e的权值)  这下小 C 蒙了,他找到了你,希望…
题意: 有许多油井和村庄什么的,让你使得这些村庄能连通一个油井就好了.第一行给你一个数字T代表有T组测试数据,第二行有 M , N , K ,M代表包括油井在内的村庄数,N 代表有N个 两两连通的地方.K代表有K个油井.接下来有N行,每行三个数 u , v, w, 代表 u 号和 v 是连通的 权值为 w. 思路: 以往做的题目都是只有一个源点,这道题油井的数目就是源点,所以源点不唯一.但是不要想复杂啦.其实一开始直接让所有源点并在一.再求最小生成树就好了. 代码: import java.ut…
Power Tower CodeForces - 906D 题目大意:有N个数字,然后给你q个区间,要你求每一个区间中所有的数字从左到右依次垒起来的次方的幂对m取模之后的数字是多少. 用到一个新知识,欧拉降幂定理 记住公式 $\LARGE n^x \equiv n^{\varphi(m)\ +\ x\ mod\ \varphi(m)}(mod\ m)​$这个式子当且仅当x>φ(m)时满足.那么就可以递归求解了. 暂时不太明白怎么证明 #include<iostream> #include…
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 给你平面上若干点,生成一颗完全图,让你生成一颗最小生成树.模板题.图中已经有了的边要将权值置0.代码是队友写的. 代码 #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <…
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Roman planted a tree consisting of n vertices. Each vertex contains a low…
Tree Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1954    Accepted Submission(s): 573 Problem Description There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities…
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5934    Accepted Submission(s): 1845 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do some resea…
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 383C64-bit integer IO format: %I64d      Java class name: (Any)   Iahub likes trees very much. Recently he discovered an interesting tr…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Roman planted a tree consisting of n vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the tree, each of the n …
Make It Connected CodeForces - 1095F You are given an undirected graph consisting of nn vertices. A number is written on each vertex; the number on vertex ii is aiai. Initially there are no edges in the graph. You may add some edges to this graph, bu…
Fix a Tree time limit per test2 seconds A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array…
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连通图)且这颗树中必须包含节点1 然后将这颗子树中的所有点的点权+1或-1 求把所有点权全部变为0的最小次数(n<=10^5) 题解: 因为每一次的子树中都必须有1,所以我们得知每一次变换的话1的权值都会变化 所以我们以1为根 现在,我们发现,如果一个节点的权值发生变化,那么他的父节点的权值一定发生变…
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一个域表示各个结点为根的子树是否有进行第二个操作,如果有那么该结点应该就要是0:而在进行第一个操作前,看一下子树是否有进行第二个操作,如果有就整个标记成没有并把标记上传,让该结点的父亲结点标记成进行了第二个操作. #include<cstdio> #include<cstring> #i…
Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to n. Then we represent the color of vertex v as cv. The tree root is a vertex…
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water. The vertices of the tree are numbered from 1 to n with the root at vertex 1. For each vertex, t…
题目链接: D. Tree Construction D. Tree Construction time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output During the programming classes Vasya was assigned a difficult problem. However, he doesn't k…
http://codeforces.com/problemset/problem/383/C 题目就是说,  给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val........这样类推, 给一系列操作,2是查询一个节点的值, 1是将一个节点的值+val. 首先,<dfs一遍求出dfs序以及每个点的深度, 然后建两颗线段树, 深度为奇数的建一棵, 偶数建一棵. 改变一个节点的值, 就把与他奇偶相同的那颗树全都+val, 不同的全都-val. 具体看代码>..…