#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_N = 1001;
const int mod = 1e9+7;
vector<int> adj[MAX_N];
int dpth[MAX_N];
bool can_win[MAX_N][2][MAX_N];
bool can_avoid[MAX_N][MAX_N];
bool can_force[MAX_N][MAX_N];
void find_d(int u, int a, int d){
dpth[u] =d;
for(auto v: adj[u]){
if(v!=a){
find_d(v, u, d+1);
}
}
}
void find_win(int f,int pf, int u,int p, int a){
for(auto v: adj[u]){
if( v!=a){
find_win(f, pf, v, (p+1)%2, u);
}
}
if(p == 0){
can_win[f][pf][u] = false;
for(auto v: adj[u]){
if( v!=a){
can_win[f][pf][u] |= can_win[f][pf][v];
}
}
}
else{
can_win[f][pf][u] = true;
for(auto v: adj[u]){
if(v!=a){
can_win[f][pf][u] &= can_win[f][pf][v];
}
}
}
}
void avoid(int f, int p, int u, int a){
int nbc= 0;
for(auto v: adj[u]){
if( v!=a){
avoid(f, (p+1)%2,v, u);
nbc++;
}
}
if(p == 0){
can_avoid[f][u] = false;
for(auto v: adj[u]){
if(v!=a){
can_avoid[f][u] |= can_avoid[f][v];
}
}
}
else{
can_avoid[f][u] = (u!=f);
for(auto v: adj[u]){
if(v!=a){
can_avoid[f][u] &= can_avoid[f][v];
}
}
}
//cout<<"can avoid "<<f<<" "<<u<<" "<<can_avoid[f][u]<<endl;
}
void force(int f, int p, int u, int a){
int nbc= 0;
for(auto v: adj[u]){
if( v!=a){
force(f, (p+1)%2,v, u);
nbc++;
}
}
if(p == 1){
can_force[f][u] = true;
for(auto v: adj[u]){
if(v!=a){
can_force[f][u] &= can_force[f][v];
}
}
}
else{
can_force[f][u] = (u==f);
for(auto v: adj[u]){
if(v!=a){
can_force[f][u] |= can_force[f][v];
}
}
}
}
signed main(){
int n, d;
cin>>n>>d;
for(int i = 0; i<n-1;i++){
int a, b;
cin>>a>>b;
adj[a-1].push_back(b-1);
adj[b-1].push_back(a-1);
}
find_d(0, -1, 0);
for(int i = 0; i<n; i++){
avoid(i, 0, 0, -1);
}
for(int i = 0; i<n; i++){
find_win(i, 0, i, 0, -1);
find_win(i, 1, i, 0, -1);
}
for(int i = 0; i<n; i++){
force(i, 0, 0, -1);
}
int res= 0;
vector<int> go(2, 0);
for(int i = 0; i<n; i++){
//cout<<i<<" "<<can_win[i][i]<<endl;
if(can_win[i][0][i]){
go[0]++;
}
if(can_win[i][1][i]){
go[1]++;
}
}
for(int i = 0; i<n; i++){
if(can_avoid[i][0]){
//cout<<"full "<<i<<endl;
res = (res+n)%mod;
}
else{
if((dpth[i]%2 == 0) && can_force[i][0]){
//cout<<"we lead "<<i<<" "<<n-go[1]<<endl;
res= (res + n-go[1])%mod;
}
else{
if(can_win[0][0][i]){
//cout<<"no choice but "<<i<<" "<<go[0]<<endl;
res =(res +go[0])%mod;
}
}
}
}
cout<<res<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
34 ms |
4352 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
34 ms |
4352 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |