小红拿到了一个n个节点、m条边的无向连通图,每个节点的权值已知。
小红删掉一条边时,可以获得连接该边的两个节点“权值乘积末尾0数量”的价值。例如,一条边连接的两个点权值是50和60,那么小红删掉这条边获得的价值为3。
小红想知道,在保证这张图连通的情况下,最多可以通过删边获得多少价值?
Input
第一行输入两个正整数 n 和 m (2 ≤ n ≤ 1e5,n-1 ≤ m ≤ 1e5),代表图的点数和边数。
第二行输入 n 个正整数 ai (1 ≤ ai ≤ 1e14),代表每个点的权值。
接下来的 m 行,每行输入两个正整数 u 和 v (1 ≤ u,v ≤ n),代表点 u 和 v 有一条边连接。
保证图连通,且无重边,无自环。
Output
输出一个整数,代表删边可以获得的最大价值。
输入
3 3
5 8 25
1 2
2 3
1 3
输出
2
解析:
建立边权 w = min(cnt5 + cnt5 ,cnt2 + cnt2),可以代表两个节点“权值乘积末尾0数量”的价值。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef pair<int,int> PII;
const int N=2e6+10;
int p[N];
int find(int x)
{
if (x!=p[x]) p[x]=find(p[x]);
return p[x];
}
struct node
{
int a,b,w;
}str[N];
bool cmp(node x,node y)
{
return x.w<y.w;
}
PII t[N];
int n,m;
signed main()
{
ios;
cin>>n>>m;
for (int i=1;i<=n;i++) p[i]=i;
for (int i=1;i<=n;i++)
{
int x;
cin>>x;
int a=0,b=0;
while (x%2==0&&x>0)
{
x /=2;
a++;
}
while (x%5==0&&x>0)
{
x /=5;
b++;
}
t[i]={a,b};
}
for (int i=0;i<m;i++)
{
int a,b,w;
cin>>a>>b;
w=min(t[a].first+t[b].first,t[a].second+t[b].second);
str[i]={a,b,w};
}
sort(str,str+m,cmp);
int ans=0;
for (int i=0;i<m;i++)
{
int a=str[i].a,b=str[i].b,w=str[i].w;
int x=find(a),y=find(b);
if (x!=y) p[y]=x;
else ans +=w;
}
cout<<ans;
return 0;
}