刚学OI,只A了2个

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

GODTREE @ 2023-11-26 10:19:03

#include <bits/stdc++.h>
using namespace std;
bool vis[505][505],g[505][505];
int n,m;
void dfs(int f,int u)
{
    cout<<u<<"\n";
    vis[f][u]=1;
    vis[u][f]=1;
    for (int i=1;i<=500;i++)
    {
        if (g[u][i]==1&&vis[u][i]==0)
        {
            dfs(u,i);
        }
    }
}
int main()
{
    cin>>m;
    for (int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        g[x][y]=1;
        g[y][x]=1;
    }
    dfs(1,1);
    return 0;
}

|