QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#20376 | #2214. Link Cut Digraph | 2018ty22# | TL | 0ms | 0kb | C++14 | 2.1kb | 2022-02-15 21:56:07 | 2022-05-03 09:46:11 |
Judging History
answer
#include<algorithm>
#include<cstdio>
#define ll long long
#define min(a,b) (a<b?a:b)
using namespace std;
struct node{
int x,y,t;
}b[250001];
int n,m,fa[100001],siz[100001];
int head[100001],s[100001],nxt[100001];
int dfn[100001],lst[100001],tot;
int st[100001],top;
int p[250001],cp[250001];
ll ans;
bool cmp(node x,node y){return x.t<y.t;}
inline int find(int x)
{
if(fa[x])return fa[x]=find(fa[x]);
return x;
}
void tarjan(const int&x,const int&f1)
{
dfn[x]=lst[x]=++tot;
for(int i=head[x];i;i=nxt[i])
if(i!=f1)
{
if(!dfn[s[i]])tarjan(s[i],i);
lst[x]=min(lst[x],lst[s[i]]);
}
if(lst[x]<dfn[x])st[++top]=x;
else
{
while(top&&lst[st[top]]>=dfn[x])
{
int x1=find(st[top]),x2=find(x);
fa[x1]=x2;--top;
}
}
}
void solve(const int&l,const int&r,const int&l1,const int&r1)
{
if(l==r||l1>r1)return;
int mid=l+r>>1,num=0;tot=0;
for(int i=l1;i<=r1&&b[p[i]].t<=mid;++i)
{
head[b[p[i]].x]=head[b[p[i]].y]=0;
dfn[b[p[i]].x]=dfn[b[p[i]].y]=0;
fa[b[p[i]].x]=fa[b[p[i]].y]=0;
}
for(int i=l1;i<=r1&&b[p[i]].t<=mid;++i)
s[++num]=b[p[i]].y,
nxt[num]=head[b[p[i]].x],
head[b[p[i]].x]=num;
for(int i=l1;i<=r1&&b[p[i]].t<=mid;++i)
if(!dfn[b[p[i]].x])
tarjan(b[p[i]].x,-1);
int n1=l1-1,n2=0;
for(int i=l1;i<=r1;++i)
if(b[p[i]].t<=mid&&find(b[p[i]].x)==find(b[p[i]].y))
p[++n1]=p[i];
else
{
b[p[i]].x=find(b[p[i]].x);
b[p[i]].y=find(b[p[i]].y);
if(b[p[i]].t<=mid)b[p[i]].t=mid+1;
cp[++n2]=p[i];
}
for(int i=1;i<=n2;++i)p[n1+i]=cp[i];
solve(l,mid,l1,n1);
solve(mid+1,r,n1+1,r1);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)
scanf("%d%d",&b[i].x,&b[i].y),b[i].t=i,p[i]=i;
solve(1,m+1,1,m);
for(int i=1;i<=n;++i)fa[i]=0,siz[i]=1;
sort(b+1,b+m+1,cmp);
for(int i=1,j=1;i<=m;++i)
{
while(j<=m&&b[j].t<=i)
{
int x=find(b[j].x),y=find(b[j].y);
if(x!=y)
{
ans+=(ll)((siz[x]+siz[y])*(siz[x]+siz[y]-1))-(ll)siz[x]*(siz[x]-1)-(ll)siz[y]*(siz[y]-1);
siz[x]+=siz[y];
fa[y]=x;
}
++j;
}
printf("%lld\n",ans>>1ll);
}
}
Details
Test #1:
score: 0
Time Limit Exceeded