P1196 [NOI2002] [銀河英雄加重互いに素セットの伝説]

 

 

 

 

 

 思考

  フリート船、弓からtohead記録電流船の距離を記録する各数の和と、船は明らかに( - tohead [J] tohead [I]) - 任意の二つの船舶の間のABSの数を求めている1。

コード

 1 #include <bits/stdc++.h>
 2 #define dbg(x) cout << #x << "=" << x << endl
 3 #define eps 1e-8
 4 #define pi acos(-1.0)
 5 
 6 using namespace std;
 7 typedef long long LL;
 8 
 9 template<class T>inline void read(T &res)
10 {
11     char c;T flag=1;
12     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
13     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
14 }
15 
16 namespace _buff {
17     const size_t BUFF = 1 << 19;
18     char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
19     char getc() {
20         if (ib == ie) {
21             ib = ibuf;
22             ie = ibuf + fread(ibuf, 1, BUFF, stdin);
23         }
24         return ib == ie ? -1 : *ib++;
25     }
26 }
27 
28 int qread() {
29     using namespace _buff;
30     int ret = 0;
31     bool pos = true;
32     char c = getc();
33     for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
34         assert(~c);
35     }
36     if (c == '-') {
37         pos = false;
38         c = getc();
39     }
40     for (; c >= '0' && c <= '9'; c = getc()) {
41         ret = (ret << 3) + (ret << 1) + (c ^ 48);
42     }
43     return pos ? ret : -ret;
44 }
45 
46 const int maxn = 5e4 + 7;
47 
48 int fa[maxn];
49 int tohead[maxn], sum[maxn];
50 int t;
51 
52 int fid(int x) {
53     if(fa[x] == x) {
54         return x;
55     }
56     int r1 = fa[x], r2 = fid(fa[x]);
57     fa[x] = r2;
58     tohead[x] += tohead[r1];
59     return r2;
60 }
61 
62 void join(int x, int y) {
63     int fx = fid(x), fy = fid(y);
64     fa[fy] = fx;
65     tohead[fy] = sum[fx];
66     sum[fx] += sum[fy];
67     sum[fy] = 0;
68 }
69 
70 int main()
71 {
72     scanf("%d",&t);
73     for ( int i = 1; i <= 30007; ++i ) {
74         fa[i] = i;
75         sum[i] = 1;
76     }
77     while(t--) {
78         char s[5];
79         int x, y;
80         cin >> s;
81         scanf("%d %d",&x, &y);
82         if(s[0] == 'M') {
83             join(x, y);
84         }
85         else {
86             if(fid(x) != fid(y)) {
87                 printf("-1\n");
88                 continue;
89             }
90             else {
91                 printf("%d\n",abs(tohead[x] - tohead[y]) - 1);
92             }
93         }
94     }
95     return 0;
96 }
View Code

 

おすすめ

転載: www.cnblogs.com/orangeko/p/12376627.html