結果
| 問題 | No.2047 Path Factory |
| コンテスト | |
| ユーザー |
C
|
| 提出日時 | 2026-12-05 09:23:40 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 50 ms / 2,000 ms |
| + 961µs | |
| コード長 | 4,552 bytes |
| 記録 | |
| コンパイル時間 | 10,196 ms |
| コンパイル使用メモリ | 418,948 KB |
| 実行使用メモリ | 22,144 KB |
| 最終ジャッジ日時 | 2026-12-05 09:23:55 |
| 合計ジャッジ時間 | 10,768 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC optimize("O3")
#if defined(__x86_64__) || defined(__i386__)
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif
#endif
#include <bits/stdc++.h>
#include <atcoder/all>
#define each(x, v) for (auto&& x : v)
#define each2(x, y, v) for (auto&& [x, y] : v)
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(x) begin(x),end(x)
#define sz(x) (int)(x).size()
using namespace std;
using namespace atcoder;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using i128=__int128;
using vll=vector<ll>;
using vvll=vector<vll>;
using vi=vector<int>;
using vvi=vector<vi>;
using pll=pair<ll,ll>;
using pi=pair<int,int>;
using mint=modint998244353;
// using mint=modint1000000007;
// using mint2=static_modint<2>;
// using mint=LazyMontgomeryModInt<998244353>;
using vm=vector<mint>;
using vvm=vector<vm>;
#line 2 "graph/graph-template.hpp"
template <typename T>
struct edge {
int src, to;
T cost;
edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnweightedGraph = vector<vector<int>>;
// Input of (Unweighted) Graph
UnweightedGraph graph(int N, int M = -1, bool is_directed = false,
bool is_1origin = true) {
UnweightedGraph g(N);
if (M == -1) M = N - 1;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
if (is_1origin) x--, y--;
g[x].push_back(y);
if (!is_directed) g[y].push_back(x);
}
return g;
}
// Input of Weighted Graph
template <typename T>
WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false,
bool is_1origin = true) {
WeightedGraph<T> g(N);
if (M == -1) M = N - 1;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
cin >> c;
if (is_1origin) x--, y--;
g[x].emplace_back(x, y, c);
if (!is_directed) g[y].emplace_back(y, x, c);
}
return g;
}
// Input of Edges
template <typename T>
Edges<T> esgraph([[maybe_unused]] int N, int M, int is_weighted = true,
bool is_1origin = true) {
Edges<T> es;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
if (is_weighted)
cin >> c;
else
c = 1;
if (is_1origin) x--, y--;
es.emplace_back(x, y, c);
}
return es;
}
// Input of Adjacency Matrix
template <typename T>
vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true,
bool is_directed = false, bool is_1origin = true) {
vector<vector<T>> d(N, vector<T>(N, INF));
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
if (is_weighted)
cin >> c;
else
c = 1;
if (is_1origin) x--, y--;
d[x][y] = c;
if (!is_directed) d[y][x] = c;
}
return d;
}
/**
* @brief グラフテンプレート
* @docs docs/graph/graph-template.md
*/
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
/**
* O(N)
*
* 素直で木dpをすればいい
*
* r を根とする部分木を T[r] で表す
*
* f(r) : T[r] の答え
* g(r) : T[r] - r の答え + T[r] かつ r の次数がちょうど 1 のような答え
*
* となれば
*
* f(r) = \sum_{子 u} g(u)\Prod_{子 x\neq u}f(x) + \sum_{子 u\neq v} g(u)*g(v)*\Prod_{子 x\neq u,v}f(x)
* g(r) = \Prod_{子 u} f(u) + \sum_{子 u} g(u)\Prod_{子 x\neq u}f(x)
*/
int N;
cin>>N;
auto G=graph(N);
vm f(N),g(N);
auto dfs=[&](this auto&&self,int r,int p)->void {
if(p!=-1&&sz(G[r])==1){
g[r]=1;
}else{
mint suff=1;
vi zero;
each(u,G[r]){
if(u==p)continue;
self(u,r);
if(f[u]==0)zero.push_back(u);
else suff*=f[u];
}
if(sz(zero)==0){
mint sum=0,pref=1;
each(u,G[r]){
if(u==p)continue;
suff/=f[u];
g[r]+=g[u]*pref*suff;
f[r]+=g[u]*sum*suff;
sum*=f[u];
sum+=g[u]*pref;
pref*=f[u];
}
f[r]+=g[r];
g[r]+=pref;
}else if(sz(zero)==1){
int a=zero[0];
g[r]=g[a]*suff;
each(u,G[r]){
if(u==p||u==a)continue;
f[r]+=g[a]*g[u]*suff/f[u];
}
f[r]+=g[r];
}else if(sz(zero)==2){
int a=zero[0],b=zero[1];
f[r]=g[a]*g[b]*suff;
}
}
};
dfs(0,-1);
cout<<f[0].val()<<'\n';
}
C