#include <bits/stdc++.h>
#define C 1
#define P 0
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;
const ll NMAX = 100001;
const ll VMAX = 101;
const ll INF = (1LL << 60);
const ll MOD = 1000000007;
const ll BLOCK = 447;
const ll base = 117;
const ll nr_of_bits = 18;
const ll inv2 = 500000004;
vector <int> v[NMAX];
int col[NMAX];
int children[NMAX][2];
int maximal[NMAX];
int rosii[NMAX];
int adv[NMAX];
void DFS(int node, int p){
col[node] = P;
for(auto x : v[node]){
if(x == p) continue;
DFS(x, node);
children[node][col[x]]++;
}
if(children[node][C] == (int)v[node].size() - (p != 0)){
col[node] = P;
}else{
col[node] = C;
}
if(col[node] == P)
rosii[node] = 1;
for(auto x : v[node]){
if(x == p) continue;
if(col[node] != col[x]){
rosii[node] += rosii[x];
}
}
}
int perdanti = 0;
void Rerooting(int node, int p){
adv[node] = col[node];
if(adv[node] == P)
perdanti++;
for(auto x : v[node]){
if(x == p) continue;
int cx = col[x], cnode = col[node];
children[node][col[x]]--;
if(children[node][C] == v[node].size() - 1){
col[node] = P;
}else{
col[node] = C;
}
children[x][col[node]]++;
if(children[x][C] == v[x].size()){
col[x] = P;
}else{
col[x] = C;
}
Rerooting(x, node);
children[x][col[node]]--;
col[x] = cx;
col[node] = cnode;
children[node][col[x]]++;
}
}
int main() {
//ifstream cin(".in");
//ofstream cout(".out");
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, d, i;
cin >> n >> d;
for(i = 1; i < n; i++){
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
DFS(1, 0);
Rerooting(1, 0);
ll schimba = 1LL * perdanti * rosii[1];
schimba %= MOD;
if(col[1] == P){
cout << schimba;
}else{
cout << (1LL * n * n - schimba + MOD) % MOD;
}
return 0;
}
Compilation message
startrek.cpp: In function 'void Rerooting(int, int)':
startrek.cpp:60:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if(children[node][C] == v[node].size() - 1){
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
startrek.cpp:66:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | if(children[x][C] == v[x].size()){
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
- |