55分(#1过了)求找错

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

YOLO_IOLO @ 2024-08-06 00:44:12

#include<bits/stdc++.h>
using namespace std;
int m,u,v,s[1010][1010],du[1010],cnt_ji,start = 0,cnt[1010],maxx = -999;
stack<int> sta;
void dfs(int x)
{
    for (int i = 1;i <= maxx;i++)
    {
        if (s[x][i])
        {
            s[x][i] = 0;
            s[i][x] = 0;
            dfs(i);
        }
    }
    sta.push(x);
}
int main()
{
    cin >> m;
    for (int i = 1;i <= m;i++)
    {
        cin >> u >> v;
        s[u][v] = 1;
        s[v][u] = 1;
        du[u]++;
        du[v]++;
        maxx = max(u,max(maxx,v));
    }
    for (int i = 1;i <= m;i++)
    {
        if (du[i] % 2 == 1)
        {
            cnt[cnt_ji] = i;
            cnt_ji++;
        }
    }
    if (cnt_ji == 2)
    {
        start = min(cnt[0],cnt[1]);
    }
    if(cnt_ji != 2)
    {
        for(int i = 1;i <= m;i++)
        {
            if(du[i])
            {
                start = i;
                break;
            }
        }
    }
    dfs(start);
    while(!sta.empty())
    {
        cout << sta.top() << endl;
        sta.pop();
    }
    /*
    for (int i = 1;i <= 4;i++)
    {
        for (int j = 1;j <= 4;j++)
        {
            cout << s[i][j] << ' ';
        }
        cout << endl;
    }
    */
    return 0;
}
/*
#1的样例 
3
2 3
3 4
4 2

2
3
4
2
*/

|