首页 > 其他分享 >P2731 [USACO3.3] 骑马修栅栏 Riding the Fences

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

时间:2024-06-08 14:22:34浏览次数:28  
标签:int max late USACO3.3 P2731 Riding Fences

原题链接

题解

贪心走最小的点,由于每个点都有偶数条边,所以能进入就一定能出去

code

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int to,id;
};
vector<node> G[505];
int late[505]={0};
int vis[1044]={0};
int start=1,finish;

stack<int> st;
int n,m;


void ss(int now)
{
    for(int i=late[now];i<G[now].size();i=late[now])
    {
        late[now]=i+1;
        int to=G[now][i].to,id=G[now][i].id;
        if(!vis[id])
        {
            vis[id]=1;
            ss(to);
        }
    }

    st.push(now);
}


int main()
{
    cin>>m;

    for(int i=1;i<=m;i++)
    {
        int u,v;
        cin>>u>>v;
        G[u].push_back({v,i});
        G[v].push_back({u,i});
        n=max(n,u);
        n=max(n,v);
    }


    int flag=0;
    for(int i=1;i<=n;i++)
    {
        sort(G[i].begin(),G[i].end(),[](const node &a,const node &b){return a.to<b.to;});
        int con=G[i].size();
        if(con&1)
        {
            if(!flag)
            {
                start=i;
                flag=1;
            }
            else
            {
                finish=i;
            }
        }
    }

    ss(start);

    while(st.size())
    {
        cout<<st.top()<<endl;
        st.pop();
    }
    return 0;
}

标签:int,max,late,USACO3.3,P2731,Riding,Fences
From: https://www.cnblogs.com/pure4knowledge/p/18238594

相关文章

  • CF479E Riding in a Lift
    题目大意一栋楼有\(n\)层,初始位置在\(a\)层,你可以移动到的\(y\)层满足\(\left|x-y\right|<\left|x-b\right|\)。不可以走到\(b\)层或留在原地,问一共走\(k\)次,有多少种走法。思路考虑简单的DP。记\(dp_{i,j}\)表示走到第\(i\)层,走了\(j\)次的方案数,\(l,r\)......
  • [React Typescript] Overriding and Removing Component Props
    UsingOmitimport{ComponentProps}from'react';import{Equal,Expect}from'../helpers/type-utils';exportconstInput=(props:Omit<ComponentProps<'input'>,'onChange'>&{onChange:......
  • Difference Between Method Overloading and Method Overriding in Java
    ThedifferencesbetweenMethodOverloadingandMethodOverridinginJavaareasfollows:MethodOverloading MethodOverridingMethodoverloadingis......
  • P1930 [USACO3.3]亚瑟王的宫殿
    #include<bits/stdc++.h>usingnamespacestd;#defineintlonglongintr,c;intkx,ky;intMap[1001][1001];namespacebfs{constintdx[8]={1,1,2,2,-1,-1,-......