Code:

#include <bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 300000
#define ll long long
using namespace std;
vector<int>G[maxn];
ll sumv[maxn];
ll Sum(ll k) {
return k*(k+1)/2;
}
ll calc(ll k) {
return 1ll*(k+1)*Sum(k)-1ll*sumv[k];
}
void Initialize() {
for(int i=1;i<maxn;++i) sumv[i]=sumv[i-1]+1ll*i*i;
}
int main() {
// setIO("input");
int n,m;
scanf("%d",&n),m=n-1;
for(int i=1;i<=m;++i) {
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(i), G[b].push_back(i);
}
Initialize();
ll ans=(ll)((ll)(1ll*m*n+n)*m)/2;
ans -= (ll) calc(m);
for(int i=1;i<=n;++i) {
sort(G[i].begin(), G[i].end());
for(int j=0;j<G[i].size();++j) {
if(j==0) {
ans-=Sum((ll)G[i][j]-1);
}
if(j>0) {
ans-=Sum((ll)G[i][j]-G[i][j-1]-1);
}
if(j==G[i].size()-1) {
ans-=Sum((ll)m-G[i][j]);
}
}
}
printf("%lld\n",ans);
return 0;
}

  

Comet OJ - Contest #6 C 一道树题 数学 + 推导的更多相关文章

  1. Comet OJ - Contest #14 转转的数据结构题 珂朵莉树+树状数组

    题目链接: 题意:有两个操作 操作1:给出n个操作,将区间为l到r的数字改为x 操作2:给出q个操作,输出进行了操作1中的第x到x+y-1操作后的结果 解法: 把询问离线,按照r从小到大排序 每次询问 ...

  2. Comet OJ - Contest #4 D求和 思维题

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...

  3. Comet OJ - Contest #4--前缀和

    原题:Comet OJ - Contest #4-B https://www.cometoj.com/contest/39/problem/B?problem_id=1577传送门 一开始就想着暴力打 ...

  4. Comet OJ - Contest #11 题解&赛后总结

    Solution of Comet OJ - Contest #11 A.eon -Problem designed by Starria- 在模 10 意义下,答案变为最大数的最低位(即原数数位的最 ...

  5. Comet OJ - Contest #13-C2

    Comet OJ - Contest #13-C2 C2-佛御石之钵 -不碎的意志-」(困难版) 又是一道并查集.最近做过的并查集的题貌似蛮多的. 思路 首先考虑,每次处理矩形只考虑从0变成1的点.这 ...

  6. Comet OJ - Contest #13 「火鼠的皮衣 -不焦躁的内心-」

    来源:Comet OJ - Contest #13 芝士相关: 复平面在信息学奥赛中的应用[雾 其实是道 sb 题??? 发现原式貌似十分可二项式定理,然后发现确实如此 我们把 \(a^i\) 替换成 ...

  7. Comet OJ - Contest #13 「佛御石之钵 -不碎的意志-」(hard)

    来源:Comet OJ - Contest #13 一眼并查集,然后发现这题 tmd 要卡常数的说卧槽... 发现这里又要用并查集跳过访问点,又要用并查集维护联通块,于是开俩并查集分别维护就好了 一开 ...

  8. Comet OJ - Contest #2 简要题解

    Comet OJ - Contest #2 简要题解 cometoj A 模拟,复杂度是对数级的. code B 易知\(p\in[l,r]\),且最终的利润关于\(p\)的表达式为\(\frac{( ...

  9. Comet OJ - Contest #2简要题解

    Comet OJ - Contest #2简要题解 前言: 我没有小裙子,我太菜了. A 因自过去而至的残响起舞 https://www.cometoj.com/contest/37/problem/ ...

随机推荐

  1. if you wanna the rainbow, you have to deal with the rain.

    bulk. n. 大量 reluctant. adj. 不情愿的 terrorist. n. 恐怖分子 recognition. n. 认出 tout.v. 兜售 conceal.v. 隐藏 dras ...

  2. Python_ONLINE_习题集_02 函数封装

    2.1 封装函数实现如下要求 例如:输入2,5 则求:2 + 22+222 + 2222+22222的和 参考答案: https://www.bilibili.com/read/cv4185619 d ...

  3. [DS+Algo] 005 三种简单排序及其代码实现

    目录 1. 冒泡排序 BubbleSort 1.1 算法描述 1.2 性能分析 1.3 Python 代码实现 2. 选择排序 SelectionSort 2.1 算法描述 2.2 选择排序的主要优点 ...

  4. [转帖]NetSuite 进入中国市场满一年,甲骨文公布首份成绩单

    NetSuite 进入中国市场满一年,甲骨文公布首份成绩单 https://baijiahao.baidu.com/s?id=1617073148682281883&wfr=spider&am ...

  5. 使用ntpdate 同步 linux的时间

    1. linux 查看时间和时区的命令 timedatectl 效果为: Local time: Sun -- :: CST Universal time: Sun -- :: UTC RTC tim ...

  6. 使用批处理命令注册运行mysql数据库,无需注册mysql服务,可以在任意电脑登录使用

    使用批处理命令初始化和开启mysql服务,移植数据库之后可以直接运行访问,对于学习数据库的人来说特别的方便哦. 我们可以从mysql官网下载官方社区版本的mysql: 这里使用之前下载的8.0.15来 ...

  7. PHP7中的数据类型(一)计数引用、写时复制,可垃圾回收

    列个简单的表格说明一下:

  8. MyBatis逆向工程无效

    在Taget目录下修改的东西无法逆向, 在源代码目录就可以

  9. 最大连续和 Easy

    Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. F ...

  10. 工作日记之查看Linux系统里面的启动频率2017_02_07

    链接:http://www.jb51.net/LINUXjishu/19905.html 查看Linux里面的batch: cat /etc/crontab (1)0 19 * * 6 root /d ...