【题解】CF1687B Railway System
Sol
首先先用
然后把边按照权值排序。从小到大依次加入每条边。
当一条边在最后的答案中那么当且仅当
正确性可以考虑 kruskal 的过程。
Code
//LYC_music yyds!
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0)
#define lowbit(x) (x&(-x))
#define int long long
using namespace std;
inline char gc()
{
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
int read()
{
int pos=1,num=0;
char ch=getchar();
while (!isdigit(ch))
{
if (ch=='-') pos=-1;
ch=getchar();
}
while (isdigit(ch))
{
num=num*10+(int)(ch-'0');
ch=getchar();
}
return pos*num;
}
void write(int x)
{
if (x<0)
{
putchar('-');
write(-x);
return;
}
if (x>=10) write(x/10);
putchar(x%10+'0');
}
void writesp(int x)
{
write(x);
putchar(' ');
}
void writeln(int x)
{
write(x);
putchar('\n');
}
const int N=1e5+10;
int n,m,bel[N],a[N],ans,p[N],last;
int query()
{
cout<<"? ";
for (int i=1;i<=m;i++)
cout<<bel[i]; cout<<'\n';
int now; cin>>now; return now;
}
signed main()
{
n=read(); m=read();
for (int i=1;i<=m;i++)
bel[i]=1,a[i]=query(),bel[i]=0,p[i]=i;
sort(p+1,p+m+1,[](int x,int y)
{
return a[x]<a[y];
});
for (int i=1;i<=m;i++)
{
bel[p[i]]=1;
int x=query();
if (x-last==a[p[i]]) last=x;
else bel[p[i]]=0;
}
for (int i=1;i<=m;i++)
if (bel[p[i]]) ans+=a[p[i]];
cout<<"! "<<ans<<'\n';
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】