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设计之精妙.不得不说,笔者最近几年的 ...
随机推荐
- Scrapy框架安装配置小结
Windows 平台: 系统是 Win7 Python 2.7.7版本 官网文档:http://doc.scrapy.org/en/latest/intro/install.html 1.安装Pyt ...
- JS 处理十六进制颜色渐变算法-输入颜色,输出渐变rgb数组
html颜色有几种表示方式: 英文单词颜色值:background-color:Blue:十六进制颜色值:background-color:#FFFFFF: RGB颜色值三元数字:backgroun ...
- Node.js学习笔记(一):快速开始
最近接了一个node项目,虽然最后顺利完成了,但是由于第一次实战,整个过程是赶出来的,许多地方一知半解.现在项目结束了,就静下心来系统地学一学,理一理,读书不忘拿笔,既然读书了,当然就要记点东西.一方 ...
- 【CSS学习笔记】整齐的表格
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- hdu1045
#include<iostream> using namespace std; int count = 0, n = 0; //判断该ch[x][y]是否可以放置 bool isOk(ch ...
- linux之LVS简介(转自南非的蚂蚁)
一. LVS简介LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,它的官方站点是www.linuxvirtualserver. ...
- iOS中的布局
1.UIView 有三个比较重要的布局属性: frame , bounds 和 center , CALayer 对应地叫做 frame , bounds 和 position .为了能清楚区分,图层 ...
- HTML form的一些属性(第一版)
p,li { white-space: pre-wrap } HTML表单属性总结(第一版) 基本格式为:<input type="类型" name="名字[唯一, ...
- CodeForces 671A Recycling Bottles
暴力. 每个人找到一个入口,也就是从回收站到这个入口走的路程由人的位置到入口的路程来替代. 因此,只要找两个人分别从哪里入口就可以了.注意:有可能只要一个人走,另一人不走. #pragma comme ...
- mac下 redis安装使用
安装redis:brew install redis 开启redis服务:redis-server /usr/local/etc/redis.conf 重新打开一个命令窗口:redis-cli ,进入 ...