#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5, MOD=1e9+7;
vector<int> adj[MAXN+1];
bool win0[MAXN+1], win1[MAXN+1];
int n;
long long power4(long long n)
{
long long result=1, current=4;
for (long long i=0; i<64; i++)
{
if (n&((long long)1<<i))
result=result*current%MOD;
current=current*current%MOD;
}
return result;
}
bool dfs0(int x, int t)
{
bool possible=false;
for (auto y : adj[x])
{
if (y==t)
continue;
if (!dfs0(y, x))
possible=true;
}
return win0[x]=possible;
}
bool dfs1(int x, int t)
{
for (auto y : adj[x])
if (y!=t && !dfs1(y, x))
return true;
return false;
}
int dfscritical(int x, int t)
{
int critical=0;
if (!win0[x])
{
critical=critical+1;
for (auto y : adj[x])
{
if (y==t)
continue;
critical=critical+dfscritical(y, x);
}
}
else
{
int losenodes=0;
for (auto y : adj[x])
{
if (y==t)
continue;
if (!win0[y])
{
critical=critical+dfscritical(y, x);
losenodes=losenodes+1;
}
}
if (losenodes>1)
critical=0;
}
return critical;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int x, y, i;
long long d, total;
cin >> n >> d;
for (i=0; i<n-1; i++)
{
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
if (n==2)
{
cout << power4(d);
return 0;
}
dfs0(1, -1);
for (i=1; i<=n; i++)
win1[i]=dfs1(i, -1);
int win=0;
for (i=1; i<=n; i++)
if (win1[i])
win=win+1;
int lose=n-win;
int critical=dfscritical(1, -1);
/*for (i=1; i<=n; i++)
cout << win0[i] << " ";
cout << '\n';
for (i=1; i<=n; i++)
cout << win1[i] << " ";
cout << '\n';*/
//cout << win << " " << lose << " " << critical << '\n';
if (win0[1])
total=n*win+(n-critical)*lose;
else
total=critical*lose;
cout << total;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2736 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
4 ms |
2652 KB |
Output is correct |
8 |
Correct |
8 ms |
2908 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
3 ms |
2648 KB |
Output is correct |
11 |
Correct |
5 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
4 ms |
2652 KB |
Output is correct |
8 |
Correct |
8 ms |
2908 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
3 ms |
2648 KB |
Output is correct |
11 |
Correct |
5 ms |
2648 KB |
Output is correct |
12 |
Execution timed out |
1004 ms |
9052 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
4 ms |
2652 KB |
Output is correct |
8 |
Correct |
8 ms |
2908 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
3 ms |
2648 KB |
Output is correct |
11 |
Correct |
5 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
4 ms |
2652 KB |
Output is correct |
8 |
Correct |
8 ms |
2908 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
3 ms |
2648 KB |
Output is correct |
11 |
Correct |
5 ms |
2648 KB |
Output is correct |
12 |
Execution timed out |
1004 ms |
9052 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |