#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
vector<int> edges[N];
int dp[N], sub[N], up[N];
int is[N][2], cnt[N];
ar<int, 2> C{};
void dfs(int u, int p = -1){
cnt[u] = 1;
for(auto x : edges[u]){
if(x == p) continue;
dfs(x, u);
if(dp[x] == 0) dp[u] = 1;
cnt[u] += cnt[x];
}
sub[u] = dp[u];
}
void re(int u, int p = -1){
if(up[u] == 0) dp[u] = 1;
ar<int, 2> c {};
c[up[u]]++;
for(auto x : edges[u]){
if(x == p) continue;
c[dp[x]]++;
}
for(auto x : edges[u]){
if(x == p) continue;
c[dp[x]]--;
if(c[0]) up[x] = 1;
re(x, u);
}
}
int n;
void dfs2(int u, int p = -1){
int f = 0, v = -1;
for(auto x : edges[u]){
if(x == p) continue;
dfs2(x, u);
if(!sub[x]){
f++, v = x;
}
}
if(f > 1){
is[u][1] = cnt[u] * 1ll * n % mod;
return;
} if(f == 1) {
is[u][1] = (cnt[u] - cnt[v]) * 1ll * n % mod;
is[u][1] += is[v][0], is[u][0] += is[v][1];
} else {
is[u][1] = C[0], is[u][0] = C[1];
for(auto x : edges[u]){
if(x == p) continue;
is[u][0] += is[x][1], is[u][1] += is[x][0];
}
}
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int d; cin>>n>>d;
for(int i=1;i<n;i++){
int a, b; cin>>a>>b;
edges[a].push_back(b);
edges[b].push_back(a);
}
dfs(1);
up[1] = 1;
re(1);
for(int i=1;i<=n;i++){
C[dp[i]]++;
}
dfs2(1);
cout<<is[1][1]<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |