The Best Path

The Best Path

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 207    Accepted Submission(s): 91

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

2
3 2
3
4
5
1 2
2 3
4 3
1
2
3
4
1 2
2 3
2 4

Sample Output

2
Impossible

思路:欧拉路,欧拉回路;

首先判断给定的边的点是否连通,因为要经过每条边一次,所以用欧拉路来判断,如果是欧拉路的话,那么就是原来所有边经过的点的亦或和,否则如果是欧拉回路的话那么起点会多经过一次,那么枚举起点就行了;

  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<string.h>
  4 #include<iostream>
  5 #include<queue>
  6 #include<stdlib.h>
  7 #include<math.h>
  8 #include<set>
  9 using namespace std;
 10 int bin[100005];
 11 int cnt[100005];
 12 int du[100005];
 13 int ans[100005];
 14 set<int>que;
 15 int main(void)
 16 {
 17     int n;
 18     scanf("%d",&n);
 19     while(n--)
 20     {
 21         que.clear();
 22         int i,j;
 23         memset(cnt,0,sizeof(cnt));
 24         for(i = 0; i <= 100005; i++)
 25         {
 26             bin[i] = i;
 27             du[i] = 1;
 28         }
 29         int N,M;
 30         scanf("%d %d",&N,&M);
 31         if(M==0)printf("0\n");
 32         else
 33         {
 34             for(i = 1; i <= N; i++)
 35             {
 36                 scanf("%d",&ans[i]);
 37             }
 38             while(M--)
 39             {
 40                 int x,y;
 41                 scanf("%d %d",&x,&y);
 42                 cnt[x]++;
 43                 cnt[y]++;
 44                 int xx,yy;
 45                 for(xx = x; bin[xx]!=xx;)
 46                     xx = bin[xx];
 47                 for(yy = y; bin[yy]!=yy;)
 48                     yy = bin[yy];
 49                 if(xx != yy)
 50                 {
 51                     if(du[xx]>du[yy])
 52                     {
 53                         bin[yy] = xx;
 54                         du[xx] += du[yy];
 55                     }
 56                     else
 57                     {
 58                         bin[xx] = yy;
 59                         du[yy] += du[xx];
 60                     }
 61                 }
 62             }
 63             for(i = 1; i <= N; i++)
 64             {
 65                 if(cnt[i])
 66                 {
 67                     int xx;
 68                     for(xx = i; xx!=bin[xx];)
 69                         xx = bin[xx];
 70                     que.insert(xx);
 71                 }
 72             }
 73             int cn = 0;
 74             for(i = 1; i <= N; i++)
 75             {
 76                 if(cnt[i])
 77                 {
 78                     if(cnt[i]%2)
 79                     {
 80                         cn++;
 81                     }
 82                 }
 83             }
 84             int sum = 0;
 85             if(cn == 1|| cn > 3||que.size()!=1)
 86             {
 87                 //printf("1\n");
 88                 printf("Impossible\n");
 89             }
 90             else if(cn == 2)
 91             {
 92                 for(i = 1; i <= N; i++)
 93                 {
 94                     if(cnt[i]>1)
 95                         cnt[i]=cnt[i]+1;
 96                     cnt[i]/=2;
 97                     if(cnt[i]%2)
 98                         sum^=ans[i];
 99                 }
100                 printf("%d\n",sum);
101             }
102             else
103             {
104                 for(i = 1; i <= N; i++)
105                 {
106                     if(cnt[i])
107                     {
108                         sum ^= ans[i];
109                     }
110                 }
111                 int flag = 0;
112                 int k = sum;
113                 for(i = 1; i <= N; i++)
114                 {
115                     if(cnt[i])
116                     {
117                         if(!flag)
118                             sum^=ans[i],flag = 1;
119                         else sum = max(sum,k^ans[i]);
120                     }
121                 }
122                 printf("%d\n",sum);
123             }
124         }
125     }
126     return 0;
127 }
时间: 2024-08-27 21:55:57

