蒟蒻WA了最后两个点,求助

P2731 [USACO3.3] 骑马修栅栏 Riding the Fences

CHClFNO @ 2021-08-11 15:05:15

如题

#include <bits/stdc++.h>
using namespace std;
int n, m, t[30000], h[30000], cnt, sum;
bool vis[30000];
struct lalala {
    int u, v, next;
} e[30000];
void add(int u, int v) {
    cnt++;
    e[cnt].u = u;
    e[cnt].v = v;
    e[cnt].next = h[u];
    h[u] = cnt;
}
void dfs(int x) {
    printf("%d\n", x);
    int k = 0;
    for (int i = h[x]; i > 0; i = e[i].next) {
        if (!vis[i] && (k == 0 || e[i].v < e[k].v))
            k = i;
    }
    if (k == 0)
        return;
    sum++;
    vis[k] = 1;
    if (k % 2 == 1)
        vis[k + 1] = 1;
    else
        vis[k - 1] = 1;
    dfs(e[k].v);
    return;
}
int main() {
    scanf("%d", &m);
    int u, v;
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &u, &v);
        add(u, v);
        add(v, u);
        t[u]++;
        t[v]++;
        n = max(n, max(u, v));
    }
    int k = INT_MAX;
    for (int i = 1; i <= n; i++) 
        if (t[i] % 2 == 1)
            k = min(k, i);
    if (k == INT_MAX)
        k = 1;
    dfs(k);
    return 0;
}

非常感谢


|