c++

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

星爵 @ 2017-03-12 10:46:55

#include<cstdio>  
#include<iostream>  
#include<cstring>  
using namespace std;  
int du[610],ans[610],map[610][610],n,c=0,max1=0;  
void search(int v)
{  
    for(int i=1;i<=max1;i++)
        if(map[v][i]>0)
        {  
            map[v][i]--;  
            map[i][v]--;  
            search(i);   
        }  
    ans[++c]=v;  
}  
int main()
{  
    cin>>n;  
    int u,v;  
    for(int i=1;i<=n;i++)
    {  
        cin>>u>>v;  
        map[u][v]++;
        map[v][u]++;  
        du[u]++; 
        du[v]++;
        max1=max(max(v,u),max1); 
    }   
    int star=0;  
    for(int i=1;i<=max1;i++)
        if(du[i]%2==1)
        {
            star=i;  
            break;  
        }  
    if(star==0)
        for(int i=1;i<=max1;i++)  
            if(du[i]>0)
            {
                star=i;  
                break;  
            }  
    search(star);  
    for(int i=c;i>=1;i--)
        cout<<ans[i]<<endl;  
}

by ZJH365 @ 2018-08-16 22:11:38

What do you want?


|