hdu 5883(欧拉路)

Posted qingfengdahui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 5883(欧拉路)相关的知识,希望对你有一定的参考价值。

这道题实际上是一道判断是否含有欧拉路的题,欧拉路是经过所有的边有且一次,分为欧拉通路和欧拉回路,

首先我以为要先判断一下该无向图是否是通路(可以用dfs或bfs遍历一遍),但是后来发现不用判断,题目已经说明其是通路,

这就好做多了,首先分析每个点的度,如果奇数度顶点为2个,则为欧拉通路,且起点和终点分别是这两个奇数度顶点,这时我们发现求其路径中经过顶点的异或时(异或满足交换律),

就是每个顶点需要异或自己的值度/2次(异或自己偶数次相当于没异或,奇数次相当于异或自己一次),其中起点和终点都需要加一次(奇数),

如果奇数度顶点为0个,则为欧拉回路,此时起点需要多异或一次,因为最终会回到起点(如1-2-3-1,这里1的度为2,需要异或自己2/2+1次)所以我们需要判断哪个顶点多异或

一次才会最大。

除了这2种情况,都为impossible

 1 #include<iostream>
 2 #include<string.h>
 3 #include<string>
 4 #include<sstream>
 5 #include<vector>
 6 #include<deque>
 7 #include<map>
 8 #include<algorithm>
 9 #include<iomanip>
10 #include<math.h>
11 #include<set>
12 using namespace std;
13 
14 typedef long long ll;
15 typedef unsigned long long ull;
16 
17 int degree[100005];
18 int value[100005];
19 int main()
20 {
21     int t, n, m;
22     cin >> t;
23     while (t--)
24     {
25         memset(degree, 0, sizeof(degree));
26         int a, b;
27         cin >> n >> m;
28         for (int i = 1; i <= n; i++)
29             scanf_s("%d", &value[i]);
30         for (int i = 0; i < m; i++)
31         {
32             scanf_s("%d %d", &a, &b);
33             degree[a]++;
34             degree[b]++;
35         }
36         int ans = 0;
37         int count = 0;
38         for (int i = 1; i <= n; i++)
39         {
40             if (degree[i] & 1)
41             {
42                 count++;
43                 ans = ans ^ value[i];
44             }
45         }
46         if (count != 0 && count != 2)
47         {
48             cout << "Impossible" << endl;
49             continue;
50         }
51         if (count == 2)
52         {
53             for (int i = 1; i <= n; i++)
54             {
55                 int x = degree[i] / 2;
56                 if (x & 1)
57                 {
58                     ans = ans ^ value[i];
59                 }
60             }
61             cout << ans << endl;
62             continue;
63         }
64         else
65         {
66             for (int i = 1; i <= n; i++)
67             {
68                 int x = degree[i] / 2;
69                 if (x & 1)
70                 {
71                     ans = ans ^ value[i];
72                 }
73             }
74             int res = -1;
75             for (int i = 1; i <= n; i++)
76             {
77                 res = max(res, ans^value[i]);
78             }
79             cout << res << endl;
80             continue;
81         }
82     }    
83     return 0;
84 }

 

以上是关于hdu 5883(欧拉路)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5883 The Best Path (欧拉路或者欧拉回路)

HDU5883 The Best Path(并查集+欧拉路)

HDU 5883 The Best Path(并查集+欧拉回路 or 欧拉路)——2016 ACM/ICPC Asia Regional Qingdao Online

HDU5883 The Best Path(欧拉回路 | 通路下求XOR的最大值)

2016 ACM/ICPC Asia Regional Qingdao Online HDU5883

The Best Path---hdu5883(欧拉路径)