UVa 539 卡坦岛

题意:某游戏的玩家会建造一些公路,公路是连接两个岛的,这样形成一个图。顶点是岛,边是公路,且边长均为1。要求每条边只能走一遍,这样最长的公路是多长。

思路:回溯法。以所有的点都开始枚举一次。由于这里每条边只能访问一次,而顶点是可以访问多次的,所以不能简单地用vis[26]数组来判断,而是用g[][]的值来表示边的条数,这样,输入时边的相应g值++,选择一条边时,则相应g值--。这样没要输出路径,所以连路径都没有保存,直接修改g值就可以了。但不要忘了,dfs之后还需要恢复g值,dfs下面相当于不选择这条边,而你之前已经把这条边删了,所以这时应恢复。

Code:

#include<stdio.h>
#include<string.h>

void dfs(int n,int cnt,int pre);

int g[26][26];
//int vis[26];
int maxlen;

int main()
{
  freopen("539.in","r",stdin);
  freopen("539.out","w",stdout);

  int n,m;
  while(scanf("%d%d",&n,&m)==2 && n && m)
  {
    memset(g,0,sizeof(g));
    for(int i=0;i<m;++i)
    {
      int a,b;
      scanf("%d%d",&a,&b);
      g[a][b]++;
      g[b][a]++;
    }
    maxlen=0;
    dfs(n,0,-1);
    printf("%d\n",maxlen);
  }
  return 0;
}

void dfs(int n,int cnt,int pre)
{
  for(int i=0;i<n;++i)
  {
      if(pre==-1)
      {
        dfs(n,cnt,i);
      }
      else if(g[pre][i]>0)
      {
        //vis[i]=1;
        g[pre][i]--;
        g[i][pre]--;
        cnt++;
        dfs(n,cnt,i);
        //vis[i]=0;
        g[pre][i]++;
        g[i][pre]++;
        cnt--;//不要忘了这个!!这里相当于不再选择i,则长度减1
      }
  }
  maxlen=cnt>maxlen?cnt:maxlen;
}
时间: 2024-10-28 23:44:09

UVa 539 卡坦岛的相关文章

uva 539 The Settlers of Catan(回溯)

uva 539 The Settlers of Catan Within Settlers of Catan, the 1995 German game of the year, players attempt to dominate an island by building roads, settlements and cities across its uncharted wilderness. You are employed by a software company that jus

UVA - 539

  The Settlers of Catan  Within Settlers of Catan, the 1995 German game of the year, players attempt to dominate an island by building roads, settlements and cities across its uncharted wilderness. You are employed by a software company that just has

为什么说“概率”带来一场现代革命?

作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁转载. 概率是生活中平常不过的概念.我们用概率来量化某种结果的可能性.日常生活中常见到概率.成功有概率,体育比赛的胜负有概率,彩票中奖也有概率.概率就是“概率论”这门学科研究的核心.不过,像概率这样“日常”的概念,是在16世纪文艺复兴时才成为数学家研究的课题. 最先研究概率论的是一位名为卡尔达诺的数学家.他研究了一个概率问题: 扔两个色子,总和为10的概率有多大? 扔色子这个游戏,大家常玩.色子是一个方块,六个

爆零狗的北京9日游

感谢曾大和zzx我才有了参加ctsc和apio的机会. Day1 赶早上第一班飞机实在是有点累.到了北京着实被五星级昆泰(百泰)酒店的阵势吓到了(据说有的学科竞赛住学生宿舍).认识了原本应该和我住一间的广西oier,后来每场比赛都被屠才知道是一位广西神犇orz. 经过一连串交易最终和lxe住了一间,领了密码条,泡了个澡就早早睡觉了,毕竟第二天还有ctsc一试. Day2 一进考场发现cjk坐在我旁边,阴吹斯汀.T1时空旅行果断把我和众神犇区分开来了,脑海中飞快闪过各种各样奇怪的骗分算法,也曾经一

桑吉轮监视现场:东侧持续冒油 疑似有燃料油泄漏

But as Pallanza will sing again, she is bound to find it out sooner or later.Eh! no doubt, Signor Hugo; but by the time she finds out I hope to be married. In that case it does not matter. Besides, I am going to make Pallanza promise not to sing anyw

百度京东方科技的经费卡老师的减肥了上岛咖啡;是

http://www.ebay.com/cln/bahon26/cars/167375558010/2015-02-02 http://www.ebay.com/cln/mcn4247/cars/167375562010/2015-02-02 http://www.ebay.com/cln/h_lon34/cars/167648505015/2015-02-02 http://www.ebay.com/cln/hocn_6nvxf8oft/cars/167353296014/2015-02-02

UVA 11367 - Full Tank?(最短路+DP)

UVA 11367 - Full Tank? 题目链接 题意:给定一个无向图,每个点有一个加油站,有一个油价,现在一辆车,每次询问要从起点s走到t,邮箱容量为c,问最小代价 思路:dijkstra算法,d数组多一个状态,表示当前油量即可 不过这题如果每次都把所有状态转移完,挺费时间的,卡着时间过的 后面改成每次1升1升加油去转移状态,效率会比较快,因为有很多无用状态可以省去 代码: #include <cstdio> #include <cstring> #include <

【最小矩形面积覆盖:凸包+旋转卡壳】UVA 10173 Smallest Bounding Rectangle

[最小矩形面积覆盖:凸包+旋转卡壳]UVA 10173 Smallest Bounding Rectangle 题目链接:UVA 10173 Smallest Bounding Rectangle 题目大意 给你n个点,求能够覆盖所有点集的最小矩形面积. 笔者的第2道凸包题目,凸包 + 旋转卡壳,实现点集的最小矩形面积覆盖问题 ">=0"写成"<=0"坑了我一下午!QAQ 说一下思路 ①Graham's Scan法构建凸包,时间复杂度O(nlogn) ②

uva 757 Gone Fishing (贪心)

uva 757 Gone Fishing John is going on a fishing trip. He has h hours available ( ), and there are n lakes in the area ( ) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel fr