zoukankan      html  css  js  c++  java
  • 2016青岛网络赛 The Best Path

    The Best Path

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

    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的联通块,如果没有,取最大值即可,如果>1个,不可能;
       否则判断是欧拉路还是欧拉回路,欧拉路每个点直接求贡献即可,回路则要取一个使得答案最大的起点,因为起点多算1次;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, rt<<1
    #define Rson mid+1, R, rt<<1|1
    const int maxn=1e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,k,t,p[maxn],a[maxn],d[maxn],q[maxn],ans;
    int find(int x)
    {
        return p[x]==x?x:p[x]=find(p[x]);
    }
    int main()
    {
        int i,j;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            ans=0;
            memset(d,0,sizeof d);
            memset(q,0,sizeof q);
            rep(i,1,n)scanf("%d",&a[i]),p[i]=i;
            while(m--)
            {
                int b,c;
                scanf("%d%d",&b,&c);
                d[b]++,d[c]++;
                int fa=find(b),fb=find(c);
                if(fa!=fb)p[fa]=fb;
            }
            int cnt=0;
            rep(i,1,n)
            {
                int fa=find(i);
                if(p[fa]!=i&&!q[p[fa]])cnt++,q[p[fa]]=1;
            }
            if(cnt>1){puts("Impossible");continue;}
            else if(cnt==0)
            {
                rep(i,1,n)ans=max(ans,a[i]);
                printf("%d
    ",ans);
                continue;
            }
            cnt=0;
            rep(i,1,n)
            {
                if(d[i]&1)cnt++;
                int now=(d[i]+1)/2;
                if(now&1)ans^=a[i];
            }
            if(cnt==2)
            {
                printf("%d
    ",ans);
            }
            else if(cnt==0)
            {
                ans=0;
                int ma=0;
                rep(i,1,n)
                {
                    int now=(d[i]+1)/2;
                    if(now&1)ans^=a[i];
                }
                rep(i,1,n)if(d[i])ma=max(ma,ans^a[i]);
                printf("%d
    ",ma);
            }
            else puts("Impossible");
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    [C++]DirectShow检测音视频输入设备及其采集参数
    [C#] 使用Accord.Net,实现相机画面采集,视频保存及裁剪视频区域,利用WriteableBitmap高效渲染
    [C#]使用第三方开源库iText7.pdfHtml,将Html转换成Pdf,以及如何以Html作为打印模板
    C# 佳能相机SDK对接,采集并保存视频,使用WriteableBitmap高效渲染
    wpf常用类型转换器,支持基元类型、可空基元类型、枚举
    wpf单位转换及DPI获取
    使用wpf技术实现画图工具
    InstallShield 创建 visual studio 工程的时候 指向 任意 visual studio 版本 方法 (修改 计算机 默认 visual studio shell 版本)
    WPF ScrollViewer(滚动条) 自定义样式表制作 再发一套样式 细节优化
    C#实现屏幕指定区域截屏
  • 原文地址:https://www.cnblogs.com/dyzll/p/5879405.html
Copyright © 2011-2022 走看看