Xtu read problem training 3 B-Gears

Source: Internet
Author: User
Gearstime limit: 2000 msmemory limit: 65536 kbthis problem will be judged on zju. Original ID: 3789
64-bit integer Io format: % LLD Java class name: Main

Bob hasN(1 ≤N≤ 2*105) gears (numbered from 1N). Each gear can rotate clockwise or counterclockwise. bob thinks that sort ing 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 hasM(1 ≤N≤ 4*105) operations to his gears group:

    • "L u v". Link GearUAnd gearV. 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 wocould shutdown the rotation of his gears group, he won't do that.

    • "D u". Disconnect the gearU. Bob may feel something wrong about the gear. he will put the gear away, and the gear wowould 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 gearsUAndV. It cocould 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 gearU.

 

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

Input

Input will consist of multiple test cases. In each case, the first line consists of two integersNAndM. FollowingMLines, each line consists of one of the operations which are described abve. Please process to the end of input.

Output

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

Sample Input
3 7L 1 2L 2 3Q 1 3Q 2 3D 2Q 1 3Q 2 35 10L 1 2L 2 3L 4 5Q 1 2Q 1 3Q 1 4S 1D 2Q 2 3S 1
Sample output
SameDifferentSameUnknownDifferentSameUnknown3Unknown2
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 ).

Sourcezoj monthly, June 2014 authorfeng, Jingyi: query the set
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #define LL long long13 #define INF 0x3f3f3f3f14 using namespace std;15 const int maxn = 1010000;16 int fa[maxn],dis[maxn],sum[maxn],mp[maxn],t,n, m;17 void init() {18     for(int i = 0; i <= n+m; i++) {19         fa[i] = i;20         mp[i] = i;21         dis[i] = 0;22         sum[i] = 1;23     }24     t = n+1;25 }26 int Find(int x) {27     if(fa[x] != x) {28         int root = Find(fa[x]);29         dis[x] += dis[fa[x]];30         fa[x] = root;31     }32     return fa[x];33 }34 int main() {35     char st[10];36     while(~scanf("%d %d",&n, &m)) {37         init();38         int x, y;39         for(int i = 0; i < m; i++) {40             scanf("%s",st);41             if(st[0] == ‘L‘) {42                 scanf("%d %d",&x, &y);43                 x = mp[x];44                 y = mp[y];45                 int tx = Find(x);46                 int ty = Find(y);47                 if(tx != ty) {48                     sum[tx] += sum[ty];49                     fa[ty] = tx;50                     dis[ty] = dis[x]+dis[y]+1;51                 }52             } else if(st[0] == ‘Q‘) {53                 scanf("%d %d",&x, &y);54                 x = mp[x];55                 y = mp[y];56                 if(Find(x) != Find(y)) puts("Unknown");57                 else {58                     if(abs(dis[x]-dis[y])&1) puts("Different");59                     else puts("Same");60                 }61             } else if(st[0] == ‘D‘) {62                 scanf("%d",&x);63                 int tx = mp[x];64                 tx = Find(tx);65                 sum[tx] -= 1;66                 mp[x] = ++t;67             } else if(st[0] == ‘S‘) {68                 scanf("%d",&x);69                 x = mp[x];70                 int tx = Find(x);71                 printf("%d\n",sum[tx]);72             }73         }74     }75     return 0;76 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.