QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#325258#7936. Eliminate TreeMilanTL 0ms3788kbC++231.6kb2024-02-11 06:44:192024-02-11 06:44:19

Judging History

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

  • [2024-02-11 06:44:19]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3788kb
  • [2024-02-11 06:44:19]
  • 提交

answer

#include <bits/stdc++.h>
#define MULTI int _; cin >> _; while(_--)
#define fi first
#define se second
#define pb(a) push_back(a)
#define rep(i, n) for(int i = 0; i < n; i++)
#define reps(i, n, m) for(int i = n; i <= m; i++)
#define repsv(i, n, m) for(int i = n; i >= m; i--)
#define vsz(a) (int)(a.size())
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()

using namespace std;

typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;

#ifdef LOCAL
#include "debugs.hpp"
#else
#define dbg(...) 0
#endif

vector<vi> to;

int dfs(int i, int p, bool m){
    map<int, int> dp[2];
    for(int j : to[i]){
        if(j == p) continue;
        dp[0][j] = dfs(j, i, 0);
        dp[1][j] = dfs(j, i, 1);
    }
    int res = 0;
    for(int j : to[i])
        if(j!=p)
            res += dp[0][j];
    if(m)
        return res;
    int ans = 0;
    for(int j : to[i])
        if(j != p)
            ans = max(ans, res-dp[0][j]+dp[1][j]+1);
    return ans;
}

void solve(){
    int n;
    cin >> n;
    to = vector<vi>(n);
    rep(i, n-1){
        int v, w;
        cin >> v >> w;
        v--;w--;
        to[v].pb(w);
        to[w].pb(v);
    }
    int m;
    rep(i, n)
        if(vsz(to[i])==1){
            m = dfs(i, -1, 0);
            break;
        }
    dbg(m);
    cout << 2*n-3*m << '\n';
}

int main(){
    ios_base::sync_with_stdio(false);
    // MULTI
    solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3736kb

input:

5
1 2
2 3
3 4
3 5

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

4
1 2
2 3
3 4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Time Limit Exceeded

input:

196666
32025 108673
181779 17115
162570 134230
93003 39673
89144 1269
185919 154408
34896 65369
35860 44720
55698 1390
45520 189805
147867 124511
135459 132555
87303 18031
176314 59695
33352 130640
87272 39716
35749 108807
143493 94486
126512 116598
40212 70895
132216 80855
22241 188737
150354 17346...

output:


result: