結果

問題 No.1769 Don't Stop the Game
ユーザー auauaauaua
提出日時 2021-11-27 01:35:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 834 ms / 3,000 ms
コード長 1,781 bytes
コンパイル時間 1,858 ms
コンパイル使用メモリ 181,540 KB
実行使用メモリ 108,020 KB
最終ジャッジ日時 2023-09-12 06:15:28
合計ジャッジ時間 15,235 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
18,412 KB
testcase_01 AC 9 ms
18,488 KB
testcase_02 AC 9 ms
18,560 KB
testcase_03 AC 9 ms
19,068 KB
testcase_04 AC 9 ms
18,824 KB
testcase_05 AC 10 ms
19,120 KB
testcase_06 AC 9 ms
18,804 KB
testcase_07 AC 10 ms
18,804 KB
testcase_08 AC 424 ms
60,132 KB
testcase_09 AC 377 ms
58,664 KB
testcase_10 AC 588 ms
81,240 KB
testcase_11 AC 328 ms
50,876 KB
testcase_12 AC 310 ms
42,512 KB
testcase_13 AC 325 ms
43,356 KB
testcase_14 AC 345 ms
47,172 KB
testcase_15 AC 428 ms
54,924 KB
testcase_16 AC 585 ms
73,852 KB
testcase_17 AC 734 ms
92,568 KB
testcase_18 AC 797 ms
103,072 KB
testcase_19 AC 798 ms
104,144 KB
testcase_20 AC 834 ms
108,020 KB
testcase_21 AC 818 ms
105,252 KB
testcase_22 AC 832 ms
106,520 KB
testcase_23 AC 301 ms
35,120 KB
testcase_24 AC 304 ms
39,124 KB
testcase_25 AC 191 ms
53,112 KB
testcase_26 AC 449 ms
65,404 KB
testcase_27 AC 226 ms
81,184 KB
testcase_28 AC 477 ms
93,584 KB
testcase_29 AC 524 ms
81,248 KB
testcase_30 AC 533 ms
81,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=200010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;

vector<map<ll,pll> >ma(MAX);
vector<vector<pll> >G(MAX);
ll sum=0;
vector<ll>sz(MAX);
ll n;
void dfs(ll i,ll p,ll s){
    map<ll,pll>tmp;
    ll cnt=1;
    for(auto e:G[i]){
        if(e.fi==p)continue;
        dfs(e.fi,i,s^e.se);
        cnt+=sz[e.fi];
    }
    for(auto e:G[i]){
        if(e.fi==p)continue;
        ll z=sz[e.fi];
        sum+=ma[e.fi][s].fi*(cnt-z);
        if(ma[e.fi].size()>tmp.size()){
            swap(tmp,ma[e.fi]);
        }
        for(auto u:ma[e.fi]){
            if(u.fi!=s)sum+=u.se.fi*tmp[u.fi].se+u.se.se*tmp[u.fi].fi;
            tmp[u.fi].fi+=u.se.fi;
            tmp[u.fi].se+=u.se.se;
        }
    }
    sum+=tmp[s].se;
    sum+=(n-cnt)*tmp[s].fi;
    tmp[s].fi=1;
    tmp[s].se=cnt;
    swap(ma[i],tmp);
    sz[i]=cnt;
}

void solve(){
    cin>>n;
    rep(i,n-1){
        ll a,b,c;cin>>a>>b>>c;
        a--;b--;
        G[a].pb(mp(b,c));
        G[b].pb(mp(a,c));
    }
    dfs(0,-1,0);
    ll ans=n*(n-1)-sum;
    cout<<ans<<endl;
}

signed main(){
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    solve();
}
0