The Best Path的相关文章

Linux下修改环境变量PATH

1.什么是环境变量(PATH) 在Linux中,在执行命令时,系统会按照PATH的设置,去每个PATH定义的路径下搜索执行文件,先搜索到的文件先执行. 我们知道查阅文件属性的指令ls 完整文件名为:/bin/ls(这是绝对路径), 那你会不会觉得很奇怪:"为什么我可以在任何地方执行/bin/ls这个指令呢? " 为什么我在任何目录下输入 ls 就一定可以显示出一些讯息而不会说找不到该 /bin/ls 指令呢? 这是因为环境变量 PATH 的帮助所致呀! 当我们在执行一个指令癿时候,举例

Description Resource Path Location Type The superclass &quot;javax.servlet.http.HttpServlet&quot; was not foun

一段时间没亲自建新项目玩乐,今天建立了一Maven project的时候发现了以下异常,Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path index.jsp /easyBuy/src/main/webapp line 1 JSP Problem 经过查找原因,原来是因为忘记设置server

Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]56528192: startup date [Tue Sep 19 15:05:24 CST 2017]; root of context hierarchy 2017-09-19 15:05:24.858 INFO 9986 --

Linux下修改.bash_profile 文件改变PATH变量的值

Linux中含有两个重要的文件 /etc/profile和$HOME/.bash_profile 每当系统登陆时都要读取这两个文件,用来初始化系统所用到的变量,其中/etc/profile是超级用户所用,$HOME/.bash_profile是每个用户自己独立的,我们可以修改该文件来设置一些变量. 命令用法如下 $ cd (进入用户登陆目录) $ls –al .bash_profile(.bash_profile为隐藏文件,因此要用ls –a命令查找) $vi .bash_profile(用vi

配置class PATH

声明:本栏目所使用的素材都是凯哥学堂VIP学员所写,学员有权匿名,对文章有最终解释权:凯哥学堂旨在促进VIP学员互相学习的基础上公开笔记. PATH->是用来配置DOS中的快捷命令,里面存储很多路径,从前置方式去搜索 PATHEXT->后缀配置,搜索的时候配合你名字进行搜索 DOS中的临时环境变量(DOS关闭就失效): 1.SET环境变量名称=环境变量内容 2.临时的环境变量配置会把之前的环境变量内容全部覆盖掉,所以以前的PATH内容就丢失了 3.SET PATH=C:\program Fil

使用path监听指定文件系统的变化

在以前的JAVA版本中,如果程序需要检测文件的变化,那么需要开辟一个线程每隔一段时间去遍历一次指定的目录,如果发现此次遍历结果和上次不同,那么就认为文件变动了 ,这样的方式非常繁琐,JAVA 7之后的NIO.2 Path类提供了一个方法来监听指定文件目录内文件的变化状态. 1.获取文件系统的WatchService对 2.使用Path类的方法去注册一个监听,指定监听文件的哪些状态,如新增.修改.删除. package com.nio2; import java.io.IOException;im

Hive报错 Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:user.name%7D

报错信息如下 Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:user.name%7D 解决方法: 编辑 hive-site.xml 文件,添加下边的属性 <property> <name>system:java.io.tmpdir<

leetcode 656. Coin Path

Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The integer B denotes that from any place (suppose the index is i) in the array A, you can jump to any one of the place in the array A indexed i+1, i+2,

如何更改系统环境变量PATH

1. 如何更改系统环境变量PATH? 答:如果是永久更改系统环境变量,对所有用户生效,有两种办法:推荐使用第三种. (1)vim /etc/profile  加入 PATH=$PATH:/usr/local/mysql/bin/ (2)vim /etc/bashrc 加入 PATH=$PATH:/usr/local/mysql/bin/ (3)vim /etc/profile.d/path.sh #!/bin/bash export PATH=$PATH:/ usr/local/mysql/bi

POJ3967Ideal Path[反向bfs 层次图]

Ideal Path Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 1754   Accepted: 240 Description New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colo