The Best Path

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 798    Accepted Submission(s): 332

Problem Description
Alice is planning her travel route in a beautiful valley. In this valley, there are N lakes,
and M rivers
linking these lakes. Alice wants to start her trip from one lake, and enjoys the landscape by boat. That means she need to set up a path which go through every river exactly once. In addition, Alice has a specific number (a1,a2,...,an)
for each lake. If the path she finds is P0→P1→...→Pt,
the lucky number of this trip would be aP0XORaP1XOR...XORaPt.
She want to make this number as large as possible. Can you help her?
 
Input
The first line of input contains an integer t,
the number of test cases. t test
cases follow.



For each test case, in the first line there are two positive integers N (N≤100000) and M (M≤500000),
as described above. The i-th
line of the next N lines
contains an integer ai(∀i,0≤ai≤10000) representing
the number of the i-th
lake.



The i-th
line of the next M lines
contains two integers ui and vi representing
the i-th
river between the ui-th
lake and vi-th
lake. It is possible that ui=vi.
 
Output
For each test cases, output the largest lucky number. If it dose not have any path, output "Impossible".
 
Sample Input
  1. 2
  2. 3 2
  3. 3
  4. 4
  5. 5
  6. 1 2
  7. 2 3
  8. 4 3
  9. 1
  10. 2
  11. 3
  12. 4
  13. 1 2
  14. 2 3
  15. 2 4
 
Sample Output
  1. 2
  2. Impossible
 

题目的意思是遍历所有边,算出每个经历过的点异或起来最大值

首先并查集算出集合数,大于1不可能;然后判欧拉回路,若度为奇的大于2,则不可能,若度为奇的等于二,则路线固定,其所经历所有点,若等于零则枚举起点

这里有个小技巧,a^a=0,0^a=a 所以异或时重复经历的点只要判经历次数是否为奇数即可

另外有个坑的是位运算优先级较低,你写

  1. for(int i=1; i<=n; i++)
  2. {
  3. if(ans^a[i]>mx)
  4. mx=ans^a[i];
  5. }

就会wa,它先会判断>再异或

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace std;
  7. #define inf 0x3f3f3f3f
  8. #define mod 10000007
  9. #define maxn 100005
  10.  
  11. int a[maxn];
  12. int p[maxn];
  13. int pre[maxn];
  14. int n,m,sz;
  15.  
  16. int fin(int x)
  17. {
  18. if(x != pre[x])
  19. pre[x] = fin(pre[x]);
  20. return pre[x];
  21. }
  22.  
  23. void unio(int x,int y)
  24. {
  25. int x1=fin(x);
  26. int x2=fin(y);
  27. if(x1!=x2)
  28. {
  29. pre[x1]=x2;
  30. sz--;
  31. }
  32. }
  33.  
  34. int main()
  35. {
  36. int o,u,v;
  37. while(~scanf("%d",&o))
  38. {
  39. while(o--)
  40. {
  41. scanf("%d%d",&n,&m);
  42. for(int i=1; i<=n; i++)
  43. {
  44. scanf("%d",&a[i]);
  45. }
  46. memset(p,0,sizeof(p));
  47. for(int i=1; i<=n; i++)
  48. {
  49. pre[i]=i;
  50. }
  51. sz=n;
  52.  
  53. for(int i=0; i<m; i++)
  54. {
  55. scanf("%d%d",&u,&v);
  56. unio(u,v);
  57. p[u]++;
  58. p[v]++;
  59. }
  60.  
  61. if(sz>1)
  62. {
  63. printf("Impossible\n");
  64. continue;
  65. }
  66.  
  67. int tot=0;
  68. for(int i=1; i<=n; i++)
  69. {
  70. if(p[i]%2==1)
  71. tot++;
  72. }
  73. if(tot>2)
  74. {
  75. printf("Impossible\n");
  76. }
  77. else if(tot==2)
  78. {
  79. int ans=0;
  80. for(int i=1; i<=n; i++)
  81. {
  82. if(((p[i]+1)/2)%2)
  83. ans=ans^a[i];
  84. }
  85. printf("%d\n",ans);
  86. }
  87. else
  88. {
  89. int ans=0;
  90. for(int i=1; i<=n; i++)
  91. {
  92. if(((p[i]+1)/2)%2) ans=ans^a[i];
  93. }
  94. int mx=0;
  95. for(int i=1; i<=n; i++)
  96. {
  97. mx=max(mx,ans^a[i]);
  98. }
  99. printf("%d\n",mx);
  100. }
  101. }
  102. }
  103. return 0;
  104. }


hdu5883 The Best Path 2016-09-21 21:31 92人阅读 评论(0) 收藏的更多相关文章

  1. GitHub项目协作基本步骤 分类: C_OHTERS 2013-09-23 21:31 690人阅读 评论(0) 收藏

    1.查找某个项目,然后Fork 2.打开GitHub For Windows,发现刚才Fork的项目 3.对着项目点击Clone,将之复制至本地 4.使用Eclipse等进行开发,如新增一个文件 5. ...

  2. Poj 2349 Arctic Network 分类: Brush Mode 2014-07-20 09:31 93人阅读 评论(0) 收藏

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9557   Accepted: 3187 De ...

  3. Poj 1050 分类: Translation Mode 2014-04-04 09:31 103人阅读 评论(0) 收藏

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39058   Accepted: 20629 Desc ...

  4. sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏

    sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...

  5. python如何使用 os.path.exists()--Learning from stackoverflow 分类: python 2015-04-23 20:48 139人阅读 评论(0) 收藏

    Q&A参考连接 Problem:IOError: [Errno 2] No such file or directory. os.path.exists() 如果目录不存在,会返回一个0值. ...

  6. 排序练习——找出前m大的数字 分类: 排序 2015-06-08 09:33 21人阅读 评论(0) 收藏

    排序练习--找出前m大的数字 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 给定n个数字,找出前m大的数字.   输入  多组输 ...

  7. windows server 2008 R2域中的DC部署 分类: AD域 Windows服务 2015-06-06 21:09 68人阅读 评论(0) 收藏

    整个晚上脑子都有点呆滞,想起申请注册好的博客还从来都不曾打理,上来添添生机.从哪里讲起呢,去年有那么一段时间整个人就陷在域里拔不出来,于是整理了一些文档,害怕自己糊里糊涂的脑子将这些东西会在一觉醒来全 ...

  8. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  9. Hadoop集群日常运维 分类: A1_HADOOP 2015-03-01 21:26 502人阅读 评论(0) 收藏

    (一)备份namenode的元数据 namenode中的元数据非常重要,如丢失或者损坏,则整个系统无法使用.因此应该经常对元数据进行备份,最好是异地备份. 1.将元数据复制到远程站点 (1)以下代码将 ...

随机推荐

  1. maven GroupId 和ArtifactId通常填什么

    GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构.ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称.

  2. jquery clone 获取文本框值得问题

    1 clone 出来的文本框 默认不会把原来的事件也带过去 如果使用 $("#").clone(true);   true  可以将原来的事件带过去 获取文本框的值 可以使用事件 ...

  3. ContextLoaderListener和Spring MVC中的DispatcherServlet学习

    DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供Spring Web MVC的集中访问点,并且负责职责的分派,与Spring IoC容器无 ...

  4. selenium -- 鼠标悬停

    针对页面上的二级菜单,需要鼠标悬停才能进行操作. /** * Clicks (without releasing) in the middle of the given element. This i ...

  5. django通过url传递参数(编辑操作页面)

    在做到编辑部分时,想到的办法是在编辑上跳转到页面时给他一个包含唯一标识id的url,然后通过这个url中的id去查询出该条数据,将数据内容显示在编辑页面.   1.编辑按钮 <button on ...

  6. Winsock版本的“hello world!”

    1.基于TCP协议的“hello world!” 1)服务器端:WSAStartup()->socket()->bind()->listen()->accept()->s ...

  7. 常用修图工具的一些使用技巧及问题解决方法——ai

    一.ai如何修改画布大小 一. ai如何修改画布大小: 1. 左上角菜单中的文件——文档设置(也可以直接点菜单栏下边的控制栏中的文档设置) 2. 文档设置界面中,点击右上角“编辑画板“ 3. 此时面板 ...

  8. 【校招面试 之 C/C++】第31题 C++ 11新特性(二)之nullptr关键字

    1. 引入nullptr的原因 引入nullptr的原因,这个要从NULL说起.对于C和C++程序员来说,一定不会对NULL感到陌生.但是C和C++中的NULL却不等价.NULL表示指针不指向任何对象 ...

  9. MySQL中执行sql语句错误 Error Code: 1093. You can't specify target table 'car' for update in FROM clause

    MySQL中执行sql语句错误 Error Code: 1093. You can't specify target table 'car' for update in FROM clause 201 ...

  10. CookiesHelper

    /// <summary> ///CookiesHelper 的摘要说明 /// </summary> public class CookiesHelper { public ...