zoukankan      html  css  js  c++  java
  • 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 P0P1...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 (N100000) and M (M500000), as described above. The i-th line of the next N lines contains an integer ai(i,0ai10000) 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
    ");
     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
    ");
     88                 printf("Impossible
    ");
     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
    ",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
    ",sum);
    123             }
    124         }
    125     }
    126     return 0;
    127 }
    油!油!you@
  • 相关阅读:
    servlet 表单加上multipart/form-data后request.getParameter获取NULL(已解决)
    火狐浏览器通过配置文件锁定主页
    如何最快速的完成智慧树期末考试(有钱的大佬请绕道)记----智慧树考试生产力
    java代码发送邮箱验证码与qq邮箱smtp服务
    邮件服务器
    Android requestcode resultcode的作用
    伽卡拉他学生客户端无法运行任务管理器的解决方法
    Django自定义UserModel并实现认证和登录
    {%csrf_token%}的作用
    Producer Flow Control 和 vmQueueCursor
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5879672.html
Copyright © 2011-2022 走看看