#include<bits/stdc++.h>
using namespace std;
int n, i, j, k, a, b, d, A[2][2], mod=1e9+7;
vector<int>adj[100010], w[100010];
long long ans;
struct st
{
int a, b;
};
st f(st a, st b)
{
a.a=max(a.a, b.b);
a.b=min(a.b, b.a);
return a;
}
st dp[100010], dp2[100010][2][2], dp3[100010];
vector<st>pre[100010], suf[100010];
void dfs(int v, int par)
{
dp[v].a=0;dp[v].b=1;
for(auto p:adj[v])
{
if(p==par)continue;
w[v].push_back(p);
dfs(p, v);
dp[v]=f(dp[v], dp[p]);
}
}
void dfs2(int v, int par)
{
int cnt=0;
for(auto p:w[v])
{
for(int i=0;i<2;i++)for(int j=0;j<2;j++)
{
st a={0, 1};
if(cnt>0)a=f(a, pre[v][cnt-1]);
if(cnt+1<w[v].size())a=f(a, suf[v][cnt+1]);
a=f(a, {i, j});
dp2[p][i][j]=dp2[v][a.a][a.b];
}
cnt++;
dfs2(p, v);
}
}
void dfs3(int v, int par)
{
int cnt=0;
for(auto p:w[v])
{
st a={0, 1};
if(v!=1)a=f(a, dp3[v]);
if(cnt>0)a=f(a, pre[v][cnt-1]);
if(cnt+1<w[v].size())a=f(a, suf[v][cnt+1]);
dp3[p]=a;
cnt++;
dfs3(p, v);
}
}
main()
{
scanf("%d %d", &n, &d);
for(i=0;++i<n;)
{
scanf("%d %d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 0);
for(i=0;i++<n;)if(w[i].size())
{
st a={0, 1};
pre[i].resize(w[i].size());
suf[i].resize(w[i].size());
for(j=0;j<w[i].size();j++)
{
a=f(a, dp[w[i][j]]);
pre[i][j]=a;
}
a={0, 1};
for(j=(int)w[i].size()-1;j>=0;j--)
{
a=f(a, dp[w[i][j]]);
suf[i][j]=a;
}
}
dp2[1][0][0]={0, 0};
dp2[1][0][1]={0, 1};
dp2[1][1][0]={1, 0};
dp2[1][1][1]={1, 1};
dfs2(1, 0);
dfs3(1, 0);
for(i=0;i++<n;)
{
st a={0, 1};
for(auto p:w[i])
{
a=f(a, dp[p]);
}
if(i!=1)a=f(a, dp3[i]);
dp3[i]=a;
}
for(i=0;i++<n;)A[dp3[i].a][dp3[i].b]++;
for(i=0;i++<n;)
{
for(int j=0;j<2;j++)for(int k=0;k<2;k++)
{
st a=dp[i];
a=f(a, {j, k});
ans+=dp2[i][a.a][a.b].a*A[j][k];
}
}
cout<<ans%mod;
}
Compilation message
startrek.cpp: In function 'void dfs2(int, int)':
startrek.cpp:38:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if(cnt+1<w[v].size())a=f(a, suf[v][cnt+1]);
| ~~~~~^~~~~~~~~~~~
startrek.cpp: In function 'void dfs3(int, int)':
startrek.cpp:54:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | if(cnt+1<w[v].size())a=f(a, suf[v][cnt+1]);
| ~~~~~^~~~~~~~~~~~
startrek.cpp: At global scope:
startrek.cpp:60:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
60 | main()
| ^~~~
startrek.cpp: In function 'int main()':
startrek.cpp:75:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(j=0;j<w[i].size();j++)
| ~^~~~~~~~~~~~
startrek.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d %d", &n, &d);
| ~~~~~^~~~~~~~~~~~~~~~~
startrek.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Incorrect |
6 ms |
9804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
9676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Incorrect |
6 ms |
9688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Incorrect |
6 ms |
9688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Incorrect |
6 ms |
9688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Incorrect |
6 ms |
9688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Incorrect |
6 ms |
9688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Incorrect |
6 ms |
9804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |