結果

問題 No.2115 Making Forest Easy
ユーザー 👑 kmjpkmjp
提出日時 2022-10-29 00:47:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,050 ms / 2,000 ms
コード長 1,919 bytes
コンパイル時間 2,375 ms
コンパイル使用メモリ 209,024 KB
実行使用メモリ 8,724 KB
最終ジャッジ日時 2023-09-20 08:00:19
合計ジャッジ時間 30,527 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,524 KB
testcase_01 AC 3 ms
7,796 KB
testcase_02 AC 799 ms
8,560 KB
testcase_03 AC 648 ms
7,928 KB
testcase_04 AC 825 ms
8,588 KB
testcase_05 AC 608 ms
8,232 KB
testcase_06 AC 801 ms
8,688 KB
testcase_07 AC 1,048 ms
7,932 KB
testcase_08 AC 798 ms
8,600 KB
testcase_09 AC 717 ms
7,896 KB
testcase_10 AC 801 ms
8,560 KB
testcase_11 AC 647 ms
7,888 KB
testcase_12 AC 717 ms
8,020 KB
testcase_13 AC 517 ms
7,856 KB
testcase_14 AC 716 ms
7,920 KB
testcase_15 AC 1,050 ms
7,940 KB
testcase_16 AC 653 ms
8,024 KB
testcase_17 AC 9 ms
7,592 KB
testcase_18 AC 24 ms
7,700 KB
testcase_19 AC 234 ms
8,232 KB
testcase_20 AC 689 ms
7,856 KB
testcase_21 AC 7 ms
7,592 KB
testcase_22 AC 31 ms
7,840 KB
testcase_23 AC 454 ms
8,288 KB
testcase_24 AC 651 ms
7,924 KB
testcase_25 AC 148 ms
7,808 KB
testcase_26 AC 715 ms
8,168 KB
testcase_27 AC 185 ms
7,764 KB
testcase_28 AC 1,047 ms
7,984 KB
testcase_29 AC 12 ms
7,696 KB
testcase_30 AC 1,045 ms
7,892 KB
testcase_31 AC 832 ms
8,580 KB
testcase_32 AC 801 ms
8,688 KB
testcase_33 AC 795 ms
8,724 KB
testcase_34 AC 12 ms
7,660 KB
testcase_35 AC 42 ms
7,828 KB
testcase_36 AC 801 ms
8,720 KB
testcase_37 AC 166 ms
7,692 KB
testcase_38 AC 51 ms
7,792 KB
testcase_39 AC 132 ms
7,980 KB
testcase_40 AC 1,035 ms
7,908 KB
testcase_41 AC 718 ms
8,000 KB
testcase_42 AC 649 ms
7,924 KB
testcase_43 AC 189 ms
7,704 KB
testcase_44 AC 50 ms
7,912 KB
testcase_45 AC 562 ms
7,780 KB
testcase_46 AC 647 ms
7,932 KB
testcase_47 AC 148 ms
7,692 KB
testcase_48 AC 840 ms
8,364 KB
testcase_49 AC 826 ms
8,420 KB
testcase_50 AC 256 ms
7,868 KB
testcase_51 AC 105 ms
7,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
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(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
int A[101010];
int D[101010];
int C[101010];
vector<int> E[101010];
const ll mo=998244353;

ll dp1[101010][2];
ll dp2[101010][2];


ll modpow(ll a, ll n = mo-2) {
	ll r=1;a%=mo;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

void dfs(int cur,int pre,int sc) {
	ll a[2]={},b[2]={};
	a[C[cur]>sc]=b[C[cur]>sc]=1;
	int i,j;
	FORR(e,E[cur]) if(e!=pre) {
		dfs(e,cur,sc);
		ll na[2]={},nb[2]={};
		// not connect
		FOR(i,2) {
			na[i]+=a[i]*(dp1[e][0]+dp1[e][1]);
			nb[i]+=b[i]*(dp1[e][0]+dp1[e][1]);
		}
		// connect
		FOR(i,2) FOR(j,2) {
			na[max(i,j)]+=a[i]*dp1[e][j];
			nb[max(i,j)]+=b[i]*dp1[e][j]+a[i]*dp2[e][j];
		}
		
		FOR(i,2) {
			a[i]=na[i]%mo;
			b[i]=nb[i]%mo;
		}
	}
	FOR(i,2) {
		dp1[cur][i]=a[i];
		dp2[cur][i]=b[i];
	}
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	vector<pair<int,int>> V;
	FOR(i,N) {
		cin>>A[i];
		V.push_back({A[i],i});
	}
	sort(ALL(V));
	FOR(i,V.size()) C[V[i].second]=i;
	FOR(i,N-1) {
		cin>>x>>y;
		E[x-1].push_back(y-1);
		E[y-1].push_back(x-1);
	}
	
	ll ret=0;
	FOR(i,N) {
		dfs(i,i,C[i]);
		ret+=A[i]*dp2[i][0]%mo;
	}
	cout<<ret%mo<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0