結果

問題 No.1103 Directed Length Sum
ユーザー uchiiiiuchiiii
提出日時 2020-07-14 21:34:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 561 ms / 3,000 ms
コード長 2,648 bytes
コンパイル時間 6,595 ms
コンパイル使用メモリ 220,856 KB
実行使用メモリ 93,676 KB
最終ジャッジ日時 2023-08-12 00:24:51
合計ジャッジ時間 12,761 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
26,736 KB
testcase_01 AC 13 ms
26,996 KB
testcase_02 AC 301 ms
93,676 KB
testcase_03 AC 172 ms
36,308 KB
testcase_04 AC 308 ms
37,800 KB
testcase_05 AC 561 ms
46,056 KB
testcase_06 AC 181 ms
33,948 KB
testcase_07 AC 42 ms
28,548 KB
testcase_08 AC 64 ms
29,456 KB
testcase_09 AC 32 ms
27,928 KB
testcase_10 AC 91 ms
30,392 KB
testcase_11 AC 345 ms
38,812 KB
testcase_12 AC 196 ms
34,160 KB
testcase_13 AC 95 ms
30,720 KB
testcase_14 AC 28 ms
27,664 KB
testcase_15 AC 148 ms
32,428 KB
testcase_16 AC 394 ms
40,352 KB
testcase_17 AC 422 ms
40,944 KB
testcase_18 AC 94 ms
30,424 KB
testcase_19 AC 354 ms
39,200 KB
testcase_20 AC 37 ms
28,148 KB
testcase_21 AC 61 ms
29,188 KB
testcase_22 AC 292 ms
36,892 KB
testcase_23 AC 163 ms
33,044 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:99:9: 警告: ‘root’ may be used uninitialized [-Wmaybe-uninitialized]
   99 |     int root;
      |         ^~~~

ソースコード

diff #

#pragma region
#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std; typedef long double ld; typedef long long ll;
typedef unsigned long long ull;
#define endl "\n"
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define VPII vector<PII>
#define VPLL vector<PLL>
#define ALL(x) (x).begin(), (x).end()
constexpr int INF=1<<30; constexpr ll LINF=1LL<<60; constexpr ll mod=1e9+7; constexpr int NIL = -1;
template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
#pragma endregion
//-------------------
struct mint {
	ll x;
	mint(ll x=0):x(x%mod){}

	bool operator==(const mint a)const{return x==a.x;}
	bool operator!=(const mint a)const{return x!=a.x;}
	bool operator>=(const mint a){return (x >= a.x)? 1: 0;}
	bool operator<(const mint a){return !(*this>=a);}
	bool operator>(const mint a){return (x > a.x)? 1:0;}
	bool operator<=(const mint a){return !(*this>a);}
	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;  //2 square
		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;
	}
};

int n;
const int MX = 1e6+6;
vector<ll> v[MX]; 
bool nt[MX];
mint ans(0);

ll dfs(ll node, ll above) {
    ll res = 0;
    above++;
    for(auto &to: v[node]) {
        ll child;
        child = dfs(to, above);
        ans += child*above;
        res += child;
    }
    return ++res;
}
int main(){
    cin.tie(0); ios::sync_with_stdio(false); //cout << fixed << setprecision(15);
    cin >> n;
    FOR(i,0,n-2) {
        ll a,b; cin >> a >> b;
        v[a].emplace_back(b);
        nt[b] = true;
    }
    int root;
    FOR(i,1,n) {
        if(!nt[i]) {
            root = i;
            break;
        }
    }
    // cout << root;
    dfs(root, 0);
    cout << ans.x << endl;
    return 0;
}
0