結果

問題 No.763 Noelちゃんと木遊び
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-03 03:50:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 3,524 bytes
コンパイル時間 1,172 ms
コンパイル使用メモリ 115,680 KB
実行使用メモリ 16,596 KB
最終ジャッジ日時 2023-10-03 03:50:36
合計ジャッジ時間 3,924 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
16,596 KB
testcase_01 AC 16 ms
7,780 KB
testcase_02 AC 35 ms
9,556 KB
testcase_03 AC 26 ms
8,852 KB
testcase_04 AC 17 ms
7,964 KB
testcase_05 AC 24 ms
8,496 KB
testcase_06 AC 49 ms
10,184 KB
testcase_07 AC 40 ms
10,064 KB
testcase_08 AC 24 ms
8,640 KB
testcase_09 AC 18 ms
7,888 KB
testcase_10 AC 8 ms
7,016 KB
testcase_11 AC 48 ms
10,576 KB
testcase_12 AC 41 ms
9,840 KB
testcase_13 AC 40 ms
9,776 KB
testcase_14 AC 33 ms
9,432 KB
testcase_15 AC 25 ms
8,528 KB
testcase_16 AC 6 ms
6,800 KB
testcase_17 AC 23 ms
8,512 KB
testcase_18 AC 44 ms
10,224 KB
testcase_19 AC 43 ms
9,868 KB
testcase_20 AC 39 ms
9,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<random>
using namespace std;
template<long long MOD>struct Modint
{
    private:
    unsigned int value;

    public:
    constexpr Modint(const long long x = 0) noexcept
    {
	long long y = x;
	if(y < 0 || y >= MOD)
	{
		y %= MOD;
		if(y < 0) y += MOD;
	}
        value = (unsigned int)y;
    }

    constexpr unsigned int &val() noexcept {return value;}

    constexpr Modint operator+(const Modint &other) noexcept
    {
        Modint tmp = *this;
        return tmp += other;
    }

    constexpr Modint operator-(const Modint &other) noexcept
    {
        Modint tmp = *this;
        return tmp -= other;
    }

    constexpr Modint operator*(const Modint &other) noexcept
    {
        Modint tmp = *this;
        return tmp *= other;
    }

    constexpr Modint operator/(const Modint &other) noexcept
    {
        Modint tmp = *this;
        return tmp /= other;
    }

    constexpr Modint &operator+=(const Modint &other) noexcept
    {
        value += other.value;
        if(value >= MOD) value -= MOD;

        return *this;
    }

    constexpr Modint &operator-=(const Modint &other) noexcept
    {
        unsigned long long  x = value;
        if(x < other.value) x += MOD;
        x -= other.value;

        value = (unsigned int) x;

        return *this;
    }

    constexpr Modint &operator*=(const Modint &other) noexcept
    {
        unsigned long long x = value;
        x *= other.value;
        value = (unsigned int)(x%MOD);

        return *this;
    }

    constexpr Modint &operator/=(const Modint &other) noexcept
    {
        return *this = *this * other.inverse();
    }

    constexpr Modint power(long long ex) const noexcept
    {
        assert(ex >= 0);
        Modint p = *this,res = 1;

        while(ex)
        {
            if(ex & 1) res *= p;
            p *= p;
            ex >>= 1;
        }

        return res;
    }

    constexpr Modint inverse() const noexcept
    {
        assert(value);
        long long a = value,b = MOD,x = 1,y = 0;

        while(b)
        {
            long long q = a/b;

            a -= q*b; swap(a,b);
            x -= q*y; swap(x,y);
        }

        return Modint(x);
    }

    constexpr Modint &operator++(int) noexcept {return *this += 1;}

    constexpr Modint &operator--(int) noexcept {return *this -= 1;}

    constexpr bool operator==(const Modint &other){return value == other.value;}

    constexpr bool operator!=(const Modint &other){return value != other.value;}

    template<long long mod> friend ostream &operator<<(ostream &os,const Modint<mod> &x);
};

template<long long mod> ostream &operator<<(ostream &os,const Modint<mod> &x){os << x.value; return os;}

//using mint = Modint<998244353>;
//using mint = Modint<1000000007>;
int N;
vector<int> G[1 << 17];
int dp[1 << 17][2];

void dfs(int u,int p)
{
	dp[u][0] = 0,dp[u][1] = 1;
	for(int v:G[u]) if(v != p)
	{
		dfs(v,u);
		dp[u][0] += max(dp[v][0],dp[v][1]);
		dp[u][1] += max(dp[v][0],dp[v][1]-1);
	}
}
void solve()
{
	cin >> N;
	for(int i = 1;i < N;i++)
	{
		int u,v;
		cin >> u >> v;
		u--; v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}

	dfs(0,-1);

	cout << max(dp[0][0],dp[0][1]) << "\n";
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T = 1;
	//cin >> T;
	while(T--) solve();
}
0