ACdream 1028 Path
先思考一下序列上应该怎么做。
如果某段和为x,并且x为偶数,那么比x小的偶数,一定是这段的子段。
如果某段和为x,并且x为奇数,那么比x小的奇数,一定是这段的子段。
因此....只要寻找最大的连续的和为奇数的是多少,偶数的是多少。然后对询问就可以o(1)输出了。
推广到树上,就是只要计算路径上最大的奇数和最大的偶数。 树dp即可解决。 树dp写搓了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
#include<ctime>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} int n,q;
bool f[],L[];
struct Edge
{
int u,v,w,nx;
}e[];
int sz,h[]; int mx[][];
int max0,max1;
int a[]; struct X
{
int from, num;
X()
{
num = ;
from = ;
}
X(int From,int Num)
{
from=From;
num=Num;
}
bool operator < (const X &a) const {
return num<a.num;
}
}; void add(int a,int b,int c)
{
e[sz].u=a; e[sz].v=b; e[sz].w=c; e[sz].nx=h[a]; h[a]=sz++;
} struct cmp{
bool operator ()(int &a,int &b)
{
return a<b;
}
}; void dfs(int x)
{
f[x]=; priority_queue<X>Q[]; for(int i=h[x];i!=-;i=e[i].nx)
{
if(f[e[i].v]==) continue; mx[x][e[i].w%]=e[i].w; dfs(e[i].v); if(mx[e[i].v][]!=-) Q[(e[i].w+mx[e[i].v][])%].push(X(e[i].v,e[i].w+mx[e[i].v][]));
if(mx[e[i].v][]!=-) Q[(e[i].w+mx[e[i].v][])%].push(X(e[i].v,e[i].w+mx[e[i].v][])); if(mx[e[i].v][]==-||mx[e[i].v][]==-)
{
if(Q[(e[i].w)%].empty()) Q[(e[i].w)%].push(X(e[i].v,e[i].w));
}
} if(!Q[].empty()) mx[x][]=Q[].top().num;
if(!Q[].empty()) mx[x][]=Q[].top().num; int p=; X f1,f2,g1,g2;
if(!Q[].empty()) { f1=Q[].top(); Q[].pop(); }
if(!Q[].empty()) { f2=Q[].top(); Q[].pop(); } if(!Q[].empty()) { g1=Q[].top(); Q[].pop(); }
if(!Q[].empty()) { g2=Q[].top(); Q[].pop(); } if(f1.num+f2.num!=) a[p++]=f1.num+f2.num;
if(g1.num+g2.num!=) a[p++]=g1.num+g2.num; if(f1.from!=g1.from)
{
if(f1.num+g1.num!=) a[p++]=f1.num+g1.num;
} else
{
if(f1.num+g2.num!=) a[p++]=f1.num+g2.num;
if(f2.num+g1.num!=) a[p++]=f2.num+g1.num;
} for(int i=;i<p;i++)
{
if(a[i]%==) max0=max(max0,a[i]);
if(a[i]%==) max1=max(max1,a[i]);
} } int main()
{
while(~scanf("%d%d",&n,&q))
{
memset(f,sz=,sizeof f);
memset(h,-,sizeof h);
memset(L,,sizeof L); for(int i=;i<n;i++)
{
int u,v,w; scanf("%d%d%d",&u,&v,&w);
add(u,v,w); add(v,u,w);
} memset(mx,-,sizeof mx);
max0=max1=-; dfs();
for(int i=;i<=q;i++)
{
int x; scanf("%d",&x);
if(x<) printf("No\n");
else
{
if(x%==)
{
if(max0>=x) printf("Yes\n");
else printf("No\n");
}
if(x%==)
{
if(max1>=x) printf("Yes\n");
else printf("No\n");
}
}
} }
return ;
}
ACdream 1028 Path的更多相关文章
- [ACdream]瑶瑶带你玩激光坦克
题目链接:http://acdream.info/contest?cid=1269#problem-B Problem Description 有一款名为激光坦克的游戏,游戏规则是用一个坦克发出激光来 ...
- POJ 3126:Prime Path
Prime Path Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit St ...
- 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设计之精妙.不得不说,笔者最近几年的 ...
随机推荐
- 错误: 找不到或无法加载主类 tomcat-juli.jar
错误: 找不到或无法加载主类 tomcat-juli.jar 然后就可以用了.
- C语言之带有返回值的函数
带有返回值的函数 语法: 类型 函数名(参数列表){ 函数体; return 数据; } 例: int getSum(int num1,int num2){ int sum = num1 + num2 ...
- Android- assent和raw的区别
Android- assent和raw的区别 相同点: 里面的文件在编译的时候都不会被编译成二进制文件,都会原封不动的打包. 不同点: 1.存放的路径不一样,一般assets存放在项目的根目录下,而r ...
- 一个简单的使用restc demo
最近不经意间看到饿了么团队开发的restc,接口调试工具(类似postman),其实调试接口都没用过工具,每次都只是运行起项目直接调接口.闲来无事,看到restc,就决定试试,后面觉得挺不错的,就分享 ...
- Object-Widgets-Quick 构造树
Object Tree 当以某个QObject为父类创建一个QObject时, 它会被添加到该父类的children列表中. 析构时, QObjet 会首先检查自己的children, 依次析构, 然 ...
- CFround#380 div2
题目链接:http://codeforces.com/contest/738 A题:SB题. B题:SB题. C题:二分. D题:贪心. E题:乱搞. F题:设f[i][j][k]代表甲先手,左边消去 ...
- CUDA学习ing..
0.引言 本文记载了CUDA的学习过程~刚开始接触GPU相关的东西,包括图形.计算.并行处理模式等,先从概念性的东西入手,然后结合实践开始学习.CUDA感觉没有一种权威性的书籍,开发工具变动也比较快, ...
- android studio 导入外部库文件,以及将项目中module变成library引用依赖
一:导入如百度地图等的外部类. 步骤:1.首先 将androidstudio项目显示切换到 project 状态显示项目 2.然后添加.jar文件,将所有的.jar文件放入libs文件夹内(libs文 ...
- TCP TIME_WAIT详解
转自:http://m.blog.chinaunix.net/uid-20384806-id-1954363.html TIME_WAIT状态 TCP要保证在所有可能的情况下使得所有的数据都能够正确被 ...
- ORACLE 使用sqluldr2和sqlldr进行导入导出
oracle数据导出工具sqluldr2可以将数据以csv.txt等格式导出,适用于大批量数据的导出,导出速度非常快.导出后可以使用oracle loader工具将数据导入. 简介: Sqluldr2 ...