QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#20062 | #2214. Link Cut Digraph | Appleblue17# | AC ✓ | 497ms | 63444kb | C++20 | 3.3kb | 2022-02-14 17:05:21 | 2022-05-03 08:59:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const long long N=5e5+5,M=5e5+5,W=1e9;
long long n,m;
struct que{
long long u,v,t;
}P[N];
long long ans[N],anss[N];
struct nod{
long long to,nxt;
}e[M*4];
long long head[N],cnt;
void add(long long u,long long v){
e[++cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt;
}
long long fa[N],dep[N];
struct record{
long long x,y,fax,depy;
}rec[N];
long long tail;
long long getf(long long x){
if(fa[x]==x) return x;
return getf(fa[x]);
}
bool merge(long long x,long long y){
long long fx=getf(x),fy=getf(y);
rec[++tail]={0,0,0,0};
if(fx==fy) return 0;
if(dep[fx]>dep[fy]) swap(fx,fy);
rec[tail]={fx,fy,fa[fx],dep[fy]};
if(dep[fx]==dep[fy]) dep[fy]++;
fa[fx]=fy;
return 1;
}
void redo(){
record tmp=rec[tail--];
long long x=tmp.x,y=tmp.y;
fa[x]=tmp.fax;
dep[y]=tmp.depy;
}
long long FA[N],SIZ[N],SUM;
long long GETF(long long x){
if(FA[x]==x) return x;
return FA[x]=GETF(FA[x]);
}
long long cal(long long x){
return 1ll*x*(x-1)/2;
}
bool MERGE(long long x,long long y){
long long fx=GETF(x),fy=GETF(y);
if(fx==fy) return 0;
SUM+=cal(SIZ[fx]+SIZ[fy])-cal(SIZ[fx])-cal(SIZ[fy]);
FA[fy]=fx;
SIZ[fx]+=SIZ[fy];
return 1;
}
//
long long dfn[N],low[N],id;
bool vis[N];
stack <long long> st;
long long reco[N][2],tmpp;
void tarjan(long long u,bool typ){
low[u]=dfn[u]=++id;
vis[u]=1;
st.push(u);
for(long long i=head[u];i;i=e[i].nxt){
long long v=e[i].to;
if(!dfn[v]){
tarjan(v,typ);
low[u]=min(low[u],low[v]);
}
else if(vis[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
while(st.size()){
long long tmp=st.top();
vis[tmp]=0;
st.pop();
if(tmp==u) break;
merge(tmp,st.top());
if(typ) reco[++tmpp][0]=tmp,reco[tmpp][1]=st.top();
}
}
}
//
long long V[N],ct;
que tmpl[N],tmpr[N];
long long ctl,ctr;
void CDQ(long long l,long long r,long long L,long long R){
if(l==r){
for(long long i=L;i<=R;i++) ans[P[i].t]=l;
return ;
}
long long mid=(l+r)>>1;
ct=0;
long long stail=tail;
for(long long i=L;i<=R;i++){
if(P[i].t<=mid){
long long u=getf(P[i].u),v=getf(P[i].v);
V[++ct]=u,V[++ct]=v,add(u,v);
}
}
for(long long i=1;i<=ct;i++){
long long u=V[i];
if(!dfn[u]) tarjan(u,0);
}
ctl=ctr=0;
for(long long i=L;i<=R;i++){
if(getf(P[i].u)==getf(P[i].v)) tmpl[++ctl]=P[i];
else tmpr[++ctr]=P[i];
}
long long MID=L+ctl-1;
for(long long i=1;i<=ctl;i++) P[L+i-1]=tmpl[i];
for(long long i=1;i<=ctr;i++) P[MID+i]=tmpr[i];
id=cnt=0;
for(long long i=1;i<=ct;i++){
long long u=V[i];
head[u]=vis[u]=low[u]=dfn[u]=0;
}
CDQ(mid+1,r,MID+1,R);
while(tail>stail) redo();
CDQ(l,mid,L,MID);
}
//
long long edg[N][2];
vector <long long> H[N];
int main(){
cin>>n>>m;
for(long long i=1;i<=n;i++) fa[i]=FA[i]=i;
for(long long i=1;i<=m;i++){
long long u,v;
cin>>u>>v;
edg[i][0]=u,edg[i][1]=v;
P[i]=(que){u,v,i};
}
memset(ans,-1,sizeof(ans));
CDQ(0,m+1,1,m);
// for(long long i=0;i<=m+1;i++) cout<<ans[i]<<" ";
for(long long i=1;i<=m;i++)
if(ans[i]>=1 && ans[i]<=m)
H[ans[i]].push_back(i);
for(long long i=1;i<=n;i++) SIZ[i]=1;
for(long long i=1;i<=m;i++){
for(long long t=0;t<(long long)H[i].size();t++){
long long x=H[i][t];
MERGE(edg[x][0],edg[x][1]);
}
printf("%lld\n",SUM);
}
}
Details
Test #1:
score: 100
Accepted
time: 449ms
memory: 62616kb
Test #2:
score: 0
Accepted
time: 466ms
memory: 63360kb
Test #3:
score: 0
Accepted
time: 469ms
memory: 62404kb
Test #4:
score: 0
Accepted
time: 425ms
memory: 55072kb
Test #5:
score: 0
Accepted
time: 447ms
memory: 63444kb
Test #6:
score: 0
Accepted
time: 461ms
memory: 62148kb
Test #7:
score: 0
Accepted
time: 448ms
memory: 62220kb
Test #8:
score: 0
Accepted
time: 430ms
memory: 61920kb
Test #9:
score: 0
Accepted
time: 376ms
memory: 56112kb
Test #10:
score: 0
Accepted
time: 453ms
memory: 62684kb
Test #11:
score: 0
Accepted
time: 497ms
memory: 62440kb
Test #12:
score: 0
Accepted
time: 489ms
memory: 61620kb
Test #13:
score: 0
Accepted
time: 415ms
memory: 55500kb
Test #14:
score: 0
Accepted
time: 420ms
memory: 55576kb
Test #15:
score: 0
Accepted
time: 268ms
memory: 58412kb
Test #16:
score: 0
Accepted
time: 399ms
memory: 59748kb
Test #17:
score: 0
Accepted
time: 406ms
memory: 58220kb
Test #18:
score: 0
Accepted
time: 435ms
memory: 58416kb
Test #19:
score: 0
Accepted
time: 467ms
memory: 61184kb
Test #20:
score: 0
Accepted
time: 439ms
memory: 57092kb
Test #21:
score: 0
Accepted
time: 432ms
memory: 57096kb
Test #22:
score: 0
Accepted
time: 437ms
memory: 57632kb
Test #23:
score: 0
Accepted
time: 435ms
memory: 58512kb
Test #24:
score: 0
Accepted
time: 413ms
memory: 58884kb
Test #25:
score: 0
Accepted
time: 436ms
memory: 58896kb
Test #26:
score: 0
Accepted
time: 444ms
memory: 55812kb
Test #27:
score: 0
Accepted
time: 452ms
memory: 57532kb