QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#51705#2596. Even ForestAmalgadon#WA 5ms9652kbC++171.0kb2022-10-03 16:03:462022-10-03 16:03:49

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-03 16:03:49]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:9652kb
  • [2022-10-03 16:03:46]
  • 提交

answer

#include <iostream>

#define Rep(i, n) for (int i = 1; i <= n; i ++)
#define RepG(i, x) for (int i = head[x]; i; i= edge[i].next)
#define v edge[i].to

using namespace std;

const int N = 1000100;
typedef long long LL;

struct Edge{ int to, next;} edge[N * 2];
int head[N], num;
void add_edge(int a, int b){edge[++ num] = (Edge){b, head[a]}, head[a] = num;}

int c[N], f[N][2], d[N];
void dfs(int x, int fa)
{
    c[x] = !c[fa];
    bool flag = false;
    RepG(i, x) if (v != fa){ dfs(v, x); flag = true;}
    if (!flag){ f[x][c[x]] = 0, f[x][!c[x]] = 1e9; return;}
    RepG(i, x) if (v != fa) f[x][c[x]] += min(f[v][c[x]], f[v][!c[x]] + 1);
    RepG(i, x) if (v != fa) f[x][!c[x]] += f[v][!c[x]];
}

int main()
{
    int n;
    cin >> n;
    Rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
        add_edge(a, b);
        add_edge(b, a);
        d[a] ++, d[b] ++;
    }

    dfs(1, 0);

    if (d[1] == 1) cout << f[1][c[1]] << endl;
    else cout << min(f[1][0], f[1][1]) << endl;

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 9652kb

input:

4
1 2
2 3
3 4

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

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: 9628kb

input:

6
1 2
2 3
4 2
4 5
6 4

output:

2

result:

wrong answer 1st numbers differ - expected: '1', found: '2'