This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
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... |