99602024-03-21 19:08:53111Erőművekcpp17Time limit exceeded 30/100287ms25232 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

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
#ifdef CB
	freopen("be2.txt","r",stdin);
//	freopen("out.txt","w",stdout);
#endif
	int N,M;
	cin>>N>>M;
	vector<int>v(N+1);
	int su=0;
	for(int i=1;i<=N;i++){
		cin>>v[i];
		su+=v[i];
	}
	vector<vector<int>>g(N+1);
	for(int i=0;i<M;i++){
		int a,b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	int ans=INT_MAX;
	int ans2=0;
	vector<int>l(N+1,INT_MAX),a(N+1,0);
	vector<int>s(N+1);
	vector<ordered_set<int>>z(N+1);
	auto dfs=[&](auto self,int i)->void{
		for(int j:g[i]){
			if(l[j]==l[i]-1){
				continue;
			}
			if(l[j]!=INT_MAX){
				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];
			}
			if(l[a[j]]>l[i]){
				z[j].insert(s[j]);
				int k=su-s[j];
				for(int kk:z[j]){
					if(ans>max({k,kk,su-k-kk})-min({k,kk,su-k-kk})){
						ans=max({k,kk,su-k-kk})-min({k,kk,su-k-kk});
						ans2=0;
					}
					if(ans==max({k,kk,su-k-kk})-min({k,kk,su-k-kk})){
						ans2++;
					}
				}
			}
			if(z[j].size()>z[i].size()){
				swap(z[i],z[j]);
			}
			if(l[a[j]]>l[i])
			for(int k:z[j]){
				for(int kk:z[i]){
					if(ans>max({k,kk,su-k-kk})-min({k,kk,su-k-kk})){
						ans=max({k,kk,su-k-kk})-min({k,kk,su-k-kk});
						ans2=0;
					}
					if(ans==max({k,kk,su-k-kk})-min({k,kk,su-k-kk})){
						ans2++;
					}
				}
			}
			for(int k:z[j]){
				z[i].insert(k);
			}
		}
		s[i]+=v[i];
	};
	l[1]=0;
	dfs(dfs,1);
//	for(int i=1;i<=N;i++){
//		cout<<i<<" : "<<l[i]<<" "<<a[i]<<" "<<s[i]<<endl;
//	}
	cout<<ans<<'\n';
	cout<<ans2<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base30/100
1Accepted0/03ms1888 KiB
2Accepted0/012ms4028 KiB
3Accepted5/54ms2788 KiB
4Accepted5/54ms2808 KiB
5Accepted5/58ms3556 KiB
6Accepted5/58ms3560 KiB
7Accepted5/583ms6916 KiB
8Accepted5/597ms6440 KiB
9Time limit exceeded0/5250ms6736 KiB
10Time limit exceeded0/5263ms5300 KiB
11Time limit exceeded0/6287ms11524 KiB
12Time limit exceeded0/6279ms9472 KiB
13Time limit exceeded0/6259ms14976 KiB
14Time limit exceeded0/6263ms13068 KiB
15Time limit exceeded0/6270ms18344 KiB
16Time limit exceeded0/6243ms16688 KiB
17Time limit exceeded0/6263ms22020 KiB
18Time limit exceeded0/6272ms19912 KiB
19Time limit exceeded0/6280ms25232 KiB
20Time limit exceeded0/6263ms22328 KiB