当前位置:网站首页>4.20学习记录
4.20学习记录
2022-04-21 07:11:00 【追随远方的某R】
看样子像是树剖就写了树剖,,
虽然当时看到了125mb的内存限制但是我依然头铁
#include <bits/stdc++.h>
//#define int long long
#define endl '\n'
#define ls p<<1
#define rs p<<1|1
#define mid (l+r)/2
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int N = 5e5+1000;
int n,m;
struct node
{
int nex,to;
};
node edge[N<<1];
int head[N],tot;
int dep[N],son[N],fa[N],siz[N];
int top[N],cnt,nid[N];
void add(int from,int to)
{
edge[++tot].to=to;
edge[tot].nex=head[from];
head[from]=tot;
}
void DFS1(int now,int fath)
{
dep[now]=dep[fath]+1;
siz[now]=1;son[now]=0;
fa[now]=fath;
for(int i=head[now];i;i=edge[i].nex)
{
if(edge[i].to==fath)
continue;
DFS1(edge[i].to,now);
siz[now]+=siz[edge[i].to];
if(siz[edge[i].to]>siz[son[now]])
{
son[now]=edge[i].to;
}
}
}
void DFS2(int now,int topx)//topx,先重儿子再轻儿子
{
top[now]=topx;
nid[now]=++cnt;
if(son[now])
{
DFS2(son[now],topx);
}
else
return ;
for(int i=head[now];i;i=edge[i].nex)
{
if(edge[i].to!=fa[now]&&edge[i].to!=son[now])
{
DFS2(edge[i].to,edge[i].to);
}
}
}
int LCA(int x,int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])
{
swap(x,y);
}
x=fa[top[x]];
}
return dep[x]>dep[y]?y:x;
}
//segtree
int tree[N<<2];
void up(int p)
{
tree[p]=tree[ls]+tree[rs];
}
void bulid(int l,int r,int p)
{
if(l==r)
{
tree[p]=1;
return ;
}
bulid(l,mid,ls);
bulid(mid+1,r,rs);
up(p);
}
int query(int l,int r,int p,int nl,int nr)
{
int res=0;
if(l>=nl&&r<=nr)
{
return tree[p];
}
if(nl<=mid)
{
res+=query(l,mid,ls,nl,nr);
}
if(nr>mid)
{
res+=query(mid+1,r,rs,nl,nr);
}
return res;
}
//use
int q_range(int x,int y)
{
int ans=0;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])
{
swap(x,y);
}
ans+=query(1,n,1,nid[top[x]],nid[x]);
x=fa[top[x]];
}
if(dep[x]>dep[y])
{
swap(x,y);
}
ans+=query(1,n,1,nid[x],nid[y]-1);
return ans;
}
int main()
{
fastio
cin>>n>>m;
int a,b;
for(int i=1;i<=n-1;i++)
{
cin>>a>>b;
add(a,b);
add(b,a);
}
DFS1(1,0);
DFS2(1,1);
bulid(1,n,1);
for(int i=1,x,y,z,u,v,t,f,ans;i<=m;i++)
{
cin>>x>>y>>z;
u=LCA(x,y);v=LCA(y,z);t=LCA(z,x);
f=dep[u]>dep[v]?u:v;
f=dep[f]>dep[t]?f:t;
ans=q_range(x,f)+q_range(y,f)+q_range(z,f);
cout<<f<<" "<<ans<<endl;
}
return 0;
}
/* 1 2 3 4 5 6 */
然后喜提5个点MLE
发觉这题其实用不上树剖的线段树部分,只要求个LCA就行了。
#include <bits/stdc++.h>
#define endl '\n'
#define ls p<<1
#define rs p<<1|1
#define mid (l+r)/2
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int N = 5e5+1000;
int n,m;
struct node
{
int nex,to;
};
node edge[N<<1];
int head[N],tot;
int dep[N],son[N],fa[N],siz[N];
int top[N],cnt,nid[N];
void add(int from,int to)
{
edge[++tot].to=to;
edge[tot].nex=head[from];
head[from]=tot;
}
void DFS1(int now,int fath)
{
dep[now]=dep[fath]+1;
siz[now]=1;son[now]=0;
fa[now]=fath;
for(int i=head[now];i;i=edge[i].nex)
{
if(edge[i].to==fath)
continue;
DFS1(edge[i].to,now);
siz[now]+=siz[edge[i].to];
if(siz[edge[i].to]>siz[son[now]])
{
son[now]=edge[i].to;
}
}
}
void DFS2(int now,int topx)//topx,先重儿子再轻儿子
{
top[now]=topx;
nid[now]=++cnt;
if(son[now])
{
DFS2(son[now],topx);
}
else
return ;
for(int i=head[now];i;i=edge[i].nex)
{
if(edge[i].to!=fa[now]&&edge[i].to!=son[now])
{
DFS2(edge[i].to,edge[i].to);
}
}
}
int LCA(int x,int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])
{
swap(x,y);
}
x=fa[top[x]];
}
return dep[x]>dep[y]?y:x;
}
int main()
{
fastio
cin>>n>>m;
int a,b;
for(int i=1;i<=n-1;i++)
{
cin>>a>>b;
add(a,b);
add(b,a);
}
DFS1(1,0);
DFS2(1,1);
for(int i=1,x,y,z,u,v,t,f,ans;i<=m;i++)
{
cin>>x>>y>>z;
u=LCA(x,y);v=LCA(y,z);t=LCA(z,x);
f=dep[u]>dep[v]?u:v;
f=dep[f]>dep[t]?f:t;
ans=dep[x]+dep[y]+dep[z]-dep[u]-dep[v]-dep[t];
cout<<f<<" "<<ans<<endl;
}
return 0;
}
/* 1 2 3 4 5 6 */
上课的时候老师给的一个题。
题意:给定一张图,然后又“X”有“.”,我们认为一个“X”的四周(上下左右)以及四个对角的“X”都是相连的,那么请问,一张图中联通的一片“X”的周长最大是多少。
注意:不会有“X”包围“.”的情况。
思路1:直接按照联通块去搜索,但是发现对角相连的情况过于复杂,结合DFS很难实现,考虑到应该是铜牌级别的题目,所以想想别的方法
思路2:将原图拓展一圈“.”,然后对于“.”只要碰壁就让周长加1,但是发现无法判断是否属于同一联通块,所以我打算用dsu重构哈希映射来做,但是好像也不是最优解
思路3:将原图拓展一圈“.”,然后对于“X”,去标记X的边界
#include <stdio.h>
#include <string.h>
char maps[35][35];
int flag[35][35];
int mov1[4][2]= {
{
1,0},{
-1,0},{
0,1},{
0,-1}};
int mov2[4][2]= {
{
1,1},{
1,-1},{
-1,1},{
-1,-1}};
int r,c;
int click_x,click_y;
int total;///周长
void dfs(int x,int y)
{
if(flag[x][y]!=0) return ;
else
{
flag[x][y]=1;
int tx;
int ty;
for(int k=0; k<4; k++)
{
tx=x+mov1[k][0];
ty=y+mov1[k][1];
if(maps[tx][ty]=='X'&&flag[tx][ty]==0)
dfs(tx,ty);
else if(maps[tx][ty]=='.')
total++;
}
for(int k=0; k<4; k++)
{
tx=x+mov2[k][0];
ty=y+mov2[k][1];
if(maps[tx][ty]=='X'&&flag[tx][ty]==0)
dfs(tx,ty);
}
}
}
int main()
{
while(scanf("%d%d%d%d",&r,&c,&click_x,&click_y),r)
{
total=0;
memset(flag,0,sizeof(flag));
memset(maps,'.',sizeof(maps));
for(int i=1; i<=r; i++)
{
scanf("%s",maps[i]+1);
maps[i][c+1]='.';
}
dfs(click_x,click_y);
printf("%d\n",total);
}
return 0;
}
版权声明
本文为[追随远方的某R]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_35866893/article/details/124306402
边栏推荐
- Valentina Studio Pro for Mac(mac数据库管理软件)
- [Ethernet switching security] - explanation of port isolation operation principle and two-layer isolation and three-layer communication example configuration
- 2022年电工(初级)考试题库及答案
- Laravel EXECL import time becomes digital
- ECS uses FRP to map Apache on the local (win10 / win11) intranet to the extranet
- Web development related libraries or software
- 【知行】浅谈Switch与If
- openfeign调用时传递文件
- PHP infinite classification (recursive)
- Mysql双主双从+Atlas数据测试
猜你喜欢

