HDU 3594 Cactus 判断一条边是否在多个环内

本文介绍了一种用于判断特定有向图是否为Cactus图的算法,即该图是否为强连通且每条边仅属于一个环。通过实现Tarjan算法并深入理解dfn和low数组的作用来完成这一判别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

 

Cactus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 913    Accepted Submission(s): 426


Problem Description
1. It is a Strongly Connected graph.
2. Each edge of the graph belongs to a circle and only belongs to one circle.
We call this graph as CACTUS.



There is an example as the figure above. The left one is a cactus, but the right one isn’t. Because the edge (0, 1) in the right graph belongs to two circles as (0, 1, 3) and (0, 1, 2, 3).
 


 

Input
The input consists of several test cases. The first line contains an integer T (1<=T<=10), representing the number of test cases.
For each case, the first line contains a integer n (1<=n<=20000), representing the number of points.
The following lines, each line has two numbers a and b, representing a single-way edge (a->b). Each case ends with (0 0).
Notice: The total number of edges does not exceed 50000.
 


 

Output
For each case, output a line contains “YES” or “NO”, representing whether this graph is a cactus or not.

 


 

Sample Input
  
2 4 0 1 1 2 2 0 2 3 3 2 0 0 4 0 1 1 2 2 3 3 0 1 3 0 0
 


 

Sample Output
  
YES NO
 


 

Author
alpc91
 


 

Source
 


 

Recommend
zhengfeng

 

题意是说给你一个有向图,此图如果具备这两个条件:1,它是一个强连通图,2它的每一条边仅属于一个环。就输出YES,否则输出NO。

只要对tarjan算法非常了解才能够理解这么做。这个题需要很好的理解dfn和low两个数组的作用,如果low[u]!=dfn[u],则说明已经存在环,如果我们访问u点时发现low[u]!=dfn[u],则说明有一条边在两个环上了。

#include<stdio.h>
#include<string.h>
#include<stack>
#define M 20007
using namespace std;
int head[M],low[M],dfn[M],vis[M],id[M];
stack<int>s;
int n,cnt,num,count,ok;
struct Edg
{
    int to,next;
}edg[M*5];

void init()
{
    cnt=num=count=ok=0;
    memset(vis,0,sizeof(vis));
    memset(dfn,0,sizeof(id));
    memset(head,-1,sizeof(head));
}

void addedge(int a,int b)
{
    edg[cnt].to=b;
    edg[cnt].next=head[a];
    head[a]=cnt++;
}

void tarjan(int u)
{
    int v;
    low[u]=dfn[u]=++num;
    vis[u]=1;
    s.push(u);
    for(int i=head[u];i!=-1;i=edg[i].next)
    {
        v=edg[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[v],low[u]);
        }
        else if(vis[v])
        {
            low[u]=min(dfn[v],low[u]);
            if(dfn[v]!=low[v])//说明一条边在两个环上
                ok=1;
        }
        if(ok)return;
    }
    if(dfn[u]==low[u])
    {
        count++;
        if(count>1)//如果count大于1就说明存在多个环
            ok=1;
        do
        {
            v=s.top();
            s.pop();
            vis[v]=0;
        }while(s.top()!=u);
    }
}

int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        init();
        int a,b;
        while(scanf("%d%d",&a,&b),a|b)
        {
            a++;b++;
            addedge(a,b);
        }
        for(int i=1;i<=n;i++)
            if(!dfn[i])
                tarjan(i);
        if(ok||count!=1)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}


 

 

 

HDU(Hangzhou Dianzi University)OJ 中经常涉及到几何计算的问题,其中“判断两条线段是否相交”是一个经典的算法问题。以下是关于如何判断两线段是否相交的基本思路及其实现步骤: ### 判断两条线段相交的核心思想 可以利用向量叉积以及端点位置的关系来确定两条线段是否相交。 #### 具体步骤: 1. **定义基本概念** - 假设两条线段分别为 `AB` 和 `CD`。 - 使用二维平面中的坐标表示各顶点:A(x₁,y₁), B(x₂,y₂),C(x₃,y₃) ,D(x₄,y₄)。 2. **叉积的作用** 叉积可以帮助我们了解两点相对于一条直线的位置关系。 对于三个点 P、Q、R ,我们可以用叉乘 `(Q-P)x(R-P)` 来检测 R 是否在 QP 直线的一侧还是另一侧。 如果结果为正数,则表明顺时针;如果负则逆时针;若等于0则共线。 3. **快速排斥实验** 首先做一个矩形包围盒测试——即检查两个线段所在的最小外接矩形是否有重叠区域。如果没有重叠直接判定为不相交。 4. **跨立试验 (Cross-over Test)** 确认每个线段的两端分别位于另一个线段两侧即可认为它们交叉了。这通过上述提到过的叉积运算完成。 5. **特殊情况处理** 包含但不限于如下的几种情况需要单独讨论: - 完全重合的部分; - 存在一个公共端点但并不完全穿过等缘状况。 6. **代码框架示例(Pseudo code):** ```python def cross_product(p1,p2,p3): return (p2[0]-p1[0])*(p3[1]-p1[1])-(p2[1]-p1[1])*(p3[0]-p1[0]) def on_segment(p,q,r): if ((q[0] <= max(p[0], r[0])) and (q[0] >= min(p[0], r[0])) and (q[1] <= max(p[1], r[1])) and (q[1] >= min(p[1], r[1]))): return True; return False; def do_segments_intersect(A,B,C,D): # 计算四个方向的叉积值 o1 = cross_product(A, C, B) o2 = cross_product(A, D, B) o3 = cross_product(C, A, D) o4 = cross_product(C, B, D) # 标准情况判断 if(o1 !=o2 && o3!=o4): return True # 特殊情况逐一验证... ``` 7. 最终结合所有条件得出结论。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值