hdu 4674 Trip Advisor

昨天比赛的所有价值都展现在这道题上了……


给一个无向图,保证每个图都只在一个简单环上,每次询问是否存在一条从x到y且要经过z的路径,每个点只能走一次。

因为图的特殊性质,可以把每个环缩点,这样原图就可以变成森林,在森林里用LCA询问从x到y的路径上是否经过z,好像就搞定了?……


因为每个点只能走一次,所以有很多种情况是No……



诸如这样的……


所以对于每个环还要记录是从哪个节点出发往外连边的,只要把情况考虑全就没问题了

代码死长死长的,还好跑的挺快。


#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <queue>
#include <stack>
#include <utility>
#include <set>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define N 100025
#define M 300005
using namespace std;
int n , DFN[N] , low[N] , ncnt , bcnt , bel[N];
int m , pre[N] , mcnt;
struct edge
{
  int x , f , next;
}e[M];
pair<int , int> a[M];
int L[N] , con[N] , core[N] , bp[N];
int f[N][20];

int LCA(int x , int y , bool hehe = 0)
{
  int log , i;
  if (hehe)
  {
    for (log = 19 ; log >= 0 ; -- log)
      if (y & (1 << log))
        x = f[x][log];
    return x;
  }
  if (L[x] < L[y]) swap(x , y); // x is deeper than y
  for (log = 1 ; (1 << log) <= L[x] ; ++ log); -- log;
  for (i = log ; i >= 0 ; -- i)
    if (L[x] - (1 << i) >= L[y])
      x = f[x][i];
  if (x == y) return y; // y is ancstor of x
  for (i = log ; i >= 0 ; -- i)
    if (f[x][i] != -1 && f[x][i] != f[y][i])
    {
      x = f[x][i];
      y = f[y][i];
    }
  return f[x][0];
}

void dfs2(int x , int fa)
{
  int i , y;
  DFN[x] = 1 , f[x][0] = fa , low[x] = bcnt , L[x] = L[fa] + 1;
  for (i = pre[x] ; ~i ; i = e[i].next)
  {
    y = e[i].x;
    if (!DFN[y])
      dfs2(y , x);
  }
}

void dfs(int x , int fa)
{
  low[x] = DFN[x] = ++ ncnt;
  int i , y ;
  for (i = pre[x] ; ~i ; i = e[i].next)
  {
    y = e[i].x;
    if (!DFN[y])
    {
      dfs(y , x);
      low[x] = min(low[x] , low[y]);
      if (low[y] > DFN[x]) e[i].f = e[i ^ 1].f = 1;
    }
    else if (DFN[y] < DFN[x] && y != fa)
      low[x] = min(DFN[y] , low[x]);
  }
}

void fff(int x)
{
  bel[x] = ncnt;
  for (int i = pre[x] ; ~i ; i = e[i].next) if (!e[i].f)
  {
    int y = e[i].x;
    if (!bel[y])
      fff(y);
  }
}

