HDU 3926 Hand in Hand(判断同构)

本文介绍了一种通过比较两个图的结构来判断它们是否同构的方法。具体实现了标记环或链,并按点数目排序,若点数相同则环排前。通过逐个比较节点数和环状态来判断两图是否完全相同。

Hand in Hand

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 122768/62768 K (Java/Others)
Total Submission(s): 1880    Accepted Submission(s): 635


Problem Description
In order to get rid of Conan, Kaitou KID disguises himself as a teacher in the kindergarten. He knows kids love games and works out a new game called "hand in hand".

Initially kids run on the playground randomly. When Kid says "stop", kids catch others' hands immediately. One hand can catch any other hand randomly. It's weird to have more than two hands get together so one hand grabs at most one other hand. After kids stop moving they form a graph.

Everybody takes a look at the graph and repeat the above steps again to form another graph. Now Kid has a question for his kids: "Are the two graph isomorphism?"
 

Input
The first line contains a single positive integer T( T <= 100 ), indicating the number of datasets.
There are two graphs in each case, for each graph:
first line contains N( 1 <= N <= 10^4 ) and M indicating the number of kids and connections.
the next M lines each have two integers u and v indicating kid u and v are "hand in hand".
You can assume each kid only has two hands.
 

Output
For each test case: output the case number as shown and "YES" if the two graph are isomorphism or "NO" otherwise.
 

Sample Input
  
2 3 2 1 2 2 3 3 2 3 2 2 1 3 3 1 2 2 3 3 1 3 1 1 2
 

Sample Output
  
Case #1: YES Case #2: NO
 

Source

因为每个人小朋友只有两只手,所以每个点最多只有2度。图有可能是环、链,以及环和链构成的复杂图。  

  如何判断两幅图是否相似呢?判断相似是判断两幅图的圈的数量,以及构成圈的点数是否相同。还有判断链的数目和构成链的点数是否相同。

  具体实现:标记环(或者链),按照点的数目排序。如果点数相同,环排在前面。然后逐个判断,如果全部都相同,就是同构。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=10010;
struct node
{
    int cnt;
    bool iscc;
}arr1[N],arr2[N];
int f[N],r[N],iscir[N];
bool cmp(const node &x, const node &y)
{
    if(x.cnt!=y.cnt) return x.cnt<y.cnt;
    if(x.iscc<y.iscc) return 1;
    return 0;
}
int Find(int x)
{
    if(x==f[x]) return x;
    return f[x]=Find(f[x]);
}
void Link(int i,int j)
{
    int a=Find(i),b=Find(j);
    if(a!=b) {f[b]=a;r[a]+=r[b];}
    else iscir[a]=1;
}
void init()
{
    for(int i=0;i<N;i++)
    {
        f[i]=i;iscir[i]=0;r[i]=1;
    }
}
int main()
{
    //freopen("test.txt","r",stdin);
    int ca,k,i,j,x,y,n1,m2,n2,m1,a,b;
    k=1;
    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%d%d",&n1,&m1);
        init();
        while(m1--)
        {
            scanf("%d%d",&x,&y);
            Link(x,y);
        }
        a=0;
        for(i=1;i<=n1;i++)
        {
            if(i==Find(i))
            {
                arr1[a].cnt=r[i];
                arr1[a++].iscc=iscir[i];
            }
        }
        scanf("%d%d",&n2,&m2);
        init();
        while(m2--)
        {
            scanf("%d%d",&x,&y);
            Link(x,y);
        }
        b=0;
        for(i=1;i<=n1;i++)
        {
            if(i==Find(i))
            {
                arr2[b].cnt=r[i];
                arr2[b++].iscc=iscir[i];
            }
        }
        printf("Case #%d: ",k++);
        if(n2!=n1||m2!=m1)
        {
            printf("NO\n");
            continue;
        }
        if(a==b)
        {
            sort(arr1,arr1+a,cmp);
            sort(arr2,arr2+a,cmp);
            for(i=0;i<a;i++)
            {
                if(arr1[i].cnt!=arr2[i].cnt) break;
                if(arr1[i].iscc!=arr2[i].iscc) break;
            }
            if(i==a) printf("YES\n");
            else printf("NO\n");
        }
        else printf("NO\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、付费专栏及课程。

余额充值