QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#20266 | #2214. Link Cut Digraph | 19ty02# | WA | 357ms | 72584kb | C++20 | 2.0kb | 2022-02-15 10:22:33 | 2022-05-03 09:25:52 |
Judging History
answer
#include<cstdio>
#include<vector>
#include<iostream>
#define LL long long
using namespace std;
const int N=1e5+10,M=3e5+10;
int n,m;
struct edge{
int id,u,v;
}e[M];
vector<int> ae[M];
int h[N],tot;
struct tar{
int to,nxt;
}tr[M];
void add(int u,int v){
tr[++tot]=(tar){v,h[u]};
h[u]=tot;
}
int sta[N],tp,scc[N],dfn[N],low[N],_dfn,col;
void tarjan(int u){
dfn[u]=low[u]=++_dfn;
sta[++tp]=u;
for(int i=h[u];i;i=tr[i].nxt){
int v=tr[i].to;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}else{
if(!scc[v])low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
col++;
while(sta[tp]!=u)scc[sta[tp--]]=col;
scc[sta[tp--]]=col;
}
}
void solve(int l,int r,vector<int> x){
if(l==r){
for(int i=0;i<x.size();i++){
ae[l].push_back(x[i]);
}
return;
}
int mid=(l+r)>>1;
vector<int> d;
for(int i=0;i<x.size();i++){
int j=x[i];
if(e[j].id>mid)break;
add(e[j].u,e[j].v);
d.push_back(e[j].u);
d.push_back(e[j].v);
}
for(int i=0;i<d.size();i++){
int j=d[i];
if(!scc[j])tarjan(j);
}
vector<int> x1,x2;
for(int i=0;i<x.size();i++){
int j=x[i];
if(e[j].id<=mid&&scc[e[j].u]==scc[e[j].v])x1.push_back(j);
else x2.push_back(j);
}
for(int i=0;i<d.size();i++){
int j=d[i];
h[j]=dfn[j]=low[j]=scc[j]=0;
}
tot=tp=_dfn=col=0;
d.clear();
solve(l,mid,x1);
solve(mid+1,r,x2);
}
int fa[N],sz[N];
LL ans;
int Find(int x){
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
void Merge(int u,int v){
int fu=Find(u),fv=Find(v);
if(fu!=fv){
ans+=1ll*sz[fu]*sz[fv];
fa[fv]=fu;sz[fu]+=sz[fv];
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
e[i].id=i;
scanf("%d%d",&e[i].u,&e[i].v);
}
vector<int> x;
for(int i=1;i<=m;i++)x.push_back(i);
solve(1,m+1,x);
for(int i=1;i<=n;i++)fa[i]=i,sz[i]=1;
for(int i=1;i<=m;i++){
for(int j=0;j<ae[i].size();j++){
int k=ae[i][j];
Merge(e[k].u,e[k].v);
// printf("edge:%d u:%d v=%d on time %d\n",k,e[k].u,e[k].v,i);
}
printf("%lld\n",ans);
}
}
Details
Test #1:
score: 0
Wrong Answer
time: 357ms
memory: 72584kb