이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_N = 1000;
const int mod = 1e9+7;
vector<int> adj[MAX_N];
int dpth[MAX_N];
bool can_win[MAX_N][MAX_N];
bool can_avoid[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 u,int p, int a){
for(auto v: adj[u]){
if( v!=a){
find_win(f, v, (p+1)%2, u);
}
}
if(p == 0){
can_win[f][u] = false;
for(auto v: adj[u]){
if( v!=a){
can_win[f][u] |= can_win[f][v];
}
}
}
else{
can_win[f][u] = true;
for(auto v: adj[u]){
if(v!=a){
can_win[f][u] &= can_win[f][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;
}
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, i, 0, -1);
}
int res= 0;
int go = 0;
for(int i = 0; i<n; i++){
//cout<<i<<" "<<can_win[i][i]<<endl;
if(can_win[i][i]){
go ++;
}
}
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){
//cout<<"we lead "<<i<<" "<<n-go<<endl;
res= (res + n-go)%mod;
}
else{
if(can_win[0][i]){
//cout<<"no choice but "<<i<<" "<<go<<endl;
res =(res +go)%mod;
}
}
}
}
cout<<res<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |