跳到正文

闲话

关于快读压行写法的悬疑

Zyc_zhouyuchen约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年2月17日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前

晚自习时lr提出一个快读逆天压行做法,以建图过程为例:
原版代码

C++
	while(m--){
		u=rd(),v=rd();
		g[u].push_back(v);
	}

压行代码

C++
while(m--)g[rd()].push_back(rd());

当然有一定局限性,而且根据zyl的说法,这可能导致错误,因为g[a].push_back(b)中a,b优先级相同

C++
#include <bits/stdc++.h>
using namespace std;
int m,u,v;vector<int>g[15];
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}
int main(){
	cin>>m;
	while(m--)g[rd()].push_back(rd());//1
	while(m--){u=rd(),v=rd();g[u].push_back(v);}//2
	for(int i=1;i<=10;i++){
		if(!g[i].size())continue;
		cout<<i<<':';for(auto it:g[i])cout<<' '<<it;cout<<'\n';
	}
	return 0;
}

Geany本地试验通过,但还需验证,不建议使用

原文