#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fir first
#define sec second
#define pdd pair<long double, long double>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ld long double
#define pmax pair<__int128, __int128>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]= {0, 1, 0, -1}, dy[4]= {1, 0, -1, 0};
int ddx[8]={1, 1, 0, -1, -1, -1, 0, 1}, ddy[8]={0, 1, 1, 1, 0, -1, -1, -1};
const int mxN=100025;
const int mxM=300005;
const int mxK=1000000000;
const ll MOD=1000'000'007;
const ll INF=1000000000000000005;
ll N, D;
vector <int> v[mxN];
ll outw[mxN];
ll downw[mxN], upw[mxN];
ll degL[mxN];
bool W[mxN];
ll numw;
void dfs_downw(int now, int pre)
{
for(int nxt : v[now]) if(nxt!=pre)
{
dfs_downw(nxt, now);
}
downw[now]=(outw[now]==0 ? 1 : 0);
if(now!=1) outw[pre]+=downw[now];
}
void dfs_upw(int now, int pre)
{
if(now!=1)
{
upw[now]=(outw[pre]-downw[now]==0 ? 1 : 0);
outw[now]+=upw[now];
}
for(int nxt : v[now]) if(nxt!=pre)
{
dfs_upw(nxt, now);
}
}
ll dfs1(int now, int pre)
{
ll sum=1;
for(int nxt : v[now]) if(W[nxt] && nxt!=pre)
{
sum+=dfs1(nxt, now);
}
return sum;
}
ll dfs2(int now, int pre)
{
ll sum=!W[now];
for(int nxt : v[now]) if(nxt!=pre && W[nxt]+W[now]==1)
{
sum+=dfs2(nxt, now);
}
return sum;
}
int main()
{
gibon
cin >> N >> D;
for(int i=1;i<N;i++)
{
int a, b; cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs_downw(1, -1);
dfs_upw(1, -1);
for(int i=1;i<=N;i++) if(outw[i]>=1) W[i]=true;
for(int i=1;i<=N;i++) numw+=W[i];
for(int i=1;i<=N;i++) for(int ele : v[i]) degL[i]+=1-W[ele];
ll ans=0;
if(W[1])
{
ans=N*N;
if(degL[1]==0)
{
assert(0==1);
ans-=dfs1(1, -1)/2*(N-numw);
}
if(degL[1]==1)
{
ans-=dfs2(1, -1)*(N-numw);
}
}
else
{
ans+=dfs2(1, -1)*(N-numw);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Runtime error |
5 ms |
5204 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Runtime error |
5 ms |
5204 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Runtime error |
5 ms |
5204 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Runtime error |
5 ms |
5204 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Runtime error |
5 ms |
5204 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |