牛客小白月赛19 E 「火」烈火燎原 (思维,树)

牛客小白月赛19 E 「火」烈火燎原 (思维,树)

链接:https://ac.nowcoder.com/acm/contest/2272/E来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

帕秋莉掌握了一种火属性魔法

这种强大的魔法可以让一棵树瞬间化为灰烬!

现在有一棵树G(无环),定义它的“小树”是X(G),小树X(G)的点集是G的边集

在“小树”中,两点之间有边,当且仅当这两个点对应的边在原图上有公共点

完整版的魔法可以将一棵树的小树的小树的小树的小树的小树的小树……全部烧掉

但帕秋莉不想消耗那么多的魔法来干这种无聊的事,毕竟她只想用这个魔法来修建树枝罢了

于是她想问你,对于一棵她想修剪的树G,它的X(X(G))有多少个点?

输入描述:

第一行一个正整数n表示G的点数;接下来n 行,每行两个数表示G的一条边

输出描述:

输出一行,一个整数表示答案

示例1

输入

复制

5
1 2
2 3
2 5
3 4

输出

复制

4

说明

下图是想修剪的树G图二是G的小树X(G)图三是X(X(G))由图知,它有四个点

备注:

对于30%的数据,n≤100对于100%的数据,n≤100,000

思路:

问题是让求 X(X(G))有多少个点

我们可以知道 X(X(G))点的个数等于X(G) 边的个数。

那么我们来思考一下如何求X(G) 边的个数?

我们来看在G图到X(G),如何建边的。

同一个节点有多个边时,这些边在X(G)中转成的点要相互连边。

那么我们之需要用一个数组du[i]维护第i个节点的度数,

那么第i个节点对答案的贡献就是\(C(du[i],2)\)

直接统计答案即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll du[maxn];
int n;
int x, y;
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    cin >> n;
    repd(i, 2, n)
    {
        cin >> x >> y;
        du[x]++;
        du[y]++;
    }
    ll ans = 0ll;
    repd(i, 1, n) {
        if (du[i] >= 2)
        {
            ans += (du[i] * (du[i] - 1ll)) / 2ll;
        }
    }
    cout << ans << endl;
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



猜你喜欢

转载自www.cnblogs.com/qieqiemin/p/11920781.html