#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<int> adj[100100];
bool _win[100100], _win_pa[100100], _win_root[100100], ok[100100];
void dfs1(int s, int pa = -1){
int wcnt = 0, lcnt = 0;
for (auto &v:adj[s]) if (v!=pa){
dfs1(v, s);
if (_win[v]) wcnt++;
else lcnt++;
}
if (!lcnt) _win[s] = 0;
else _win[s] = 1;
if (lcnt<=1) ok[s] = 1;
}
void dfs2(int s, int pa = -1){
int wcnt = 0, lcnt = 0;
for (auto &v:adj[s]) if (v!=pa){
if (_win[v]) wcnt++;
else lcnt++;
}
if (pa!=-1){
if (_win_pa[s]) wcnt++;
else lcnt++;
}
for (auto &v:adj[s]) if (v!=pa){
if (_win[v]) wcnt--;
else lcnt--;
if (!lcnt) _win_pa[v] = 0;
else _win_pa[v] = 1;
if (_win[v]) wcnt++;
else lcnt++;
}
if (!lcnt) _win_root[s] = 0;
else _win_root[s] = 1;
for (auto &v:adj[s]) if (v!=pa) dfs2(v, s);
}
int lscnt = 0;
void dfs3(int s, int pa = -1){
if (!ok[s]) return;
if (!_win[s]) lscnt++;
for (auto &v:adj[s]) if (v!=pa){
if (!_win[s]) dfs3(v, s);
else if (!_win[v]) dfs3(v, s);
}
}
int main(){
int n;
ll d;
scanf("%d %lld", &n, &d);
for (int i=1;i<=n-1;i++){
int x, y;
scanf("%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs1(1);
dfs2(1);
dfs3(1);
/*
for (int i=1;i<=n;i++) printf("%d ", _win[i]); printf("\n");
for (int i=1;i<=n;i++) printf("%d ", _win_pa[i]); printf("\n");
for (int i=1;i<=n;i++) printf("%d ", _win_root[i]); printf("\n");
for (int i=1;i<=n;i++) printf("%d ", ok[i]); printf("\n");
*/
int lcnt = 0;
for (int i=1;i<=n;i++) if (!_win_root[i]) lcnt++;
ll ans = 0;
if (_win[1]) ans = (ll)n*n - (ll)lscnt * lcnt;
else ans = (ll)lscnt * lcnt;
printf("%lld\n", ans);
return 0;
}
Compilation message
startrek.cpp: In function 'int main()':
startrek.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d %lld", &n, &d);
| ~~~~~^~~~~~~~~~~~~~~~~~~
startrek.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2692 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2660 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2660 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Incorrect |
62 ms |
10860 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2660 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2660 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Incorrect |
62 ms |
10860 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2692 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |