結果

問題 No.1451 集団登校
ユーザー QCFiumQCFium
提出日時 2021-03-31 14:37:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 3,714 bytes
コンパイル時間 1,715 ms
コンパイル使用メモリ 175,440 KB
実行使用メモリ 10,956 KB
最終ジャッジ日時 2023-08-21 09:11:37
合計ジャッジ時間 5,273 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 113 ms
9,444 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 133 ms
7,124 KB
testcase_11 AC 128 ms
6,308 KB
testcase_12 AC 119 ms
7,068 KB
testcase_13 AC 150 ms
9,380 KB
testcase_14 AC 141 ms
10,760 KB
testcase_15 AC 84 ms
6,740 KB
testcase_16 AC 130 ms
8,648 KB
testcase_17 AC 27 ms
6,272 KB
testcase_18 AC 54 ms
5,548 KB
testcase_19 AC 82 ms
5,020 KB
testcase_20 AC 146 ms
10,956 KB
testcase_21 AC 17 ms
5,280 KB
testcase_22 AC 25 ms
4,692 KB
testcase_23 AC 6 ms
4,380 KB
testcase_24 AC 128 ms
6,692 KB
testcase_25 AC 78 ms
6,700 KB
testcase_26 AC 129 ms
8,640 KB
testcase_27 AC 83 ms
8,348 KB
testcase_28 AC 36 ms
4,904 KB
testcase_29 AC 77 ms
7,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}


template<int mod>
struct ModInt{
	int x;
	ModInt () : x(0) {}
	ModInt (int64_t x) : x(x >= 0 ? x % mod : (mod - -x % mod) % mod) {}
	ModInt &operator += (const ModInt &p){
		if ((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator -= (const ModInt &p) {
		if ((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator *= (const ModInt &p) {
		x = (int64_t) x * p.x % mod;
		return *this;
	}
	ModInt &operator /= (const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt &operator ^= (int64_t p) {
		ModInt res = 1;
		for (; p; p >>= 1) {
			if (p & 1) res *= *this;
			*this *= *this;
		}
		return *this = res;
	}
	ModInt operator - () const { return ModInt(-x); }
	ModInt operator + (const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator - (const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator * (const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator / (const ModInt &p) const { return ModInt(*this) /= p; }
	ModInt operator ^ (int64_t p) const { return ModInt(*this) ^= p; }
	bool operator == (const ModInt &p) const { return x == p.x; }
	bool operator != (const ModInt &p) const { return x != p.x; }
	explicit operator int() const { return x; }
	ModInt &operator = (const int p) { x = p; return *this;}
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while (b > 0) {
			t = a / b;
			a -= t * b;
			std::swap(a, b);
			u -= t * v;
			std::swap(u, v);
		}
		return ModInt(u);
	}
	friend std::ostream & operator << (std::ostream &stream, const ModInt<mod> &p) {
		return stream << p.x;
	}
	friend std::istream & operator >> (std::istream &stream, ModInt<mod> &a) {
		int64_t x;
		stream >> x;
		a = ModInt<mod>(x);
		return stream;
	}
};
typedef ModInt<1000000007> mint;

template<int mod> struct MComb {
	using mint = ModInt<mod>;
	std::vector<mint> fact;
	std::vector<mint> inv;
	MComb (int n) { // O(n + log(mod))
		fact = std::vector<mint>(n + 1, 1);
		for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * mint(i);
		inv.resize(n + 1);
		inv[n] = fact[n] ^ (mod - 2);
		for (int i = n; i--; ) inv[i] = inv[i + 1] * mint(i + 1);
	}
	mint ncr(int n, int r) {
		return fact[n] * inv[r] * inv[n - r];
	}
	mint npr(int n, int r) {
		return fact[n] * inv[n - r];
	}
	mint nhr(int n, int r) {
		assert(n + r - 1 < (int) fact.size());
		return ncr(n + r - 1, r);
	}
};

struct UnionFind {
	std::vector<int> data;
	UnionFind (int size, int first) : data(size, -2) {
		for (int i = first; i < size; i++) data[i] = -1;
	}
	bool unite(int x, int y) { // x : par
		x = root(x);
		y = root(y);
		if (x != y) {
			data[x] += data[y] + 1;
			data[y] = x;
		}
		return x != y;
	}
	bool same(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)] - 1; }
};


int main() {
	int n = ri();
	int m = ri();
	UnionFind uni(n + m, n);
	int head = n;
	std::vector<mint> prob(n + m, 1);
	std::vector<std::vector<int> > children(n + m);
	for (int i = 0; i < m; i++) {
		int a = ri() - 1;
		int b = ri() - 1;
		if (uni.same(a, b)) continue;
		int sza = uni.size(a);
		int szb = uni.size(b);
		if (sza == szb) prob[uni.root(a)] /= 2, prob[uni.root(b)] /= 2;
		else if (sza > szb) prob[uni.root(b)] = 0;
		else prob[uni.root(a)] = 0;
		children[head].push_back(uni.root(a));
		children[head].push_back(uni.root(b));
		uni.unite(head, a);
		uni.unite(head, b);
		head++;
	}
	for (int i = head; i--; ) {
		for (auto j : children[i]) {
			prob[j] *= prob[i];
		}
	}
	for (int i = 0; i < n; i++) {
		std::cout << prob[i] << std::endl;
	}
	
	return 0;
}
0