結果

問題 No.1878 union-find の数え上げ
ユーザー gucci0512gucci0512
提出日時 2022-03-18 21:55:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,595 bytes
コンパイル時間 4,116 ms
コンパイル使用メモリ 224,988 KB
実行使用メモリ 18,636 KB
最終ジャッジ日時 2024-04-14 12:09:30
合計ジャッジ時間 5,115 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
7,368 KB
testcase_08 AC 4 ms
7,372 KB
testcase_09 AC 4 ms
7,496 KB
testcase_10 AC 4 ms
7,492 KB
testcase_11 AC 4 ms
7,496 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,b) for(int i=b-1;i>=0;i--)
#define rbit(i,a) for(int i=0;i<(1<<a);i++)
#define all(x) (x).begin(),(x).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
typedef long long ll;
typedef long double lld;
using namespace std;
using namespace atcoder;
using mint=static_modint<1000000007>;
const ll mod=998244353;
//const ll mod=1e9+7;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const string zton="0123456789";
const string atoz="abcdefghijklmnopqrstuvwxyz";
ll gcd(ll a,ll b){
    ll r;
    r=a%b;
    if(r==0){
        return b;
    }
    else{
        return gcd(b,r);
    }
}
typedef pair<ll,int> P;

vector<int> G[101010];
int N;
ll dp[101010][2];

void dfs(int cu,int pa){
    bool f=false;
    for(int to:G[cu])if(pa!=to){
        dfs(to,cu);
        dp[cu][0]+=dp[to][0]+dp[to][1];
        f=true;
        dp[cu][0]%=mod;
        if(pa!=0&&cu!=0){
            dp[cu][1]+=dp[to][0]+dp[to][1];
            dp[cu][1]%=mod;
        }
    }
    if(cu!=0&&pa!=0){
        dp[cu][1]+=dp[cu][0];
        dp[cu][1]%=mod;
    }
    if(f){
        dp[cu][0]=(dp[cu][0]-1+mod)%mod;
    }
    return ;
}

int main(void){
    cin >> N;
    rep(i,1,N){
        int p;cin >> p;
        p--;
        G[p].push_back(i);
        G[i].push_back(p);
    }
    rep(j,0,2)rep(i,0,101010)dp[i][j]=0;

    rep(i,0,101010)dp[i][0]++;
    dfs(0,-1);
    cout << dp[0][0] << endl;
}
0