void work()
{
  int i , x , j , y , z; char c; ncnt = bcnt = mcnt = 0;
  memset(pre , -1 , sizeof(pre));
  for (i = 1 ; i <= m ; ++ i)
  {
    scanf("%d%d",&x , &y);
    a[i] = mp(x , y);
    e[mcnt].x = x , e[mcnt].next = pre[y] , e[mcnt].f = 0 , pre[y] = mcnt ++;
    e[mcnt].x = y , e[mcnt].next = pre[x] , e[mcnt].f = 0 , pre[x] = mcnt ++;
  }
  memset(bel , 0 , sizeof(bel));
  memset(DFN , 0 , sizeof(DFN));
  memset(low , 0 , sizeof(low));
  for (i = 1 ; i <= n ;i ++)
    if (!DFN[i])
      dfs(i , -1);
  ncnt = 0;
  for (i = 1 ; i <= n ; ++ i)
    if (!bel[i])
    {
      ++ ncnt;
      fff(i);
    }
  mcnt = 0 , memset(pre , -1 , sizeof(pre));
  for (i = 1 ; i <= m ; ++ i)
  {
    x = bel[a[i].fi] , y = bel[a[i].se];
    if (x == y) continue;
    e[mcnt].x = x , e[mcnt].next = pre[y] , pre[y] = mcnt ++;
    e[mcnt].x = y , e[mcnt].next = pre[x] , pre[x] = mcnt ++;
  }
  bcnt = 0;
  memset(low , 0 , sizeof(low));
  memset(DFN , 0 , sizeof(DFN));
  memset(f , 0 , sizeof(f));
  for (i = 1 ; i <= ncnt ;i ++)
    if (!DFN[i])
      ++ bcnt , dfs2(i , 0);
  memset(con , 0 , sizeof(con));
  memset(bp , 0 , sizeof(bp));
  for (i = 1 ; i <= m ; ++ i)
  {
    x = bel[a[i].fi] , y = bel[a[i].se];
    if (x == y) continue;
    if (L[x] < L[y]) swap(x , y) , swap(a[i].fi , a[i].se);
    con[a[i].fi] = y , core[x] = a[i].fi , bp[a[i].fi] = a[i].se;
  }
  //for (i = 1 ; i <= n ; ++ i)
   // printf("%d " , bel[i]); puts("");
  for (j = 1 ; (1 << j) <= ncnt ; ++ j)
    for (i = 1 ; i <= ncnt ; ++ i)
      f[i][j] = f[f[i][j - 1]][j - 1];
  int Q , A , B , C;
  scanf("%d",&Q);
  while (Q --)
  {
    scanf("%d%d%d",&x,&y,&z);
    A = bel[x] , B = bel[y] , C = bel[z];
    //puts("");
   // cout << A <<B << C<<endl;
    if (low[A] != low[B] || low[B] != low[C] || low[C] != low[A])
    {
      puts("No");
      continue;
    }
      if (A == B && B == C)
      {
        if (x != y)
          puts("Yes");
        else if (x == z)
          puts("Yes");
        else
          puts("No");
        continue;
      }
      if (A == B && A != C)
      {
        puts("No");
        continue;
      }
      int D = LCA(A , B);
      if (D == A || D == B)
      {
        if (L[A] < L[B])
          swap(x , y) , swap(A , B);
        if (LCA(A , C) != C || LCA(C , B) != B)
          puts("No");
        else if (C == B)
        {
          i = core[LCA(A , L[A] - L[B] - 1 , 1)];
          if (bp[i] == y && y != z)
            puts("No");
          else puts("Yes");
        }
        else if (C == A)
        {
          if (con[x] && con[x] == f[A][0] && x != z)
            puts("No");
          else puts("Yes");
        }
        else
        {
          i = core[LCA(A , L[A] - L[C] - 1 , 1)];
          if (bp[i] == core[C] && z != core[C])
            puts("No");
          else puts("Yes");
        }
        continue;
      }

      if (LCA(A , C) == C && LCA(C , D) == D)
      {
        if (C == A)
        {
          if (con[x] && con[x] == f[A][0] && x != z)
            puts("No");
          else puts("Yes");
        }
        else if (C == B)
        {
          if (con[y] && con[y] == f[B][0] && y != z)
            puts("No");
          else puts("Yes");
        }
        else
        {
          if (D == C)
          {
            i = core[LCA(A , L[A] - L[C] - 1 , 1)];
            j = core[LCA(B , L[B] - L[C] - 1 , 1)];
            if (bp[i] == bp[j] && z != bp[i])
              puts("No");
            else puts("Yes");
          }
          else
          {
             i = core[LCA(A , L[A] - L[C] - 1 , 1)];
             if (bp[i] == core[C] && z != core[C])
               puts("No");
             else puts("Yes");
          }
        }
        continue;
      }



      swap(x , y) , swap(A , B);
      if (LCA(A , C) == C && LCA(C , D) == D)
      {
        if (C == A)
        {
          if (con[x] && con[x] == f[A][0] && x != z)
            puts("No");
          else puts("Yes");
        }
        else if (C == B)
        {
          if (con[y] && con[y] == f[B][0] && y != z)
            puts("No");
          else puts("Yes");
        }
        else
        {
          if (D == C)
          {
            i = core[LCA(A , L[A] - L[C] - 1 , 1)];
            j = core[LCA(B , L[B] - L[C] - 1 , 1)];
            if (bp[i] == bp[j] && z != bp[i])
              puts("No");
            else puts("Yes");
          }
          else
          {
             i = core[LCA(A , L[A] - L[C] - 1 , 1)];
             if (bp[i] == core[C] && z != core[C])
               puts("No");
             else puts("Yes");
          }
        }
        continue;
      }
      puts("No");
    }
}


int main()
{
  while (~scanf("%d%d",&n,&m))
    work();
  return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值