【题解】CF1687B Railway System

Sol

首先先用 m 次查询把每条边的权值弄出来。
然后把边按照权值排序。从小到大依次加入每条边。
当一条边在最后的答案中那么当且仅当 xlast=ai,其中 x 为当前询问的答案,last 为前 i1 条边组成的最优答案,ai 表示第 i 条边的权值。
正确性可以考虑 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';
}





posted @   dd_d  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示
主题色彩