USACO fence

  题目的意思就是给你一个图, 输出他的欧拉路(欧拉通路 或者 欧拉回路),无向图欧拉回路判断条件是:1:图连通 2:所有点的度数为偶数     无向图欧拉通路的条件是:1:图连通 2:有且只有两个点的度数为奇数, 不过寻找欧拉路的代码是一样的,学习了新的建图方法,代码如下:

/*
    ID: m1500293
    LANG: C++
    PROG: fence
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;
struct edge
{
    int a, b, used;
    edge() {}
    edge(int a, int b, int used):a(a), b(b), used(used) {}
    int other(int x)
    {
        return x==a?b:a;
    }
}e[1100];
int F;    //边的数量
vector<int> G[510];
int node[510], nodenum, exist[510];
int degree[510];
vector<int> ans;

void dfs(int u)
{
    for(int i=0; i<G[u].size(); i++)
    {
        edge &tp = e[G[u][i]];
        if(!tp.used)
        {
            tp.used = 1;
            dfs(tp.other(u));
        }
    }
    ans.push_back(u);
}

struct cmp                  //高明
{
    int x;
    cmp() {}
    cmp(int x):x(x) {}
    bool operator()  (const int &a, const int &b) const
    {
        return e[a].other(x)<e[b].other(x);
    }
};

int main()
{
    //freopen("fence.in", "r", stdin);
    //freopen("fence.out", "w", stdout);
    memset(degree, 0, sizeof(degree));
    memset(exist, 0, sizeof(exist));
    nodenum = 0;
    scanf("%d", &F);
    for(int i=0; i<F; i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        if(!exist[u]) node[nodenum++] = u,  exist[u]=1;
        if(!exist[v]) node[nodenum++] = v,  exist[v]=1;
        degree[u]++; degree[v]++;
        e[i] = edge(u, v, 0);
        G[u].push_back(i);
        G[v].push_back(i);
    }
    sort(node, node+nodenum);
    for(int i=0; i<nodenum; i++)
        sort(G[node[i]].begin(), G[node[i]].end(), cmp(node[i]));
    int start = 0x3fffffff;
    for(int i=0; i<nodenum; i++)
        if(degree[node[i]]%2==1)
            start = min(start, node[i]);
    if(start == 0x3fffffff)
        start = node[0];
    dfs(start);
    for(int i=ans.size()-1; i>=0; i--)    //逆序输出
        printf("%d\n", ans[i]);
    return 0;
}
时间: 2024-10-10 05:47:09

USACO fence的相关文章

USACO 6.3 Fence Rails(一道纯剪枝应用)

Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber s

USACO 4.1 Fence Loops

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only at their endpoints though sometimes more than two fences

USACO 4.1 Fence Rails

Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber s

USACO 4.1 Fence Loops(Floyd求最小环)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only at their endpoints though sometimes more than two fences

USACO 3.3 fence 欧拉回路

题意:求给定图的欧拉回路(每条边只走一次) 若欧拉回路存在,图中只可能有0个or2个奇数度的点. 求解时,若有奇数度的点,则必须从该点开始.否则可以从任一点开始 求解过程:dfs 1 //主程序部分 2 # circuit is a global array 3 find_euler_circuit 4 circuitpos = 0 5 find_circuit(node 1) 6 --------------------------------------------- 7 # nextnod

usaco Electric Fence

这种有小数的题目总会令我格外头疼. /* ID: modengd1 PROG: fence9 LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> #include <math.h> using namespace std; long long ans,x1,x2; int n,m,p; int main() { freopen("fence9.in"

USACO 3.4 Electric Fence 皮克定理

题意:在方格纸上画出一个三角形,求三角形里面包含的格点的数目 因为其中一条边就是X轴,一开始想的是算出两条边对应的数学函数,然后枚举x坐标值求解.但其实不用那么麻烦. 皮克定理:给定顶点坐标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积A和内部格点数目i.边上格点数目b的关系:A = i + b/2 - 1. 有了这条定理就好办了. 三角形面积直接用公式就能算出来. 对于从点(0,0)到点(x,y)的线段,该线段上的格点数目即gcd(x,y)+1 这样A和b都有了,套公式就行了.

洛谷P2738 [USACO4.1]篱笆回路Fence Loops

P2738 [USACO4.1]篱笆回路Fence Loops 11通过 21提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 农夫布朗的牧场上的篱笆已经失去控制了.它们分成了1~200英尺长的线段.只有在线段的端点处才能连接两个线段,有时给定的一个端点上会有两个以上的篱笆.结果篱笆形成了一张网分割了布朗的牧场.布朗想将牧场恢复原样,出于这个考虑,他首先得知道牧场上哪一块区域的周长最小. 布朗将他的每段篱笆从1到N进行了标号

poj3253 Fence Repair STL优先队列

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=3253 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each