QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#124075 | #2596. Even Forest | UNos_maricones# | WA | 4ms | 42572kb | C++20 | 1.6kb | 2023-07-14 06:41:28 | 2023-07-14 06:41:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std ;
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
typedef pair<ll,ll> ii;
typedef long double lf;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll SQ = 320;
const ll N = 1e6+5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
long double eps = 1e-9;
int dp[N][2][2];
vector <int> g[N];
int go (int u, int par, int flag, int p) {
if(dp[u][par][flag]!=-1)return dp[u][par][flag];
if (par == 0) {
dp[u][par][flag]=0;
for (auto &v : g[u]) {
if (v == p) continue;
dp[u][par][flag] += min(go(v,1 - par,0, u), 1 + min(go(v, 1-par, 1,u),go(v,par,1,u)));
}
}
else {
vector <int> bst(3, mod);
bst[0] = 0;
for (auto &v : g[u]) {
if (v == p) continue;
vector <int> nwbst(3, mod);
for (int i = 0; i < 3; ++i) {
nwbst[i] = bst[i] + 1 + min(go(v,par,1,u), go(v, 1-par,1,u));
if (i)
nwbst[i] = min(bst[i], bst[i - 1] + go(v, 1 - par,0, u));
}
swap(bst, nwbst);
}
dp[u][par][flag] = bst[1 + flag];
}
return dp[u][par][flag];
}
int main(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n; cin >> n;
if (n <= 2) {
cout << n-1 << '\n';
return 0;
}
for (int i = 0; i + 1 < n ;++i ) {
int u, v; cin >> u >> v;
g[u-1].pb(v-1);
g[v-1].pb(u-1);
}
memset(dp, -1, sizeof dp);
cout << min(go(0, 0, 1,-1), go(0, 1,1, -1)) << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 42572kb
input:
4 1 2 2 3 3 4
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 3ms
memory: 42524kb
input:
4 1 2 1 3 1 4
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 42512kb
input:
6 1 2 2 3 4 2 4 5 6 4
output:
0
result:
wrong answer 1st numbers differ - expected: '1', found: '0'