日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ZOJ 3789 Gears

發布時間:2024/1/8 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ZOJ 3789 Gears 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


并查集,

刪除節點操作,可以用新建節點代替

維護每個點到跟節點的距離


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).


Author:? FENG, Jingyi
Source:? ZOJ Monthly, June 2014



#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn=600600;int Sz[maxn],dist[maxn],fa[maxn],now[maxn],next; int n,m;void init(int n,int m) {memset(dist,0,sizeof(dist));next=n+1;for(int i=1;i<=n+m;i++){Sz[i]=1;fa[i]=i;if(i<=n) now[i]=i;} }int Find(int x) {if(fa[x]==x) return x;int temp=Find(fa[x]);dist[x]+=dist[fa[x]];///到gen節點的距離return fa[x]=temp; }void Union(int x,int y) {int X=Find(x),Y=Find(y);if(X==Y) return ;fa[X]=Y;Sz[Y]+=Sz[X];dist[X]=dist[Y]+1;dist[x]=dist[y]+1; }void debug() {for(int i=1;i<=5;i++){Find(now[i]);cout<<i<<" : "<<now[i]<<" :: "<<dist[now[i]]<<endl;} }int main() {while(scanf("%d%d",&n,&m)!=EOF){init(n,m);char op[5];int a,b;while(m--){scanf("%s",op);if(op[0]=='L'){scanf("%d%d",&a,&b);int na=now[a],nb=now[b];Union(na,nb);}else if(op[0]=='D'){scanf("%d",&a);int na=now[a];int A=Find(na);Sz[A]--;Sz[na]=0;now[a]=next++;}else if(op[0]=='S'){scanf("%d",&a);int na=now[a];int A=Find(na);printf("%d\n",Sz[A]);}else if(op[0]=='Q'){scanf("%d%d",&a,&b);int na=now[a],nb=now[b];int A=Find(na),B=Find(nb);if(A!=B) puts("Unknown");else{if ((dist[na]-dist[nb])&1) puts("Different");else puts("Same");}}/// debug();}}return 0; }


總結

以上是生活随笔為你收集整理的ZOJ 3789 Gears的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。