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()选 ... 
随机推荐
- JS中常用的Math方法
			1.min()和max()方法 Math.min()用于确定一组数值中的最小值.Math.max()用于确定一组数值中的最大值. alert(Math.min(2,4,3,6,3,8,0,1,3)); ... 
- puppet(1)-简介
			puppet: 开源的.新一代的集中化的配置管理工具: 目标状态 配置语言 Luke Kanies,puppet labs bootstrap --> configuration --> ... 
- [Vue warn]: Attribute "id" is ignored on component <div> because the component is a fragment instanc
			今天在使用vue框架搭建环境时,遇到这个错误提示: [Vue warn]: Attribute "id" is ignored on component <div> b ... 
- 工具 - 怎么看微信h5的源码?
			这个问题在我看网易的h5案例的时候萌生的.因为想看他的源码,但是手机微信打开肯定看不了. 以下几种看代码的方法:(页面案例用网易大大刷屏的h5<二零一六娱乐圈画卷>,真的是一个值得我等众生 ... 
- csrf 攻击和防御
			CSRF概念:CSRF跨站点请求伪造(Cross—Site Request Forgery),跟XSS攻击一样,存在巨大的危害性,你可以这样来理解: 攻击者盗用了你的身份,以你的名义发送恶 ... 
- memcached解压报错gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now的解决方法
			最近在部署环境,在安装memcached的过程中解压时, 解压命令:tar -zvxf memcached-1.4.34.tar.gz 遇到了一个问题, gzip: stdin: not in gzi ... 
- 用memset设置无穷大无穷小
			memeset是以字节为单位进行赋值的,对字符数组可以直接用. 但对于int数组就不行了. 但设置无穷大来说有个技巧: 如果我们将无穷大设为0x3f3f3f3f,那么奇迹就发生了,0x3f3f3f3f ... 
- 关于Redis 应用 的一些 感悟
			最近在项目中 使用了 Redis技术 
- sublime3 快速生成html头文件
			通过安装emmt插件老师来实现该功能(https://github.com/sergeche/emmet-sublime#readme) 1. 下载好后解压到文件夹: 2. 打开TS3,点击perfe ... 
- java.io.UTFDataFormatException: encoded string too long:
			java.io.UTFDataFormatException: encoded string too long: 259553 bytes 按如下修改可避开此问题. - output.writeUTF ... 
