2019 ICPC Asia Nanchang Regional
链接:https://www.jisuanke.com/contest/5530/challenges
C. And and Pair
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10,mod=1e9+7;
int t;
string n;
int qpow(int b,int n,int mod)
{
int res=1;
while(n)
{
if(n&1) res=1ll*res*b%mod;
b=1ll*b*b%mod;
n>>=1;
}
return res;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n;
int len=n.size();
int x=0,y=0;
ll ans=1;
for(int i=len-1;i>=0;--i)
{
if(n[i]=='1')
{
ans=(ans+1ll*qpow(2,x,mod)*qpow(3,y,mod)%mod)%mod;
y++;
}
else x++;
}
printf("%lld\n",ans);
}
return 0;
}
E. Bob’s Problem
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=5e4+10,maxm=5e5+10;
int t,n,m,k;
struct Edge
{
int u,v,w,type;
bool operator<(const Edge& b) const
{
return w>b.w;
}
} e[maxm];
int p[maxn];
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
int visit[maxm];
void solve()
{
for(int i=1; i<=n; ++i) p[i]=i;
for(int i=1; i<=m; ++i) visit[i]=0;
sort(e+1,e+1+m);
ll ans=0,cnt=0;
for(int i=1; i<=m; ++i)
{
int ru=find(e[i].u),rv=find(e[i].v);
if(e[i].type==0)
{
visit[i]=1;
ans+=e[i].w;
p[ru]=rv;
}
}
for(int i=1; i<=m; ++i)
{
if(visit[i]) continue;
int ru=find(e[i].u),rv=find(e[i].v);
if(ru==rv) continue;
if(cnt==k) break;
visit[i]=1;
cnt++;
ans+=e[i].w;
p[ru]=rv;
}
for(int i=1; i<=m; ++i)
{
if(visit[i]) continue;
if(cnt==k) break;
int ru=find(e[i].u),rv=find(e[i].v);
ans+=e[i].w;
p[ru]=rv;
cnt++;
}
int cnt2=0;
for(int i=1; i<=n; ++i)
if(p[i]==i) cnt2++;
if(cnt2>1) puts("-1");
else printf("%lld\n",ans);
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1; i<=m; ++i)
{
int u,v,w,type;
scanf("%d%d%d%d",&u,&v,&w,&type);
e[i]= {u,v,w,type};
}
solve();
}
return 0;
}