QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#124075#2596. Even ForestUNos_maricones#WA 4ms42572kbC++201.6kb2023-07-14 06:41:282023-07-14 06:41:30

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-14 06:41:30]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:42572kb
  • [2023-07-14 06:41:28]
  • 提交

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'