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 ...
随机推荐
- Python中类的相关介绍
本文主要介绍python中类的概念性内容,如类的定义.说明及简单使用 1. 类的简单介绍 1 # -*- coding:utf-8 -*- 2 # Author:Wong Du 3 4 ''' 5 - ...
- 删除button中除label之外的View
因为button中的UIButtonLabel判断class类型时,会被认为是view,所以想删除view类型的子控件时,会将label也删掉,从而无法显示title,此时,可以给指定的View添加t ...
- OS开发之Objective-C与JavaScript的交互
UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS ...
- Mave 下载与安装
一,Maven 介绍 我们在开发中经常需要依赖第三方的包,包与包之间存在依赖关系,版本间还有兼容性问题,有时还需要将旧的包升级或降级,当项目复杂到一定程度时包管理变得非常重要.Maven是当前最受欢迎 ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(3. 用ansible管理机器和软件)
1. 简介 1.1. 公司环境使用的puppet,但是我更喜欢ansible,原因有二,第一,我是红帽的忠粉:),第二,我对python比较熟悉 1.2. ansible官方网站:https://ww ...
- springMVC中@requestMapper的使用和注意事项
package com.hope.controller;import org.springframework.stereotype.Controller;import org.springframew ...
- 二叉搜索树、平衡二叉树、红黑树、B树、B+树
完全二叉树: 空树不是完全二叉树,叶子结点只能出现在最下层和次下层,且最下层的叶子结点集中在树的左部.如果遇到一个结点,左孩子不为空,右孩子为空:或者左右孩子都为空:则该节点之后的队列中的结点都为叶子 ...
- 【C/C++】旋转数组的最小数字/ 剑指offer
#include <bits/stdc++.h> using namespace std; class Solution { public: int minNumberInRotateAr ...
- js--生成器总结
前言 生成器gengrator是es6 新增的函数功能,它允许你定义一个包含自有迭代算法的函数, 同时它可以自动维护自己的状态. 本文来总结一下JavaScript 中生成器的相关知识点. 正文 1. ...
- Redis慢查询配置和优化
目录 一.介绍 二.参数配置 sql动态配置 配置文件设置 三.sql操作 四.优化 一.介绍 慢查询只记录redis执行时间,并不记录redis服务到客户端之间的网络问题. 超过多少毫秒的才被记录 ...