# 正题

## 题目大意

$$1\leq n\leq 10^4,1\leq m\leq 10^6$$

## 解题思路

$\sum_{i=3}^n\left(1-\frac{1}{\sqrt n}\right)^i<\frac{1}{1-\left(1-\frac{1}{\sqrt n}\right)}=\sqrt n$

## code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const int N=1e4+10;
struct node{
int to,next,w;
}a[N<<1];
int n,m,tot,cnt,ls[N],dep[N],pos[N];
int las[N],vis[N],dis[N],len[N],c[N];
vector<int> G[N];
void addl(int x,int y,int w){
a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].w=w;
a[++tot].to=x;a[tot].next=ls[y];ls[y]=tot;a[tot].w=w;
return;
}
void dfs(int x,int fa){
dep[x]=dep[fa]+1;las[x]=fa;int num=0;
for(int i=0;i<G[x].size();i++){
int y=G[x][i];
if(y==fa)continue;
if(dep[y]){
if(dep[y]>dep[x])continue;
if(!pos[x])pos[x]=x;
if(!pos[y])pos[y]=y;
addl(pos[x],pos[y],1);
}
else{
dfs(y,x);
if(pos[y])num=num?-1:pos[y];
}
}
if(!pos[x]&&num<0)pos[x]=x;
if(pos[x]){
for(int i=0;i<G[x].size();i++){
int y=G[x][i];
if(y==fa)continue;
if(pos[y]&&las[y]==x)
addl(pos[x],pos[y],dep[pos[y]]-dep[pos[x]]);
}
}
else pos[x]=num;
return;
}
bool calc(int x,int fa,int t){
if(!dep[x])return 1;vis[x]=t;
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(i==fa||dep[y]>0||vis[y]==t)continue;
bool flag=calc(y,i^1,t);
if(flag)return 1;
}
return 0;
}
void solve(int x,int fa){
if(!calc(x,fa,++cnt))return;
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to,r=dis[x]+a[i].w;
if(i==fa)continue;
if(!dep[y]){
if(!len[r])len[r]=dep[x]+1;
if(len[r]!=dep[x]+1){puts("Yes");exit(0);}
c[r]++;
if(c[r]>len[r]*2){puts("Yes");exit(0);}
}
if(dep[y]>=0)continue;
dep[y]=dep[x]+1;dis[y]=dis[x]+a[i].w;
solve(y,i^1);dep[y]=-1;
}
return;
}
int main()
{
//	freopen("4-01.in","r",stdin);
scanf("%d%d",&n,&m);tot=1;
if(m>n+sqrt(n)*2.0)return puts("Yes")&0;
for(int i=1,x,y;i<=m;i++){
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
for(int i=1;i<=n;i++)
if(!dep[i])dfs(i,0);
memset(dep,-1,sizeof(dep));
for(int i=1;i<=n;i++)
if(pos[i]==i)
dep[i]=0,dis[i]=0,solve(i,0),dep[i]=-1;
puts("No");
return 0;
}

posted @ 2022-03-29 18:30  QuantAsk  阅读(92)  评论(0编辑  收藏  举报