您的位置:首页 > 其它

【并查集】【向量偏移】[NOI 2001]食物链 eat WikiOI 1074

2013-05-09 20:10 411 查看

WikiOI 1074 食物链

题目描述Description

动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形。A吃B,B吃C,C吃A。   

现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。   

有人用两种说法对这N个动物所构成的食物链关系进行描述:   

第一种说法是“1 X Y”,表示X和Y是同类。   

第二种说法是“2 X Y”,表示X吃Y。   

此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。   

1) 当前的话与前面的某些真的话冲突,就是假话;   

2) 当前的话中X或Y比N大,就是假话;   

3) 当前的话表示X吃X,就是假话。   

你的任务是根据给定的N(1<=N<=50,000)和K句话(0<=K<=100,000),输出假话的总数。

输入描述Input Description

第一行是两个整数N和K,以一个空格分隔。   

以下K行每行是三个正整数D,X,Y,两数之间用一个空格隔开,其中 D 表示说法的种类。   

若D=1,则表示X和Y是同类。   

若D=2,则表示X吃Y。

输出描述Output Description

只有一个整数,表示假话的数目。

样例输入Sample Input

100 7

1 101 1

2 1 2

2 2 3

2 3 3

1 1 3

2 3 1

1 5 5

样例输出Sample Output

3

数据范围及提示Data Size & Hint

输入文件  

 对7句话的分析 100 7

1 101 1  假话

2 1 2    真话

2 2 3    真话

2 3 3    假话

1 1 3    假话

2 3 1    真话

 1 5 5    真话

以前这一题一直不知道怎么做,这几天在刷WikiOI天梯,遇到这一题了,就把它解决了

现在才知道,其实这用了向量偏移的方法(不要问我是什么,我也不懂。。。)

因为题目中只给出了三种动物A B C ,所以我们可以给N个点变成3*N个点来分别表示这三种动物(可以理解成枚举每个点作为A或B或C的情况)

即 x 变成三个点:x , x+n , x+n+n

题目中给出了关系,A吃B,B吃C,C吃A

所以当题目给出 x 与 y 同类的时候,如果 x 为 A ,那么 y 也必须为 A ,x 为 B ,y 也必须为 B , ……

如果给出 x 吃 y ,如果 x 为 A ,那么 y 就只能是 B , ……

这样就可以维护出所有的关系

下面的代码就是在遇到所有情况后进行所有可能的判断,AC

/*http://blog.csdn.net/jiangzh7
By Jiangzh*/
#include<cstdio>
//A:x , B:x+n , C:x+n+n
int n,k;
int f[50000*3+10];

int getroot(int x) { return f[x]==x ? x : f[x]=getroot(f[x]); }
inline void merge(int x,int y) { f[getroot(x)]=getroot(y); }

int main()
{
freopen("1074.in","r",stdin);
freopen("1074.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=1;i<=3*n;i++) f[i]=i;
int res=0;
while(k--)
{
int d,x,y;
scanf("%d%d%d",&d,&x,&y);
if(x>n||y>n) res++;
else if(d==2 && x==y) res++;
else if(d==1)//the same
{//if x is A(B/C), so that y must be A(B/C)
if(getroot(x)==getroot(y+n)) res++;
else if(getroot(x)==getroot(y+n+n)) res++;
else if(getroot(x+n)==getroot(y)) res++;
else if(getroot(x+n)==getroot(y+n+n)) res++;
else if(getroot(x+n+n)==getroot(y)) res++;
else if(getroot(x+n+n)==getroot(y+n)) res++;
else{

merge(x,y);
merge(x+n,y+n);
merge(x+n+n,y+n+n);
}
}
else{//x eat y  //if x is A(B/C) , so that y must be B(C/A)
if(getroot(x)==getroot(y)) res++;
else if(getroot(x)==getroot(y+n+n)) res++;
else if(getroot(x+n)==getroot(y)) res++;
else if(getroot(x+n)==getroot(y+n)) res++;
else if(getroot(x+n+n)==getroot(y+n)) res++;
else if(getroot(x+n+n)==getroot(y+n+n)) res++;
else{
merge(x,y+n);
merge(x+n,y+n+n);
merge(x+n+n,y);
}
}
}
printf("%d\n",res);
return 0;
}


但是上面有很多冗余的判断,比如

if(getroot(x)==getroot(y)) res++;
else if(getroot(x+n)==getroot(y+n)) res++;
else if(getroot(x+n+n)==getroot(y+n+n)) res++;


其实我们只需要判断第一句话就ok了,为什么呢?看上面入队的时候

else{
merge(x,y);
merge(x+n,y+n);
merge(x+n+n,y+n+n);
}


是三个一起入队的,所以只需要判断第一句话,二三句自然也就判断了

所以,我们就可以精简出一份代码

/*http://blog.csdn.net/jiangzh7
By Jiangzh*/
#include<cstdio>

int n,k;
int f[50000*3+10];

int getroot(int x) { return f[x]==x ? x : f[x]=getroot(f[x]); }
inline void merge(int x,int y) { f[getroot(x)]=getroot(y); }

int main()
{
freopen("1074.in","r",stdin);
freopen("1074.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=1;i<=3*n;i++) f[i]=i;
int res=0;
while(k--)
{
int d,x,y;
scanf("%d%d%d",&d,&x,&y);
if(x>n||y>n) res++;
else if(d==2 && x==y) res++;
else if(d==1)//the same
{
if(getroot(x)==getroot(y+n)) res++;//x eat y
else if(getroot(x+n)==getroot(y)) res++;//y eat x
else{ merge(x,y); merge(x+n,y+n); merge(x+n+n,y+n+n); }}
else{//x eat y
if(getroot(x)==getroot(y)) res++;//x y are the same
else if(getroot(y)==getroot(x+n)) res++;//x eat y
else{
merge(x,y+n);
merge(x+n,y+n+n);
merge(x+n+n,y);
}
}
}
printf("%d\n",res);
return 0;
}


上面的两份代码都是AC了的,对比一下效率

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: