您的位置:首页 > 其它

ZOJ 3789 齿轮操作 带权并查集

2014-12-30 15:17 295 查看
传送门:点击打开链接

Gears

Time Limit: 2 Seconds
Memory Limit: 65536 KB

Bob has N (1 ≤ N ≤ 2*105) gears (numbered from 1 to
N). Each gear can rotate clockwise or counterclockwise. Bob thinks that assembling gears is much more exciting than just playing with a single one. Bob wants to put some gears into some groups. In each gear group, each gear has a specific rotation
respectively, clockwise or counterclockwise, and as we all know, two gears can link together if and only if their rotations are different. At the beginning, each gear itself is a gear group.

Bob has M (1 ≤ N ≤ 4*105) operations to his gears group:

"L u v". Link gear u and gear v. If two gears link together, the gear groups which the two gears come from will also link together, and it makes a new gear group. The two gears will have different rotations. BTW, Bob won't link two
gears with the same rotations together, such as linking (a, b), (b, c), and (a, c). Because it would shutdown the rotation of his gears group, he won't do that.

"D u". Disconnect the gear u. Bob may feel something wrong about the gear. He will put the gear away, and the gear would become a new gear group itself and may be used again later. And the rest of gears in this group won't be separated apart.

"Q u v". Query the rotations between two gears u and v. It could be "Different", the "Same" or "Unknown".

"S u". Query the size of the gears, Bob wants to know how many gears there are in the gear group containing the gear
u.

Since there are so many gears, Bob needs your help.

Input

Input will consist of multiple test cases. In each case, the first line consists of two integers
N and M. Following M lines, each line consists of one of the operations which are described above. Please process to the end of input.

Output

For each query operation, you should output a line consist of the result.

Sample Input

3 7
L 1 2
L 2 3
Q 1 3
Q 2 3
D 2
Q 1 3
Q 2 3
5 10
L 1 2
L 2 3
L 4 5
Q 1 2
Q 1 3
Q 1 4
S 1
D 2
Q 2 3
S 1

Sample Output

Same
Different
Same
Unknown
Different
Same
Unknown
3
Unknown
2

Hint

Link (1, 2), (2, 3), (4, 5), gear 1 and gear 2 have different rotations, and gear 2 and gear 3 have different rotations, so we can know gear 1 and gear 3 have the same rotation, and we didn't link group (1, 2, 3) and group (4, 5), we don't know the situation
about gear 1 and gear 4. Gear 1 is in the group (1, 2, 3), which has 3 gears. After putting gear 2 away, it may have a new rotation, and the group becomes (1, 3).

题意:对一些齿轮进行合并和删除操作,询问齿轮组内齿轮的数量或齿轮间的转动情况。操作有:

1、链接两个齿轮(组)。连接时需要连接的两个齿轮转动方向相反,否则不操作。

2、删除一个齿轮。将这个齿轮拿出齿轮组,齿轮组其他齿轮的转动不变。

3、询问两个齿轮的转动状况

4、询问某齿轮所属齿轮组内齿轮的数量。

思路:合并两个集合,很明显是用并查集,还要查询齿轮间的关系,那就是带权并查集了。数量的话在合并的时候维护即可。至于删除操作,我们可以用给齿轮重新编号的方法。如i齿轮的编号是p[i]。删除的时候我们直接让p[i]的祖先的num减1,然后让p[i]=0(i齿轮未使用的时候p[i]=0)。原来的p[i]是没有动的,在并查集中可以正常地使用。当下一次用到齿轮i的时候,我们让p[i]=++cnt即可。

详细操作见代码

#include<cstdio>
#include<cstring>
#define maxn 400005
int num[maxn],v[maxn],f[maxn];
int p[200005];
int findfa(int x)
{
if(x!=f[x])
{
int y=findfa(f[x]);
v[x]+=v[f[x]];
v[x]&=1;
f[x]=y;
}
return f[x];
}
int cnt;
void check(int a,int b)
{
if(!p[a])
{
p[a]=++cnt;
f[cnt]=cnt;
num[cnt]=1;
v[cnt]=0;
}
if(!p[b])
{
p[b]=++cnt;
f[cnt]=cnt;
num[cnt]=1;
v[cnt]=0;
}
}
void link(int a,int b)
{
check(a,b);
int x=findfa(p[a]);
int y=findfa(p[b]);
if(x==y) return;
f[y]=x;
v[y]=(v[p[a]]+v[p[b]]+1)&1;
num[x]+=num[y];
}
void del(int u)
{
if(!p[u]) return;
{
int x=findfa(p[u]);
num[x]--;
p[u]=0;
}
}
void quer(int a,int b)
{
if(p[a]&&p[b])
{
int x=findfa(p[a]);
int y=findfa(p[b]);
if(x!=y) printf("Unknown\n");
else if(v[p[a]]==v[p[b]]) printf("Same\n");
else printf("Different\n");
}
else printf("Unknown\n");
}
void ques(int u)
{
if(p[u])
{
int x=findfa(p[u]);
printf("%d\n",num[x]);
}
else printf("1\n");
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
memset(p,0,sizeof(p));
cnt=0;
char s[10];
int u,v;
while(m--)
{
scanf("%s",s);
if(s[0]=='L')
{
scanf("%d %d",&u,&v);
link(u,v);
}
else if(s[0]=='D')
{
scanf("%d",&u);
del(u);
}
else if(s[0]=='Q')
{
scanf("%d %d",&u,&v);
quer(u,v);
}
else
{
scanf("%d",&u);
ques(u);
}
}
}
return 0;
}



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