CF重现赛37
CF1677B Tokitsukaze and Meeting
::::info[题目链接] https://www.luogu.com.cn/problem/CF1677B :::: ::::info[做法] 定义两个数组:
col[i] 代表这一列中学生编号模
row[i] 代表
首先对于 col[i] 容易发现,对于每次进入的学生更新即可,因为一个模
对于 row[i],我们可以不断递推,当 row[i] 就可以从 row[i-m] 转移过来,只用判断当前 row[i]=row[i-m]+1,判断一个区间是否有
假如 row[i]=1 |
|---|
::::info[代码]
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define str string
#define YES cout<<"YES"<<'\n';
#define NO cout<<"NO"<<'\n';
inline int read()
{
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*10+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
return;
}
const ll INF=1e18;
const ll MAXN=1e6+5;
ll t,n,m;
ll pre[MAXN],row[MAXN],col[MAXN];
str s;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--){
cin>>n>>m;
cin>>s;
for(int i=0;i<=n*m;++i){
pre[i]=0;
row[i]=0;
col[i]=0;
}
for(int i=1;i<=n*m;++i){
pre[i]=pre[i-1];
if(s[i-1]=='1'){
pre[i]++;
}
}
ll curcol=0;
for(int i=1;i<=n*m;++i){
ll x=i%m;
if(s[i-1]=='1'&&!col[x]){
col[x]=1;
curcol++;
}
if(i<=m){
if(pre[i]>0){
row[i]=1;
}
}
else{
row[i]=row[i-m]+(pre[i]-pre[i-m]>0);
}
cout<<row[i]+curcol<<" ";
}
cout<<'\n';
}
return 0;
}
::::
CF1675F Vlad and Unfinished Business
::::info[题目链接]
https://www.luogu.com.cn/problem/CF1675F
::::
::::info[做法]
可以从
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define str string
#define YES cout<<"YES"<<'\n';
#define NO cout<<"NO"<<'\n';
inline int read()
{
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*10+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
return;
}
const ll INF=1e18;
const ll MAXN=2e5+5;
ll t,n,k,a[MAXN],x,y,dep[MAXN];
ll need[MAXN],fa[MAXN],cnt;
vector<ll> g[MAXN];
void dfs1(ll u,ll f){
fa[u]=f;
dep[u]=dep[f]+1;
for(ll v:g[u]){
if(v==f){
continue;
}
dfs1(v,u);
if(need[v]){
need[u]=1;
cnt++;
}
}
}
void solve(){
cin>>n>>k>>x>>y;
for(int i=1;i<=n;++i){
g[i].clear();
need[i]=0;
dep[i]=0;
fa[i]=0;
}
for(int i=1;i<=k;++i){
cin>>a[i];
need[a[i]]=1;
}
for(int i=1;i<n;++i){
ll u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
cnt=0;
dfs1(x,0);
ll same=0;
ll u=y;
while(u!=x){
if(need[u]){
same++;
}
u=fa[u];
}
ll dis=dep[y]-dep[x];
ll ans=2*cnt+dis-2*same;
cout<<ans<<'\n';
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--){
solve();
}
return 0;
}
::::