QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#262028#7755. Game on a ForestSatsuki#WA 2ms8184kbC++171.5kb2023-11-23 14:50:532023-11-23 14:50:53

Judging History

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

  • [2023-11-23 14:50:53]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:8184kb
  • [2023-11-23 14:50:53]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; ++ i)
// #define int long long
using namespace std;
const int MAXN = 1e6 + 10, mod = 998244353;

int read()
{
    char cc(getchar()); int cn(0), flus(1);
    while(!isdigit(cc)) {if(cc == '-') flus = -flus; cc = getchar();}
    while(isdigit(cc)) cn = cn * 10 + cc - 48, cc = getchar();
    return cn * flus;
}

struct edge
{
    int to, next;
} e[MAXN << 1];

int n, m, head[MAXN], tot;
int col[MAXN], cnt, pcs[MAXN];

void add(int u, int v)
{
    e[++tot].next = head[u];
    head[u] = tot;
    e[tot].to = v;
}

void dfs(int u, int fa)
{
    col[u] = cnt;
    pcs[cnt] ++;
    for(int i = head[u]; i; i = e[i].next)
    {
        int v = e[i].to;
        if(v == fa) continue;
        dfs(v, u);
    }
}

signed main()
{
    n = read(), m = read();
    rep(i, 1, m)
    {
        int u = read(), v = read();
        add(u, v);
        add(v, u);
    }
    rep(i, 1, n)
    {
        if(!col[i])
        {
            col[i] = ++cnt;
            dfs(i, 0);
        }
    }
    int ans(0);
    if(cnt == 1)
    {
        if(pcs[1] & 1) ans = 1;
        else ans = pcs[1] - 1;
    }
    else
    {
        rep(i, 1, cnt)
        {
            if(pcs[i] & 1)
            {
                ans += 2 * pcs[i] - 2;
            }
            else
            {
                ans += pcs[i];
            }
        }
    }
    cout << ans;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 1
1 2

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 1ms
memory: 5360kb

input:

4 3
1 2
2 3
3 4

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: -100
Wrong Answer
time: 2ms
memory: 8184kb

input:

100000 1
4647 17816

output:

2

result:

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