POJ 1182食物链——并查集模板题
2011-10-08 18:02
369 查看
没什么好说的了,用CL2姐姐的话说,题解满天飞,标程也满天飞……
我的代码:
我的代码:
Program eat;//by_Thispoet Const maxn=50005; Var father,data :Array[1..maxn]of Longint; ans,i,k,n,m,p,q :Longint; Function Root(i:Longint):Longint; var t:Longint; begin if father[i]=i then exit(i); t:=father[i]; father[i]:=Root(father[i]); data[i]:=(data[i]+data[t])mod 3; exit(father[i]); end; BEGIN readln(n,m); ans:=0; for i:=1 to n do father[i]:=i; fillchar(data,sizeof(data),0); while m>0 do begin readln(k,p,q); if (p>n)or(q>n) then begin dec(m); inc(ans); continue; end;//pos 1 case k of 1: begin if Root(p)=Root(q) then begin if data[p]<>data[q] then inc(ans); end else begin i:=Root(p); father[i]:=q; data[i]:=(3-data[p])mod 3; end; end; 2: begin if Root(p)=Root(q) then begin i:=(data[p]-data[q]+3)mod 3; if i<>1 then inc(ans); end else begin i:=Root(p); father[i]:=q; data[i]:=(4-data[p])mod 3; end; end; end; dec(m); end; writeln(ans); END.
相关文章推荐
- poj 1182 食物链(并查集)(并查集模板)
- poj 1182 食物链 【带权并查集】
- poj 1182 带权并查集模板
- poj1182 食物链 (并查集)
- POJ 1182 食物链(种类并查集)
- poj 1182 食物链 经典并查集
- 2017省选拔(三)Poj1182 食物链 (并查集)
- 【解题报告】 POJ 1182 食物链 并查集的经典应用+相对位置
- Poj 1182 食物链--带边权的并查集
- 【带权并查集】poj1182 食物链
- poj 1182 食物链(经典并查集)
- POJ 1182 食物链(并查集)
- Hdu 1829 A Bug's Life && Poj 1182 食物链 (并查集偏移量的应用)
- poj1182 食物链(并查集)
- 【POJ 1182】 食物链 【种类并查集】
- POJ 1182 食物链(带权并查集)
- POJ_1182_食物链_[NOI]_(并查集)
- POJ 1182 食物链 带权并查集
- POJ1182 - 食物链(带权并查集)
- POJ 1182 食物链(分层并查集)