99582024-03-21 18:20:25111Erőművekcpp17Wrong answer 16/100282ms76208 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("be1.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 ans1=0;
	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);
			if(l[a[j]]<l[a[i]]){
				a[i]=a[j];
			}
			if(l[a[j]]>l[i]){
				z[i].insert(s[j]);
				int s1=su-s[j],s2,s3;
				auto f=[&](){
					ans1=max({s1,s2,s3})-min({s1,s2,s3});
					if(ans1<ans){
						ans=ans1;
						ans2=0;
					}
					if(ans1==ans){
						//s2>=s1-ans
						//su-s1-s2>=s1-ans,s2<=su-s1-(s1-ans)
						//s2<=s1+ans
						//su-s1-s2<=s1+ans,s2>=su-s1-(s1+ans)
						//s2>=su-s1-s2-ans,s2>=(su-s1-ans)/2
						//s2<=su-s1-s2+ans,s2<=(su-s1+ans)/2
						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){
							exit(1);
						}
						ans2+=z[j].order_of_key(hh+1)-z[j].order_of_key(ll);
					}
				};
				auto it=z[j].lower_bound(s[j]/2);
				if(it!=z[j].end()){
					s2=*it;
					s3=su-s1-s2;
					f();
				}
				if(it!=z[j].begin()){
					--it;
					s2=*it;
					s3=su-s1-s2;
					f();
				}
				if(!z[j].empty()){
					s2=*s.begin();
					s3=su-s1-s2;
					f();
					s2=*--s.end();
					s3=su-s1-s2;
					f();
				}
			}
			s[i]+=s[j];
			if(z[j].size()>z[i].size()){
				swap(z[i],z[j]);
			}
			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
base16/100
1Accepted0/03ms1888 KiB
2Wrong answer0/07ms4152 KiB
3Accepted5/53ms2780 KiB
4Wrong answer0/53ms2800 KiB
5Accepted5/54ms3556 KiB
6Wrong answer0/54ms3584 KiB
7Wrong answer0/514ms7040 KiB
8Wrong answer0/59ms6188 KiB
9Wrong answer0/530ms12296 KiB
10Wrong answer0/519ms10204 KiB
11Wrong answer0/683ms30352 KiB
12Wrong answer0/652ms23968 KiB
13Accepted6/6149ms53120 KiB
14Wrong answer0/685ms39472 KiB
15Time limit exceeded0/6261ms76208 KiB
16Wrong answer0/6123ms54788 KiB
17Time limit exceeded0/6268ms46020 KiB
18Wrong answer0/6171ms66552 KiB
19Time limit exceeded0/6282ms54168 KiB
20Wrong answer0/6171ms76168 KiB