QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#334585#5651. Parmigiana With SeafoodMilanWA 127ms30132kbC++232.3kb2024-02-22 06:14:422024-02-22 06:14:42

Judging History

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

  • [2024-02-22 06:14:42]
  • 评测
  • 测评结果:WA
  • 用时:127ms
  • 内存:30132kb
  • [2024-02-22 06:14:42]
  • 提交

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> g;
vector<unordered_map<int, int>> dp; // largeur de l'arbe en enlevant tel parent
int n;

int calc(int i, int p){ // noeud i en enlevant le parent p
    if(dp[i].count(p))
        return dp[i][p];
    int res = 1; // self
    for(int j : g[i])
        if(j!=p)
            res += calc(j, i);
    return dp[i][p] = res;
}

void dfs(int i, vector<bool> & vu, int k){
    if(i >= k) return;
    vu[i] = true;
    for(int j : g[i])
        if(!vu[j])
            dfs(j, vu, k);
}

bool test(int k){
    reps(i, k, n-1){
        if(g[i].size()==1)
            return true;
        for(int j : g[i])
            if(j>=k)
                return true;
    }
    vector<bool> vu(n);
    rep(i, n)
        if(g[i].size()==1)
            dfs(i, vu, k);
    int s = 0;
    reps(i, k, n-1){
        bool ok = false;
        for(int j : g[i])
            if(vu[j])
                ok = true;
        if(ok)
            for(int j : g[i])
                if(!vu[j])
                    s += calc(i, j)-1;
    }
    // dbg(k, s);
    if(s%2==0) return false;
    else return true;
}

void solve(){
    cin >> n;
    g = vector<vi>(n);
    dp = vector<unordered_map<int,int>>(n);
    rep(i, n-1){
        int a, b;
        cin >> a >> b;
        g[a-1].pb(b-1);
        g[b-1].pb(a-1);
    }
    // rep(i, n){bool t = test(i); cerr << i << " : " << t << '\n';}
    int l = 0, r = n-1;
    while(r-l>1){
        int m = (l+r)/2;
        if(test(m))
            l = m;
        else
            r = m-1;
    }
    if(test(r)) cout << r+1 << '\n';
    else cout << l+1 << '\n';
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3608kb

input:

4
1 2
1 3
1 4

output:

4

result:

ok single line: '4'

Test #2:

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

input:

5
1 5
5 3
3 4
4 2

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 26ms
memory: 14512kb

input:

99999
81856 39633
81856 94012
99999 43062
99946 220
81856 46131
99933 36505
99939 35662
99952 70971
99999 3275
99938 58416
99976 66658
99991 87922
81856 80992
99933 6392
99951 41047
99970 54115
81856 38150
99934 73554
81856 64578
81856 18576
99951 67996
99938 84479
81856 39617
99999 18664
99946 2505...

output:

99925

result:

ok single line: '99925'

Test #4:

score: -100
Wrong Answer
time: 127ms
memory: 30132kb

input:

99997
90325 59106
22545 8765
88871 37709
14739 95233
8778 29659
48110 57549
91258 76066
15724 65144
48244 87291
12076 94378
41946 96707
93645 12812
53817 34343
72097 94062
81212 263
78713 78150
6754 94906
20957 97539
59293 5018
77961 78090
57262 95225
79349 47902
99024 7869
10613 13728
61757 41090
4...

output:

86537

result:

wrong answer 1st lines differ - expected: '85398', found: '86537'