hdu4916 Count on the path
调了好久。。。。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#define re(i,l,r) for(int i=(l);i<=(r);i++)
#define rre(i,r,l) for(int i=(r);i>=(l);i--)
using namespace std;
const int xxx=;
int n,m,head[xxx],Next[xxx<<],zhi[xxx<<],fa[xxx],ed;
int g[xxx][],f[][],dui[xxx],F[xxx],dp[xxx],xiao[];
void add(int a,int b)
{
Next[++ed]=head[a],head[a]=ed,zhi[ed]=b;
Next[++ed]=head[b],head[b]=ed,zhi[ed]=a;
}
const int L=1e7;
char in[L],*In,*End;
char getc(){
if(In==End){
End=in+fread(in,,L,stdin);
if(in==End) return ;
In=in;
}
return *In++;
}
int inin(int &x){
static char c;
for(;c=getc(),!isdigit(c) && c;);
if(!c) return ;
for(x=;isdigit(c);c=getc()) (x*=)+=c-'';
return ;
}
void dfs(int x)
{
g[x][]=;g[x][]=;
for(int i=head[x];i;i=Next[i])if(zhi[i]!=fa[x])
{
fa[zhi[i]]=x;
dfs(zhi[i]);
int temp=min(g[zhi[i]][],zhi[i]);
if(temp<=g[x][])
g[x][]=g[x][],
g[x][]=temp;
else g[x][]=min(g[x][],temp);
// else g[x][1]=min(g[x][1],g[zhi[i]][0]);
}
}
void dfsxxx(int x,int t)
{
dui[x]=t;
for(int i=head[x];i;i=Next[i])if(zhi[i]!=fa[x])
dfsxxx(zhi[i],t);
}
void dfs2(int x)
{
if(x>&&fa[x]!=)
if(g[fa[x]][]==min(g[x][],x))
F[x]=min(F[fa[x]],g[fa[x]][]);
else F[x]=min(F[fa[x]],g[fa[x]][]);else ;
if(fa[x]==)F[x]=;
dp[x]=min(F[x],g[x][]);
for(int i=head[x];i;i=Next[i])if(zhi[i]!=fa[x])
dfs2(zhi[i]);
}
//void dfs3(int x)
//{
// for(int i=head[x];i;i=Next[i])if(zhi[i]!=fa[x])
// dfs3(zhi[i]);
//}
int hhh(int x)
{
x=dui[x];
if(min(x,g[x][])==xiao[])return ;
else if(min(x,g[x][])==xiao[])return ;
else if(min(x,g[x][])==xiao[])return ;
else return ;
}
int pre=;
int main()
{
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
while(inin(n))
{
inin(m);
pre=;
memset(head,,sizeof(head));ed=;
re(i,,n)
{
int a,b;
// scanf("%d%d",&a,&b);
inin(a),inin(b);
add(a,b);
}
dfs();
xiao[]=xiao[]=xiao[]=n+;
for(int i=head[];i;i=Next[i])
{
dfsxxx(zhi[i],zhi[i]);
int temp=min(g[zhi[i]][],zhi[i]);
if(xiao[]>temp)
xiao[]=xiao[],
xiao[]=xiao[],
xiao[]=temp;
else if(xiao[]>temp)
xiao[]=xiao[],
xiao[]=temp;
else if(xiao[]>temp)
xiao[]=temp;
}
f[][]=xiao[];
f[][]=f[][]=xiao[],f[][]=f[][]=xiao[],f[][]=f[][]=xiao[];
f[][]=f[][]=xiao[],f[][]=f[][]=xiao[],f[][]=f[][]=xiao[];
dfs2();
// dfs3(1);
dui[]=-;
re(i,,m)
{
int a,b;
// scanf("%d%d",&a,&b);
inin(a),inin(b);
a=a^pre,b=b^pre;
if(a==)a++;if(b==)b++;if(a>n)a=a%n+;if(b>n)b=b%n+;
// printf("%d %d %d\n",a,b,pre);
if(dui[a]==dui[b]&&dui[a]!=-){pre=;}
else if(a==)
if(b==){pre=xiao[];}
else
{
int now=hhh(b);
re(i,,)if(i!=now){pre=min(dp[b],xiao[i]);break;}
}
else if(b==)
{
int now=hhh(a);
re(i,,)if(i!=now){pre=min(dp[a],xiao[i]);break;}
}
else {pre=min(min(dp[a],dp[b]),f[hhh(a)][hhh(b)]);}
printf("%d\n",pre);
}
}
return ;
}
hdu4916 Count on the path的更多相关文章
- HDU4916 Count on the path(树dp??)
这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...
- Count on the path
Count on the path Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 4916 Count on the path
意甲冠军: 考虑到一棵树,m询价 不要求回答每一次询价u和v通过在两个节点形成的最低等级点路径 思路: 一開始以为是LCA- 只是T了好几次- 后来发现不用LCA也可做 考虑每一个询问u和v ...
- [LeetCode] Longest Univalue Path 最长相同值路径
Given a binary tree, find the length of the longest path where each node in the path has the same va ...
- [Swift]LeetCode71. 简化路径 | Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- php 生成word的三种方式
原文地址 http://www.jb51.net/article/97253.htm 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像只要是标题带PHP的貌似点击 ...
- WPF 自定义雷达图
自定义雷达图表如下: Git下载地址:https://github.com/Kybs0/RadarChartControl 1.创建UserControl,名为“RadarChartControl” ...
- WPF Tookit Chart
如何使用Chart 实例: Binding数据源中是一个KeyValuePair对象.可以是Dictionary. <charting:Chart x:Name="chtSumma ...
- Proj.4 API 中文参考
ProjAPI https://github.com/OSGeo/proj.4/wiki/ProjAPI Tom Kralidis在2015年5月27日编辑此页·修订4 简介 执行pj_init()选 ...
随机推荐
- Android实现夜间模式
如今非常多App都有夜间模式,特别是阅读类的App.夜间模式如今已经是阅读类App的标配了,其实,日间模式与夜间模式就是给App定义并应用两套不同颜色的主题,用户能够自己主动或者手动的开启,今天用An ...
- SpringBoot------异步任务的使用
步骤,如图所示: 1.添加异步任务业务类 package top.ytheng.demo.task; import java.util.concurrent.Future; import org.sp ...
- HTML中include file的用法
语法 <!-- #include PathType = "FileName" --> 参数 PathType 路径类型 路径可为以下某种类型: 文件 该文件名是带有 ...
- Windows下安装配置Yaf框架的方法及创建典型合理的Demo目录结构
Yaf是一个C语言编写的PHP框架,由鸟哥Laruence开发的高性能框架: Yaf官方文档:http://www.laruence.com/manual/index.html 第一步:安装PHP扩展 ...
- java开学考试有感以及源码
一.感想 Java开学测试有感 九月二十号,王老师给我们上的第一节java课,测试. 说实话,不能说是十分有自信,但还好,直到看见了开学测试的题目,之前因为已经做过了王老师发的16级的题目,所以当时还 ...
- oracle xe 创建表空间
使用OracleXE控制台,我们会发现一个奇怪的现象:没有提供创建表空间.增加数据文件等功能.这似乎和我们使用Oracle标准版控制台不一样. 其实在OracleXE中可以创建新的表空间,不过一般情况 ...
- 写jquery插件(转载)
如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...
- common lisp的几个基本概念
S-表达式 quote nil 与 () cons car cdr 真假 predicate 谓词与 t 与 nil null 函数 与 not 函数 if then else and 与 or de ...
- docker的安装教程
一.Docker服务安装 Docker 支持以下的 Ubuntu 版本: Ubuntu Precise 12.04 (LTS) Ubuntu Trusty 14.04 (LTS) Ubuntu Wil ...
- nginx配置https访问
一.准备 环境:centos6.8 nginx:1.13.6 二.开始 首先安装依赖包: yum install -y gcc gcc-c++ autoconf automake make ...