99702024-03-21 21:40:31111Erőművekcpp17Accepted 100/100123ms36064 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long

#define INF (int)1e18

auto pi = new char[10000000];

int ru() {
	while (!isdigit(*pi)) {
		pi++;
	}
	int res = 0;
	while (isdigit(*pi)) {
		res *= 10;
		res += *pi - '0';
		pi++;
	}
	return res;
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
#ifdef CB
	freopen("be2.txt","r",stdin);
//	freopen("out.txt","w",stdout);
#endif
	fread(pi,1,10000000,stdin);
	int N=ru(),M=ru();
	vector<int>v(N+1);
	int su=0;
	for(int i=1;i<=N;i++){
		v[i]=ru();
		su+=v[i];
	}
	vector<vector<int>>g(N+1);
	for(int i=0;i<M;i++){
		int a=ru(),b=ru();
		g[a].push_back(b);
		g[b].push_back(a);
	}
	vector<int>l(N+1,INF),a(N+1,0);
	vector<int>s(N+1);
	auto dfs=[&](auto self,int i)->void{
		for(int j:g[i]){
			if(l[j]==l[i]-1){
				continue;
			}
			if(l[j]!=INF){
				if(l[j]<l[a[i]]){
					a[i]=j;
				}
				continue;
			}
			l[j]=l[i]+1;
			self(self,j);
			s[i]+=s[j];
			if(l[a[j]]<l[a[i]]){
				a[i]=a[j];
			}
		}
		s[i]+=v[i];
	};
	l[1]=0;
	dfs(dfs,1);
	int ans=INF;
	int ans1=0;
	int ans2=0;
	ordered_set<int>z,o;
	auto dfs2=[&](auto self,int i)->void{
		for(int j:g[i]){
			if(l[j]<l[i]){
				continue;
			}
			if(l[a[j]]>l[i]){
				int s1=s[j],s2,s3;
				auto f=[&](){
					ans1=max({s1,s2,s3})-min({s1,s2,s3});
					if(ans1<ans){
						ans=ans1;
						ans2=0;
					}
				};
				auto it=o.upper_bound((su-s1)/2);
				if(it!=o.end()){
					s2=*it;
					s3=su-s1-s2;
					f();
				}
				if(it!=o.begin()){
					--it;
					s2=*it;
					s3=su-s1-s2;
					f();
				}
				int ll=max({s1-ans,su-s1-(s1+ans),(su-s1-ans)/2});
				int hh=min({s1+ans,su-s1-(s1-ans),(su-s1+ans)/2});
				if(ll<=hh){
					ans2+=o.order_of_key(hh+1)-o.order_of_key(ll);
				}
				{
					s1=s[j],s2,s3;
					auto f=[&](){
						ans1=max({s1,s2,s3})-min({s1,s2,s3});
						if(ans1<ans){
							ans=ans1;
							ans2=0;
						}
					};
					auto it=z.upper_bound((su-s1)/2+s[j]);
					if(it!=z.end()){
						s2=*it-s[j];
						s3=su-s1-s2;
						f();
					}
					if(it!=z.begin()){
						--it;
						s2=*it-s[j];
						s3=su-s1-s2;
						f();
					}
					int ll=max({s1-ans,su-s1-(s1+ans),(su-s1-ans)/2})+s[j];
					int hh=min({s1+ans,su-s1-(s1-ans),(su-s1+ans)/2})+s[j];
					if(ll<=hh){
						ans2+=z.order_of_key(hh+1)-z.order_of_key(ll);
					}
				}
				z.insert(s[j]);
			}
			self(self,j);
			if(l[a[j]]>l[i]){
				z.erase(z.upper_bound(s[j]));
				o.insert(s[j]);
			}
		}
	};
	dfs2(dfs2,1);
	cout<<ans<<'\n';
	cout<<ans2<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base100/100
1Accepted0/03ms1972 KiB
2Accepted0/04ms3324 KiB
3Accepted5/53ms2680 KiB
4Accepted5/53ms2776 KiB
5Accepted5/53ms3200 KiB
6Accepted5/53ms3164 KiB
7Accepted5/57ms4552 KiB
8Accepted5/56ms4612 KiB
9Accepted5/512ms6080 KiB
10Accepted5/510ms6080 KiB
11Accepted6/630ms12752 KiB
12Accepted6/628ms12580 KiB
13Accepted6/654ms19752 KiB
14Accepted6/648ms19312 KiB
15Accepted6/682ms26444 KiB
16Accepted6/672ms26248 KiB
17Accepted6/6104ms31320 KiB
18Accepted6/694ms30908 KiB
19Accepted6/6123ms36064 KiB
20Accepted6/6104ms35428 KiB