結果

問題 No.1451 集団登校
ユーザー Example0911Example0911
提出日時 2021-03-31 15:57:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,969 bytes
コンパイル時間 2,643 ms
コンパイル使用メモリ 219,912 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-05-08 19:16:54
合計ジャッジ時間 6,841 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 216 ms
10,496 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 WA -
testcase_11 AC 134 ms
9,472 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 80 ms
6,912 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
	vector<mint>ans(N, 1);
	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) {
			ans[a] /= 2;
			ans[b] /= 2;
		}
		else if (sa < sb) {
			dfs(a);
		}
		else {
			dfs(b);
		}
		if (!uf.issame(a, 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 {
			cout << ans[uf.root(i)] << endl;
		}
	}
	return 0;
}
0