結果

問題 No.2047 Path Factory
ユーザー QCFiumQCFium
提出日時 2022-08-19 23:18:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 3,152 bytes
コンパイル時間 1,865 ms
コンパイル使用メモリ 172,036 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2024-04-23 13:32:50
合計ジャッジ時間 3,581 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 36 ms
7,296 KB
testcase_08 AC 27 ms
6,940 KB
testcase_09 AC 35 ms
7,296 KB
testcase_10 AC 48 ms
8,576 KB
testcase_11 AC 26 ms
6,940 KB
testcase_12 AC 15 ms
6,940 KB
testcase_13 AC 16 ms
6,944 KB
testcase_14 AC 28 ms
6,940 KB
testcase_15 AC 36 ms
7,424 KB
testcase_16 AC 26 ms
7,168 KB
testcase_17 AC 24 ms
7,040 KB
testcase_18 AC 30 ms
8,064 KB
testcase_19 AC 35 ms
9,216 KB
testcase_20 AC 61 ms
11,904 KB
testcase_21 AC 29 ms
7,808 KB
testcase_22 AC 7 ms
6,940 KB
testcase_23 AC 8 ms
6,940 KB
testcase_24 AC 7 ms
6,944 KB
testcase_25 AC 44 ms
20,480 KB
testcase_26 AC 74 ms
17,840 KB
testcase_27 AC 62 ms
9,856 KB
testcase_28 AC 30 ms
9,972 KB
testcase_29 AC 2 ms
6,940 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<998244353> 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);
	}
};


std::vector<std::vector<int> > hen;
std::vector<mint> dp0;
std::vector<mint> dp1;



void dfs(int i, int prev) {
	if (prev != -1) hen[i].erase(std::find(hen[i].begin(), hen[i].end(), prev));
	
	mint dp[3];
	dp[0] = 1;
	int m = hen[i].size();
	for (auto j : hen[i]) {
		dfs(j, i);
		for (int k = 2; k >= 0; k--) {
			if (k < 2) dp[k + 1] += dp[k] * dp1[j];
			dp[k] *= dp0[j];
		}
	}
	dp0[i] = dp[1] + dp[2];
	dp1[i] = dp[0] + dp[1];
}
int main() {
	int n = ri();
	hen.resize(n);
	for (int i = 1; i < n; i++) {
		int x = ri() - 1;
		int y = ri() - 1;
		hen[x].push_back(y);
		hen[y].push_back(x);
	}
	dp0.resize(n);
	dp1.resize(n);
	dfs(0, -1);
	
	std::cout << dp0[0] << std::endl;
	
	return 0;
}
0