【HDU - 5883】The Best Path(判断欧拉回路)
題干:
Alice is planning her travel route in a beautiful valley. In this valley, there are?NN?lakes, and?MM?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,...,ana1,a2,...,an) for each lake. If the path she finds is?P0→P1→...→PtP0→P1→...→Pt, the lucky number of this trip would be?aP0XORaP1XOR...XORaPtaP0XORaP1XOR...XORaPt. She want to make this number as large as possible. Can you help her?
Input
The first line of input contains an integer?tt, the number of test cases.?tt?test cases follow.?
For each test case, in the first line there are two positive integers?N?(N≤100000)N?(N≤100000)?and?M?(M≤500000)M?(M≤500000), as described above. The?ii-th line of the next?NN?lines contains an integer?ai(?i,0≤ai≤10000)ai(?i,0≤ai≤10000)?representing the number of the?ii-th lake.?
The?ii-th line of the next?MM?lines contains two integers?uiui?and?vivi?representing the?ii-th river between the?uiui-th lake and?vivi-th lake. It is possible that?ui=viui=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 4Sample Output
2 Impossible題目大意:
一個圖,每一條邊必須且只能經過一次,寫出經過點的路徑,類似于p1->p2->p1,求一條路徑,使得路徑的點的異或和最大,求這個最大值。
解題報告:
首先判斷聯通,然后看是否有歐拉回路或者歐拉通路,然后兩種情況分別判斷。如果都沒有則輸出impossible。
AC代碼:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<cctype> using namespace std; typedef long long ll; const int maxn=1e5+10; int n,m,in[maxn]; int a[maxn]; vector<int> vv[maxn]; int main() {int t,i,j,k,cnt,u,v,ans=0;int sum=0;cin>>t; while(t--){scanf("%d%d",&n,&m);cnt=0;for(i=1;i<=n;i++){scanf("%d",&a[i]);in[i]=0;vv[i].clear();}for(i=1;i<=m;i++){scanf("%d%d",&u,&v);vv[u].push_back(v);vv[v].push_back(u);in[u]++; in[v]++;}for(i=1;i<=n;i++){// cout<<i<<":"<<in[i]<<endl;cnt+=(in[i]&1);}if(cnt==1||cnt>2){puts("Impossible");continue;}ans=0;if(cnt){for(i=1;i<=n;i++){if(in[i]&1){ans^=a[i];if(((in[i]-1)/2)%2)ans^=a[i];}else if((in[i]/2)%2)ans^=a[i];}}else {sum=0;for(i=1;i<=n;i++){if((in[i]/2)%2)sum^=a[i];}for(i=1;i<=n;i++)ans=max(ans,sum^a[i]);}printf("%d\n",ans);}return 0; }?
總結
以上是生活随笔為你收集整理的【HDU - 5883】The Best Path(判断欧拉回路)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 申请信用卡拼音格式 字母要大写
- 下一篇: 【牛客 - 297C】little w