#include <bits/stdc++.h>
using namespace std;
#define ii pair<int, int>
#define fi first
#define se second
// #define int long long
#define ll long long
#define ld double
#define mp make_pair
#define lg2 30
#define iii pair<int, ii>
#define iiii pair<ii, ii>
#define base 29
#define eps 1e-8
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
int dx[] = {0LL, 0LL, 1, -1, 1, 1, -1, -1};
int dy[] = {1, -1, 0LL, 0LL, 1, -1, 1, -1};
const int maxn = 4e5 + 5;
const int mod = 1e9 + 7;
int dp[maxn][3], tt[maxn];
vector<int> a[maxn];
void dfs(int u, int cha)
{
if (!tt[u])
dp[u][0] = dp[u][1] = 0;
else
dp[u][tt[u]] = 0;
for (int v : a[u])
{
if (v == cha)
continue;
dfs(v, u);
dp[u][1] += min({dp[v][1], dp[v][2] + 1});
dp[u][2] += min({dp[v][2], dp[v][1] + 1});
}
}
int cat(int v)
{
tt[v] = 1;
// cout << v << " ";
memset(dp, 0x3f, sizeof(dp));
dfs(1, -1);
return min({dp[1][2], dp[1][1]});
}
int dog(int v)
{
tt[v] = 2;
memset(dp, 0x3f, sizeof(dp));
dfs(1, -1);
return min({dp[1][2], dp[1][1]});
}
int neighbor(int v)
{
tt[v] = 0;
memset(dp, 0x3f, sizeof(dp));
dfs(1, -1);
return min({dp[1][2], dp[1][1]});
}
void initialize(int N, vector<int> A, vector<int> B)
{
int n = N;
for (int i = 0; i < n - 1; i++)
{
a[A[i]].push_back(B[i]);
a[B[i]].push_back(A[i]);
}
}
/*signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task "task"
if (fopen(task ".inp", "r"))
{
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
initialize(3, {1, 2}, {2, 3});
cout << cat(1) << " ";
cout << cat(3) << " ";
cout << dog(2) << " ";
cout << neighbor(2);
cerr << endl
<< "TIME : " << clock() * 0.001 << "s" << endl;
}
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |