Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:在《挑战程序设计竞赛》中看到了一种新的解法,于是理解后写下来,欢迎各位也把自己的理解感悟分享一下

Posted by 201501060326 at 2016-02-21 21:17:04 on Problem 1182
In Reply To:Re:在《挑战程序设计竞赛》中看到了一种新的解法,于是理解后写下来,欢迎各位也把自己的理解感悟分享一下 Posted by:201501060326 at 2016-02-21 20:58:57
#include <stdio.h>
int p[150000];
int find(int x)//find函数功能为找到与x相关联的第一个输入项,比如可能有很多个输入是同
{             //类或有间接捕食关系,可以找到所有关联项的第一个,把所有输入都逻辑串联
    if(p[x]==x)
        return x;
    else
        return p[x]=find(p[x]);
}
int same(int x,int y)//判断x,y是否为同类
{
    if(find(x)==find(y))
        return 1;
    else
        return 0;
}
int unite(int x,int y)//与find函数密切关联,将x,y逻辑关系串联
{
    int u,v;
    u=find(x);
    v=find(y);
    if(u!=v) p[u]=v;//将x,y串联的关键
}
int main()
{
    int i,n,k,d,x,y,err=0;
    scanf("%d%d",&n,&k);
    for(i=1; i<=3*n; i++) p[i]=i;//x表示x为A种动物,x+n表示x为B种动物,x+2*n表示
    while(k--)                  //x为C种动物
    {
        scanf("%d%d%d",&d,&x,&y);
        if(x>n||y>n) err++;
        else if(d==1)
        {
            if(same(x,y+n)==1||same(x,y+2*n)==1) err++;//互为同类,不可捕食
            else
            {
                unite(x,y);
                unite(x+n,y+n);
                unite(x+2*n,y+2*n);//合并所有同类关系的可能
            }
        }
        else
        {
            if(same(x,y)==1||same(x,y+2*n)==1) err++;//捕食关系不可为同类或相反
            else
            {
                unite(x,y+n);
                unite(x+n,y+2*n);
                unite(x+2*n,y);//合并所有捕食关系的可能
            }
        }
    }
    printf("%d\n",err);
    return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator