AtCoder Beginner Contest 173F Intervals on Tree(贡献)

题意:给出一颗树,F(L, R) 是区间L-R的所有的节点形成的森林的联通分量个数,求 ∑F(L,R)。

题解:森林联通分量=总节点数-总边数目,对于所有的区间,总边的数目,可以考虑每一条边对答案的贡献,即在多少区间内出现过。

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define fre freopen("C:\\in.txt", "r", stdin)
#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 lowbit(a) ((a)&-(a))
#define inf 0x3f3f3f3f
#define endl "\n"
using namespace std;
typedef long long ll;
template <class T>
void read(T &x)
{
    char c; bool op=0;
    while(c=getchar(), c<'0'||c>'9') if(c=='-') op=1;
    x=c-'0';
    while(c=getchar(), c>='0'&&c<='9') x=x*10+c-'0';
    if(op) x=-x;
}

const int maxn=2e5+5;
int n, u[maxn], v[maxn];

int main()
{
    read(n);
    _rep(i, 1, n-1) read(u[i]), read(v[i]);
    _rep(i, 1, n) if(u[i]>v[i]) swap(u[i], v[i]);
    ll ans=0;
    _rep(i, 1, n) ans+=1ll*i*(n-i+1);
    _rep(i, 1, n) ans-=1ll*u[i]*(n-v[i]+1);
    printf("%lld\n", ans);
    return 0;
}

 

posted @ 2020-08-08 10:43  N_Yokel  阅读(190)  评论(0编辑  收藏  举报