QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#20415 | #2214. Link Cut Digraph | The_Nobody# | AC ✓ | 460ms | 38696kb | C++14 | 3.3kb | 2022-02-16 08:20:38 | 2022-05-03 09:49:31 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define Il inline
#define Re register
#define mem(u,v) memset(u,v,sizeof(u))
#define rep(i,a,b) for(Re ll i=(a),KKK##i=(b);i<=KKK##i;i++)
#define drep(i,a,b) for(Re ll i=(a),KKK##i=(b);i>=KKK##i;i--)
#define go(u) for(ll i=head[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define _go(u) for(ll i=Head[u],v=E[i].to;i;i=E[i].nxt,v=E[i].to)
#define __go(u) for(ll i=Head[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writesp(x) write(x),putchar(' ')
#define writeln(x) write(x),puts("")
using namespace std;
Il ll read(){ll sum=0,f=0;char ch=getchar();for(;!isdigit(ch);ch=getchar())f|=(ch=='-');for(;isdigit(ch);ch=getchar())sum=((sum<<1)+(sum<<3)+(ch^48));return f?-sum:sum;}
void write(const ll x){if(x<0){putchar('-');write(-x);return;}if(x>9)write(x/10);putchar(x%10+'0');}
char getc(){char c=getchar();while(!isdigit(c))c=getchar();return c;}
#define N 550000
ll n,m,fa[N],h[N],ans[N],tot,head[N],t,low[N],dfn[N],cnt,s[N],top,P[N],b[N],sz[N],ANS;bool c[N];
struct node{ll to,nxt;}e[N*2];
void add(ll f,ll to){e[++tot].to=to;e[tot].nxt=head[f];head[f]=tot;}
struct abc{ll x,y,i;}q[N],q1[N],q2[N];
struct nd{ll x,y,h;}S[N];
ll getf(ll x){for(;x!=fa[x];)x=fa[x];return x;}
bool cmp(abc a,abc b){return a.i<b.i;}
map<pair<ll,ll>,ll>mp;
void merge(ll x,ll y){
x=getf(x),y=getf(y);
if(x==y)return;
if(h[x]>h[y])swap(x,y);
S[++t]=(nd){x,y,h[x]==h[y]};
fa[x]=y;if(h[x]==h[y])h[y]++;
}
void del(ll n){
ll x=S[n].x,y=S[n].y;
h[y]-=S[n].h;fa[x]=x;
}
void tar(ll u,ll fa){
low[u]=dfn[u]=++cnt;s[++top]=u;c[u]=1;
go(u){
if(!dfn[v])tar(v,u),low[u]=min(low[u],low[v]);
else if(c[v])low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
do{
c[s[top]]=0;
merge(u,s[top]);
top--;
}while(s[top+1]!=u);
}
}
void solve(ll x,ll y,ll l,ll r){
if(x>y)return;
// cout<<x<<" "<<y<<' '<<l<<' '<<r<<endl;
// rep(i,x,y)cout<<q[i].x<<' '<<q[i].y<<endl;
ll mid=(l+r)>>1;
// cout<<mid<<endl;
if(l==r){rep(i,x,y)ans[q[i].i]=l;return;}
ll tmp=t,kk=0,t1=0,t2=0;
rep(i,x,y)if(q[i].i<=mid){
ll x=getf(q[i].x),y=getf(q[i].y);
P[++kk]=x,P[++kk]=y;add(x,y);
// cout<<"AD"<<x<<" "<<y<<endl;
}
rep(i,1,kk)if(!dfn[P[i]])tar(P[i],0);
rep(i,1,kk)head[P[i]]=low[P[i]]=dfn[P[i]]=0;
tot=0;cnt=0;top=0;
rep(i,x,y){
if(q[i].i<=mid&&getf(q[i].x)==getf(q[i].y))q1[++t1]=q[i];
else q2[++t2]=q[i];
}
rep(i,1,t1)q[i+x-1]=q1[i];
rep(i,1,t2)q[i+x+t1-1]=q2[i];
solve(x+t1,y,mid+1,r);
while(t>tmp)del(t--);
solve(x,x+t1-1,l,mid);
}
ll _getf(ll x){return fa[x]==x?x:fa[x]=getf(fa[x]);}
ll C(ll x){return x*(x-1)/2;}
void _merge(ll x,ll y){
x=_getf(x),y=_getf(y);
if(x==y)return;
ANS-=C(sz[y])+C(sz[x]);
ANS+=C(sz[x]+sz[y]);
sz[y]+=sz[x];fa[x]=y;
}
int main(){
n=read(),m=read();
rep(i,1,n)fa[i]=i,h[i]=1;
rep(i,1,m)q[i].x=read(),q[i].y=read(),q[i].i=i;
solve(1,m,0,m+1);
// rep(i,1,m)cout<<ans[i]<<' ';puts("");
sort(q+1,q+m+1,cmp);
rep(i,1,m)q[i].i=ans[i];
sort(q+1,q+m+1,cmp);
ll now=1;
rep(i,1,n)fa[i]=i,sz[i]=1;
rep(i,1,m){
while(now<=m&&q[now].i<=i)_merge(q[now].x,q[now].y),now++;
writeln(ANS);
}
}
/*
大致操作是先把小于mid的建边,然后跑这部分的tarjan,用并查集维护强连通分量,每次在这上面连边走右边区间,然后走左边区间
*/
詳細信息
Test #1:
score: 100
Accepted
time: 431ms
memory: 35492kb
Test #2:
score: 0
Accepted
time: 435ms
memory: 38676kb
Test #3:
score: 0
Accepted
time: 441ms
memory: 35948kb
Test #4:
score: 0
Accepted
time: 401ms
memory: 34656kb
Test #5:
score: 0
Accepted
time: 458ms
memory: 38524kb
Test #6:
score: 0
Accepted
time: 444ms
memory: 35200kb
Test #7:
score: 0
Accepted
time: 422ms
memory: 38696kb
Test #8:
score: 0
Accepted
time: 439ms
memory: 35980kb
Test #9:
score: 0
Accepted
time: 345ms
memory: 32516kb
Test #10:
score: 0
Accepted
time: 430ms
memory: 35716kb
Test #11:
score: 0
Accepted
time: 438ms
memory: 38420kb
Test #12:
score: 0
Accepted
time: 460ms
memory: 35948kb
Test #13:
score: 0
Accepted
time: 344ms
memory: 31128kb
Test #14:
score: 0
Accepted
time: 343ms
memory: 33840kb
Test #15:
score: 0
Accepted
time: 171ms
memory: 30096kb
Test #16:
score: 0
Accepted
time: 381ms
memory: 34564kb
Test #17:
score: 0
Accepted
time: 387ms
memory: 34756kb
Test #18:
score: 0
Accepted
time: 420ms
memory: 33652kb
Test #19:
score: 0
Accepted
time: 439ms
memory: 35708kb
Test #20:
score: 0
Accepted
time: 402ms
memory: 33348kb
Test #21:
score: 0
Accepted
time: 377ms
memory: 30848kb
Test #22:
score: 0
Accepted
time: 435ms
memory: 30856kb
Test #23:
score: 0
Accepted
time: 432ms
memory: 30872kb
Test #24:
score: 0
Accepted
time: 394ms
memory: 30828kb
Test #25:
score: 0
Accepted
time: 391ms
memory: 30880kb
Test #26:
score: 0
Accepted
time: 404ms
memory: 30980kb
Test #27:
score: 0
Accepted
time: 407ms
memory: 31476kb