QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#20481 | #2214. Link Cut Digraph | 2018ty22# | WA | 307ms | 11904kb | C++20 | 2.1kb | 2022-02-16 11:28:13 | 2022-05-03 10:08:35 |
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[250001],siz[250001];
int head[250001],s[250001],nxt[250001];
int dfn[250001],lst[250001],tot;
int st[250001],top;
int p[250001],cp[250001];
bool vis[250001];
ll ans;
bool cmp(const node x,const 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)
{
dfn[x]=lst[x]=++tot;
st[++top]=x;vis[x]=1;
for(int i=head[x];i;i=nxt[i])
if(!dfn[s[i]])
tarjan(s[i]),lst[x]=min(lst[x],lst[s[i]]);
else if(vis[s[i]])
lst[x]=min(lst[x],lst[s[i]]);
if(lst[x]==dfn[x])
{
while(st[top]!=x)
fa[st[top]]=x,vis[st[top]]=0,--top;
vis[x]=0,--top;
}
}
void solve(const int&l,const int&r,const int&l1,const int&r1)
{
if(l>=r||l1>r1)return;
#define xx b[p[i]].x
#define yy b[p[i]].y
// #define FoR for(int i=l1;i<=r1&&b[p[i]].t<=mid;++i)
#define FoR for(int i=l1;i<=r1;++i)if(b[p[i]].t<=mid)
int mid=l+r>>1,num=0;tot=top=0;
for(int i=l1;i<=r1;++i)
{
// if(i>l1&&b[i].t<b[i-1].t)xx=0x7fffffff,yy=-0x7ffffff;
fa[xx]=fa[yy]=0,vis[xx]=vis[yy]=0;
head[xx]=head[yy]=0;
dfn[xx]=dfn[yy]=0;
}
FoR{
s[++num]=yy;
nxt[num]=head[xx];
head[xx]=num;
}
FoR
if(!dfn[xx])
tarjan(xx);
int n1=l1-1,n2=0;
for(int i=l1;i<=r1;++i)
if(b[p[i]].t<=mid&&find(xx)==find(yy))
p[++n1]=p[i];
else
{
xx=find(xx);yy=find(yy);
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);
}
}
详细
Test #1:
score: 0
Wrong Answer
time: 307ms
memory: 11904kb