为什么这样写代码输入会有问题
查看原帖
为什么这样写代码输入会有问题
411963
uFTvL9楼主2022/4/16 18:39
//#pragma GCC optimize(3, "Ofast", "inline")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;


inline void work(void);


class _fast_IO {
	public:
	    virtual inline int read() {
			int x = 0, f = 1;
			char s = getchar();
			while (s < '0' || s > '9') {
				if (s == '-') f = - f;
				s = getchar();
			}
			while (s >= '0' && s <= '9') {
				x = (x << 3) + (x << 1) + s - '0';
				s = getchar();
			}
			return x * f;
		}
		virtual inline void write_(int x) {
			if (x >= 10)
		   		write_(x / 10);
			putchar(x % 10 + 48);
		}
}_fast_IO;



class union_find_set {
    private:
        static const int unionFindSetSize = 30005;
        
        int f[unionFindSetSize];
        int length[unionFindSetSize];
        int dis[unionFindSetSize];
        
        virtual inline int find(int x) {
            return (f[x] == x)? x : f[x] = find(f[x]);
        }
    public:
        virtual inline void __init__(int n) {
        	for(int i = 1; i <= n; ++i) {
        		f[i] = i;
        		length[i] = 1;
        	}
        	memset(dis, -1, sizeof(dis));
		}
        virtual inline void merge(int x, int y) {
        	x = find(x), y = find(y);
            f[x] = y;
            dis[x] = length[y];
            length[y] += length[x];
        }
        virtual inline bool judge(int x, int y) {
            return find(x) == find(y);
        }
        virtual inline int requestDis(int x, int y) {
        	return abs(dis[x] - dis[y]) - 1;
		}
}union_find_set;


signed main(void) {
	ios::sync_with_stdio(false);
	std::cin.tie(0);
//    freopen(".in", "r", stdin);
//    freopen(".out", "w", stdout);
	work();
	return 0;
}

const int n = 30000;
inline void work(void) {
	int T;
	T = _fast_IO.read();
	while(T--) {
		char instruct = _fast_IO.read();
		int i = _fast_IO.read();
		int j = _fast_IO.read();
		union_find_set.__init__(n);
		
		if (instruct == 'M') {
			union_find_set.merge(i, j);
		}
		if (instruct == 'C') {
			if (!union_find_set.judge(i, j)) {
				putchar('-');
				putchar('1');
				putchar('\n');
				continue;
			}
			else {
				int dist;
				dist = union_find_set.requestDis(i, j);
				_fast_IO.write_(dist);
				putchar('\n');
			}
		}
	}
}

2022/4/16 18:39
加载中...