QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#20372 | #2214. Link Cut Digraph | 2018ty22# | TL | 0ms | 0kb | C++20 | 2.1kb | 2022-02-15 21:41:10 | 2022-05-03 09:45:53 |
Judging History
answer
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#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;
ll ans;
bool cmp(node x,node y){return x.t<y.t;}
int find(int x)
{
if(fa[x])return fa[x]=find(fa[x]);
return x;
}
void tarjan(int x,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(lst[st[top]]>=dfn[x])
{
int x1=find(st[top]),x2=find(x);
fa[x1]=x2;--top;
}
}
}
void solve(int l,int r,vector<int>&p)
{
if(l==r||p.empty())return;
vector<int>::iterator it;
int mid=l+r>>1,num=0;tot=0;
for(it=p.begin();it!=p.end()&&b[*it].t<=mid;++it)
{
head[b[*it].x]=head[b[*it].y]=0;
dfn[b[*it].x]=dfn[b[*it].y]=0;
fa[b[*it].x]=fa[b[*it].y]=0;
}
for(it=p.begin();it!=p.end()&&b[*it].t<=mid;++it)
s[++num]=b[*it].y,
nxt[num]=head[b[*it].x],
head[b[*it].x]=num;
for(it=p.begin();it!=p.end()&&b[*it].t<=mid;++it)
if(!dfn[b[*it].x])
tarjan(b[*it].x,-1);
vector<int>x1,x2;
for(it=p.begin();it!=p.end();++it)
if(b[*it].t<=mid&&find(b[*it].x)==find(b[*it].y))
x1.push_back(*it);
else
{
b[*it].x=find(b[*it].x);
b[*it].y=find(b[*it].y);
if(b[*it].t<=mid)b[*it].t=mid+1;
x2.push_back(*it);
}
p.clear();
solve(l,mid,x1);
solve(mid+1,r,x2);
}
int main()
{
scanf("%d%d",&n,&m);
vector<int>p;p.reserve(m);
for(int i=1;i<=m;++i)
scanf("%d%d",&b[i].x,&b[i].y),b[i].t=i,p.push_back(i);
solve(1,m,p);
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
Time Limit Exceeded