在Win10系统中用mimikatz抓取明文密码

Mysql双主双从+Atlas数据测试

IIC bus design ① - IIC communication protocol
![[PROJECT] small hat takeout (V)](/img/63/fcbb5a748fe0442443a4784dee5ae4.png)
[PROJECT] small hat takeout (V)

matlab画散点图

Install the go plug-in in vscode and configure the go environment to run go

sys. stdin. ReadLine and readlines and input ()

2022年R2移动式压力容器充装考试题模拟考试题库及模拟考试

Implementation and application of STM32 system and custom bootloader

MongoDB 实验——数据备份和恢复和数据库优化
随机推荐
Music download and other file names have become the same name solution
343. Find the product of decomposed integers and maximize integer break
Php article keyword replacement class
2022 R2 mobile pressure vessel filling test question simulation test question bank and simulation test
Div Click to collapse
Apache solr 远程代码执行漏洞(CVE-2019-0193)复现
go语言中的读写锁以及协程通信
The fifth stop is the hometown of Confucius and Mencius ------- the shortest path to the maze
An entity class maps a field
强到离谱,Transformer为何能闯入CV界秒杀CNN?
[2022dasctf x Su] Web replay of March spring challenge
EF de duplication operation
Zephyr IOT operating system column summary
MongoDB 实验——数据备份和恢复和数据库优化
[Ethernet switching security] - explanation of port isolation operation principle and two-layer isolation and three-layer communication example configuration
Picture material free material picture material website picture material where to find some picture material download the purpose of picture material picture material product picture material website
Sword finger offer day22 bit operation (medium)
Pycharm's latest method of importing PIL Library
2022年电工(初级)考试题库及答案
[Ethernet switching security] - port security and MAC address drift prevention and detection