結果
| 問題 |
No.1451 集団登校
|
| コンテスト | |
| ユーザー |
Example0911
|
| 提出日時 | 2021-03-31 15:35:47 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,819 bytes |
| コンパイル時間 | 2,281 ms |
| コンパイル使用メモリ | 210,488 KB |
| 最終ジャッジ日時 | 2025-01-20 01:55:01 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 9 WA * 18 |
ソースコード
#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);
for (int i = 0; i < N; i++) {
ans[i] = 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);
}
uf.merge(a, b);
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 0; i < N; i++) {
if (seen[i])cout << 0 << endl;
else cout << ans[uf.root(i)] << endl;
}
return 0;
}
Example0911