2018 Toutiao Hubei Provincial Competition [D]

[Title link] https://www.nowcoder.com/acm/contest/104/C

 

I don't know why this question didn't come up. The teammates were right on the spot. . 233333 It seems that there is a problem with the code, and it is very annoying when it comes down.

 

The meaning of the question is probably to count the difference and the number of subgraphs.

dp formula dp[i] = ∏ (j is the son of i) (dp[j]+1)

 

The answer is ∑dp[i]

 

There are a few things to say about the code below.

First of all, be sure to use long long ah QAQ. Then you must always remember the mod, QAQ.

In dfs, the value of dp[i] is sought. The logic is to see if there is access to the current number, if not, then calculate the product down, and continue to dfs the point, looking down.

 

 

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define ll long long 
const ll Max = 2*1e5+10;
const ll mod = 1e7+7;


vector<ll> a[Max];
ll vis[Max];
ll dp[Max];


void dfs(ll num){
    force[num] = 1 ;
    ll ans = 1;
    ll len = a[num].size();
    for(int i = 0 ;i < len ;i++){
        if(vis[a[num][i]]){
            continue;
        }
        
        dfs(a[num][i]);     
        ans *= (dp[a[num][i]]+1) ;
        ans % = mod;
    }    
    dp[num] = ans;
}

int main(){
    ll n;
    scanf("%lld",&n);
    ll x,y;
    for(int i = 0; i < n-1 ; i++){
        scanf("%lld%lld",&x,&y);
        a[x].push_back(y);
        a[y].push_back(x);        
    }    
    for(int i = 0 ;i < n ;i++){
        dp[i] = 1;
    }
    
    dfs(1);
    
    ll cnt = 0;
    for(int i = 1; i <= n ;i++){
        cnt += dp[i];
        cnt %= mod;
    }
    
    printf("%d",cnt);    
    return 0;
}
View Code

 

Guess you like

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