D. Who killed Cock Robin Hubei University Programming Contest

Link: https://www.nowcoder.com/acm/contest/104/C
Source: Niuke.com

The Sparrow is for trial, at next bird assizes,we should select a connected subgraph from the whole tree of sparrows as trial objects.

Because the relationship between sparrows is too complex, so we want to leave this problem to you. And your task is to calculate how many different ways can we select a connected subgraph from the whole tree.

The meaning of the question: give you a tree, output the number of all its subtrees (the number of connected blocks).

Solution: Touch the sample with your hand, draw a ternary tree, and find a formula: num[i]=(num[v1]+1)*(num[v2]+1)*````*(num[ v0]+1) num[i] is the number of words in the tree rooted at the i-th node. v1````v0 is its son.

    Simply deduce this formula: for each son v of n, you have num[v] ways to select it. This is a step-by-step process, so the multiplication principle is used. But note that there is a situation where the son is not selected, so +1.

    The specific implementation is to randomly find a point as the root dfs, and then add up all num

 

Pit: I didn't pass it at first, but I still didn't pass it when I changed it to ll. After the game, I found that the mod was not changed to ll orz

#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<algorithm>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n)  for(int i =(t);i<=(n);++i)
#define per(i,n,t)  for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
    ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    long _begin_time = clock();
#endif
    smain();
#ifndef ONLINE_JUDGE
    long _end_time = clock();
    printf("time = %ld ms.", _end_time - _begin_time);
#endif
    return 0;
}
const int maxn = 2e5 + 100;
const ll mod = 1e7 + 7;
const ll INF = (100000)*(200000ll) + 1000;
 
int n;
vector<int> E[maxn];
int num[maxn];
void dfs(int n,int fa) {
    if (num[n] != 1)return;
    int sz = E[n].size();
    if (sz == 1 && E[n][0] == fa) {
        num[n] = 1 ;
    }
    rep(i, 0, sz - 1) {
        int v = E[n][i];
        if (v ==fa)continue;
        dfs(v,n);
        num[n] = num[n]*(num[v] + 1 )% mod;
    }
}
void Run() {
    dfs( 1 , 0 );
    int ans= 0 ;
    rep(i,1,n) {
        ans = (ans + num[i]) % mod;
    }
    cout << ans << endl;
 
     
 
 
}
 
void smain() {
     
    cin >> n;
    rep(i,1,n-1) {
        int a, b;
        cin >> a >> b;
        E[a].pb(b);
        E[b].pb(a);
         
    }
    rep(i, 1, n)num[i] = 1;
    Run();
     
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324695985&siteId=291194637