結果

問題 No.1451 集団登校
ユーザー Example0911Example0911
提出日時 2021-03-31 15:50:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,909 bytes
コンパイル時間 2,708 ms
コンパイル使用メモリ 217,728 KB
実行使用メモリ 10,904 KB
最終ジャッジ日時 2024-05-08 18:58:51
合計ジャッジ時間 6,693 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 165 ms
9,984 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 WA -
testcase_11 AC 215 ms
8,712 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 163 ms
10,704 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 25 ms
5,676 KB
testcase_18 WA -
testcase_19 AC 129 ms
6,528 KB
testcase_20 AC 170 ms
10,904 KB
testcase_21 AC 15 ms
5,376 KB
testcase_22 AC 25 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 84 ms
7,492 KB
testcase_28 AC 37 ms
5,376 KB
testcase_29 AC 86 ms
7,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define int long long
using namespace std;
using ll = long long;
using P = pair<ll, pair<ll, ll>>;
const ll INF = (1LL << 61);
ll mod = 1000000007;
struct mint {
	ll x; // typedef long long ll;
	mint(ll x = 0) :x((x%mod + mod) % mod) {}
	mint operator-() const { return mint(-x); }
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }
struct UnionFind {
	vector<int>par, vsize, esize;
	UnionFind(int sz_) : par(sz_), vsize(sz_, 1), esize(sz_, 0) {
		for (int i = 0; i < sz_; i++)par[i] = i;
	}
	void init(int sz_) {
		par.resize(sz_);
		vsize.resize(sz_, 1);
		esize.resize(sz_, 0);
		for (int i = 0; i < sz_; i++)par[i] = i;
	}
	int root(int x) {
		if (par[x] == x)return x;
		else return par[x] = root(par[x]);
	}
	bool merge(int x, int y) {
		x = root(x), y = root(y);
		if (x == y) {
			esize[x]++;
			return false;
		}
		if (vsize[x] < vsize[y])swap(x, y);
		vsize[x] += vsize[y];
		esize[x] += esize[y] + 1;
		par[y] = x;
		return true;
	}
	bool issame(int x, int y) {
		return root(x) == root(y);
	}
	int vs(int x) {
		return vsize[root(x)];
	}
	int es(int x) {
		return esize[root(x)];
	}
};
vector<vector<int>>G;
vector<bool>seen;
void dfs(int v) {
	seen[v] = true;
	for (auto nv : G[v]) {
		if (seen[nv])continue;
		dfs(nv);
	}
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N, M; cin >> N >> M;
	G.resize(N);
	seen.assign(N, false);
	UnionFind uf(N);
	
	for (int i = 0; i < M; i++) {
		int a, b; cin >> a >> b; a--; b--;
		a = uf.root(a), b = uf.root(b);
		int sa = uf.vs(a), sb = uf.vs(b);
		if (sa == sb) {
			
		}
		else if (sa < sb) {
			dfs(a);
		}
		else {
			dfs(b);
		}
		G[a].push_back(b);
		G[b].push_back(a);
		uf.merge(a, b);
		
	}
	vector<int>cnt(N);
	for (int i = 0; i < N; i++) {
		if (seen[i]) {
			cnt[uf.root(i)]++;
		}
	}
	for (int i = 0; i < N; i++) {
		int now = uf.vs(i);
		now -= cnt[uf.root(i)];
		if (now == 0 || seen[i])cout << 0 << endl;
		else {
			mint ans = 1;
			ans /= now;
			cout << ans << endl;
		}
	}
	return 0;
}
0