HDU 3896 dfs树倍增

Greatest TC

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 509    Accepted Submission(s): 148


Problem Description
TC (Tian Chao) is magical place, as you all know...
The railways and the rail-stations in TC are fragile and always meet with different kinds of problems. In order to reach the destination safely on time, you are asked to develop a system which has two types of main functions as below.
1: A B C D, reporting whether we can get from station A to station B without passing the railway that connects station C and station D.
2: A B C, reporting whether we can get from station A to station B without passing station C.
Please notice that the railways are UNDIRECTED.
 

Input
For each test case, the first line will contain two integers N (2<=N<=100000) and M (1<=M<=500000), namely the number of stations and railways in TC. Then each of the next M lines will have two integers, describing the two stations that a certain railway is connecting. After this, there comes a line containing a single integer Q (Q<=300000), which means the number of queries to make on the system. The next Q lines will be queries. Each query begins with a integer, indicating the type of query, followed by 4 (the first type) or 3 (the second type) integers describing the details of the query as what mentioned above.
The stations are always labeled from 1 to N.
 

Output
For each test case, print "yes" or "no" in separated lines for the queries.
 

Sample Input
  
  
13 15 1 2 2 3 3 5 2 4 4 6 2 6 1 4 1 7 7 8 7 9 7 10 8 11 8 12 9 12 12 13 5 1 5 13 1 2 1 6 2 1 4 1 13 6 7 8 2 13 6 7 2 13 6 8
 

Sample Output
  
  
yes yes yes no yes
 
题意:给定一个图,有两种询问,第一种,从a-b是否经过c-d的路径,从a-b是否经过c点。
根据tarjan可以得到一个dfs序,记录第一次经过这个点的时间戳,已经离开这个点的时间戳,每一个点只访问一次,
记录预处理倍增数组。
对于查询1,假设c在d的下边,讨论a,b与c的关系,假如a,b都是c的子树上的点,或者都不是,那么显然存在,只需要讨论
一个在c的子树上,一个不在的情况,假如a在c的子树上假如a的low值小于等于c的dfn,则显然可以,否则不能。
对于查询2,假如a,b都不在c的子树上,则显然可以。
假如都在c的子树上,那么找a,b在c下方的点pp,qq,假如pp==qq,则显然不需要经过c,如果pp,qq都可以查找到c前面的点,那么a,b可以经过pp,qq
到达c的前面,显然可以绕过c相遇。
剩下的就是a,b中有一个在c的子树上,假如a,那么找a在c下方的那个点pp,假如pp存在,那么看pp是否可以返祖到c的前面,假如可以,那么肯定可以
找到不经过c的路径。
好恶心的题目,折腾了一天,在别人的指导下终于ac了,wa30次,坑爹。
代码:
/* ***********************************************
Author :_rabbit
Created Time :2014/5/2 13:43:24
File Name :12.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=101000;
const int maxm=1001000;
int head[maxn],tol;
int low[maxn],dfn[maxn],indexx;
int fa[maxn][20],dep[maxn],out[maxn];
int vis[maxm];
struct Edge{
    int next,to;
}edge[maxm];
void addedge(int u,int v){
    edge[tol].to=v;
    edge[tol].next=head[u];
    head[u]=tol++;
}
void tarjan(int u,int deep){
    low[u]=dfn[u]=++indexx;
    dep[u]=deep;
    for(int i=head[u];i!=-1;i=edge[i].next){
        if(vis[i])continue;
        vis[i]=vis[i^1]=1;
        int v=edge[i].to;
        if(!dfn[v]){
            tarjan(v,deep+1);
            fa[v][0]=u;
            low[u]=min(low[u],low[v]);
        }
        else low[u]=min(low[u],dfn[v]);
    }
    out[u]=indexx;
}
bool judge1(int a,int b,int c,int d){
    if(dep[c]<dep[d])swap(c,d);
    int ina=0,inb=0;
    if(dfn[a]>=dfn[c]&&out[a]<=out[c])ina=1;
    if(dfn[b]>=dfn[c]&&out[b]<=out[c])inb=1;
    if((ina&&inb)||(!ina&&!inb))return 1;
    else{
        if(low[c]<=dfn[d])return 1;
        return 0;
    }
}
int move(int x,int step){
    if(step<0)return -1;
    for(int i=19;i>=0;i--)
        if((1<<i)&step)x=fa[x][i];
    return x;
}
bool judge2(int a,int b,int c){
    int ina=0,inb=0;
    if(dfn[a]>=dfn[c]&&out[a]<=out[c])ina=1;
    if(dfn[b]>=dfn[c]&&out[b]<=out[c])inb=1;
    int flag=0;
    if(!ina&&!inb)flag=1;
    else if(ina&&!inb){
        int pp=move(a,dep[a]-dep[c]-1);
        if(pp!=-1&&low[pp]<dfn[c])flag=1;
    }
    else if(!ina&&inb){
        int pp=move(b,dep[b]-dep[c]-1);
        if(pp!=-1&&low[pp]<dfn[c])flag=1;
    }
    else{
        int pp=move(a,dep[a]-dep[c]-1);
        int qq=move(b,dep[b]-dep[c]-1);
        if(pp==qq)flag=1;
        else if(low[pp]<dfn[c]&&low[qq]<dfn[c])flag=1;
    }
    return flag;
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     int n,m;
     while(~scanf("%d%d",&n,&m)){
         memset(head,-1,sizeof(head));tol=0;
         while(m--){
             int a,b;
             scanf("%d%d",&a,&b);
             addedge(a,b);
             addedge(b,a);
         }
         memset(dfn,0,sizeof(dfn));indexx=0;
         memset(vis,0,sizeof(vis));
         memset(fa,0,sizeof(fa));
         for(int i=1;i<=n;i++)
             if(!dfn[i]){
                 tarjan(i,0);
             }
         for(int j=1;j<20;j++)
		    for(int i=1;i<=n;i++)
                 fa[i][j]=fa[fa[i][j-1]][j-1];
         int Q;
         scanf("%d",&Q);
         while(Q--){
             int op,a,b,c,d;
             scanf("%d",&op);
             if(op==1){
                 scanf("%d%d%d%d",&a,&b,&c,&d);
                 if(judge1(a,b,c,d))puts("yes");else puts("no");
             }
             else{
                 scanf("%d%d%d",&a,&b,&c);
                 if(judge2(a,b,c))puts("yes");
                 else puts("no");
             }
         }
     }
     return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值