QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#288527#5651. Parmigiana With SeafoodSorting#WA 1ms5924kbC++201.2kb2023-12-22 20:40:022023-12-22 20:40:03

Judging History

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

  • [2023-12-22 20:40:03]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5924kb
  • [2023-12-22 20:40:02]
  • 提交

answer

#include <iostream>
#include <vector>
#include <functional>

using namespace std;

const int N = 1e5 + 3;

vector<int> adj[N];
int n;

bool check(int mid){
    int d = -1;
    bool ok = true;

    function<void(int, int, int)> dfs = [&](int u, int p, int depth){
        if(u >= mid){
            if(d == -1){
                d = depth;
            }
            else{
                if(d != depth){
                    ok = false;
                }
            }
        }

        for(int to: adj[u]){
            if(to == p) continue;
            dfs(to, u, depth ^ 1);
        }
    };

    dfs(1, -1, 0);
    return ok;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    if(n % 2 == 0){
        cout << n << "\n";
        return 0;
    }

    int l = 1, r = n;
    for(int i = 1; i <= n; ++i){
        if(adj[i].size() == 1){
            l = max(l, i);
        }
    }

    while(l != r){
        int mid = (l + r + 1) / 2;
        if(check(mid)) l = mid;
        else r = mid - 1;
    }

    cout << l << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
1 2
1 3
1 4

output:

4

result:

ok single line: '4'

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 5924kb

input:

5
1 5
5 3
3 4
4 2

output:

5

result:

wrong answer 1st lines differ - expected: '3', found: '5'