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
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.
3 2
3
4
5
1 2
2 3
4 3
1
2
3
4
1 2
2 3
2 4
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 }
The Best Path的更多相关文章
- NodeJs之Path
Path模块 NodeJs提供的Path模块,使得我们可以对文件路径进行简单的操作. API var path = require('path'); var path_str = '\\Users\\ ...
- 【原】实时渲染中常用的几种Rendering Path
[原]实时渲染中常用的几种Rendering Path 本文转载请注明出处 —— polobymulberry-博客园 本文为我的图形学大作业的论文部分,介绍了一些Rendering Path,比较简 ...
- Node.js:path、url、querystring模块
Path模块 该模块提供了对文件或目录路径处理的方法,使用require('path')引用. 1.获取文件路径最后部分basename 使用basename(path[,ext])方法来获取路径的最 ...
- VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%
1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Thinking in Unity3D:渲染管线中的Rendering Path
关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的 ...
- node之path模块
node之path模块 原文链接 //引用该模块 var path = require("path"); 1.路径解析,得到规范化的路径格式 对window系统,目录分隔为'', ...
- Linux系统修改PATH环境变量方法
在Linux安装一些软件通常要添加路径环境变量PATH.PATH环境变量通俗的讲就是把程序的路径"备案"到系统中,这样执行这些程序时就不需要输入完整路径,直接在bash输入程序名就 ...
- 利用XML FOR PATH 合并分组信息
-- ================================================ -- Description:合并分组内容 -- Author:夏保华 -- Date:2009 ...
随机推荐
- CSS区分Chrome和Firefox
CSS区分Chrome和FireFox 描述:由于Chrome和Firefox浏览器内核不同,对CSS解析有差别,因此常会有在两个浏览器中显示效果不同的问题出现,解决办法如下: /*Chrome*/ ...
- Java中static关键字声明的静态内部类与非静态内部类的区别
(1)内部静态类不需要有指向外部类的引用.但非静态内部类需要持有对外部类的引用.(2)非静态内部类能够访问外部类的静态和非静态成员.静态类不能访问外部类的非静态成员.他只能访问外部类的静态成员.(3) ...
- 日常Java 2021/10/19
Java集合框架 Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射. Collection接口又有3种子类型,Li ...
- 文件和目录之间建立链接 (ln)
- Leetcode中的SQL题目练习(一)
595. Big Countries https://leetcode.com/problems/big-countries/description/ Description name contine ...
- js 如何全部替代一个子串为另一个子串
更多描述: 假设有一个字符串 `hello. hello. hello. ` 需要替换为 `AAA`,即把 `hello. ` 替换为 `A` 如果需要全量替换字符串,可以使用 String.prot ...
- javaAPI2
---------------------------------------------------------------------------------------------------- ...
- android:textAppearance解析
Android的系统自带的文字外观设置及实际显示效果图 android:textAppearancexml布局里面设置文字的外观: 如"android:textAppearance=&quo ...
- JSP中session、cookie和application的使用
一.session (单用户使用) 1.用处:注册成功后自动登录,登录后记住用户状态等 使用会话对象session实现,一次会话就是一次浏览器和服务器之间的通话,会话可以在多次请求中保存和使用数据. ...
- uWSGI和WSGI之间的关系
一.WSGI 协议 WSGI:是一种协议规范,起到规范参数的作用,就像告诉公路一样,规定超车靠右行,速度不低于90km/h,等.但这一切都是对双方进行沟通,比如,重庆到武汉这条高速路,这儿重庆和武汉就 ...