//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
void solve(){
int n, d; cin >> n >> d;
//n <= 100, d = 1;
vector<vector<int>>g(n+1);
for (int i = 1; i<n; i++){
int a, b; cin >> a >> b;
g[a].emplace_back(b);
g[b].emplace_back(a);
}
vector dp(n+1, vector<int>(2));
//jak jestesmy w lisciu w tak ukorzenionym drzewie to przegrywamy
vector<int>depth(n+1), par(n+1);
function<void(int, int)>dfs = [&](int v, int pa){
par[v] = pa;
dp[v][0] = 0;
dp[v][1] = 1; //gracz 1 jest teraz w v i musi zrobic ruch, ale nie ma gdzie :p
for (auto x: g[v]){
if (x == pa) continue;
depth[x] = depth[v]+1;
dfs(x, v);
dp[v][0] |= dp[x][1]; //musi byc jakas wygrywajaca
dp[v][1] &= (dp[x][0] == 1); //musi prowadzic do samych wygrywajacych
}
debug(v, dp[v]);
};
vector win(n+1, vector<int>(2));
for (int i = 1; i<=n; i++){
dp.assign(n+1, vector<int>(2, 0));
depth.assign(n+1, 0);
dfs(i, i);
win[i] = dp[i];
// return;
}
depth[1] = 0;
dfs(1, 1);
int ans = 0;
for (int i = 1; i<=n; i++){
for (int j = 1; j<=n; j++){
//krawedz i--j
if (depth[i]%2 == 0){
if (win[1][0] || win[j][1]) {
ans++;
}
} else {
//gracz 2 moze podejmowac decyzje
//nie dojdziemy w jakis sposob do wierzcholka i(?)
//zeby dojsc do wierzcholka i, na calej sciezce od i do 1 gracz 2 musi wykonac decyzje, ze warto isc w te konkretne poddrzewo
int v = i;
int prev = -1;
bool ok = 1;
while (v != 1){
for (auto x: g[v]){
if (x == prev || x == par[v]) continue;
debug(v, x);
if (!dp[x][0]) {
//zmieniam zdanie, ide w to poddrzewo
ok = 0;
break;
}
}
v = par[v];
prev = v;
v = par[v];
}
if (ok && win[j][0]){
debug(i, j);
ans++;
}
}
}
}
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
29 ms |
652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
29 ms |
652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |