QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#18441 | #2214. Link Cut Digraph | guobo# | AC ✓ | 281ms | 38432kb | C++ | 2.6kb | 2022-01-19 19:37:12 | 2022-05-06 01:32:58 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=255555,mod=998244353;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
char ch=getchar();ll x=0,f=0;
while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
return f?-x:x;
}
inline int qpow(int a,int b){
int ans=1;
for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) ans=1ll*ans*a%mod;
return ans;
}
int n,m,tim[maxn],id[maxn],cnt,el,head[maxn],to[maxn],nxt[maxn],dfn[maxn],low[maxn],dfc,at[maxn],scc,stk[maxn],tp,fa[maxn],sz[maxn];
bool ins[maxn];
ll sum;
struct edge{
int u,v,id;
bool operator<(const edge &e)const{return tim[id]<tim[e.id];}
};
inline void add(int u,int v){
to[++el]=v;nxt[el]=head[u];head[u]=el;
}
void dfs(int u){
dfn[u]=low[u]=++dfc;
stk[++tp]=u;
ins[u]=true;
for(int i=head[u];i;i=nxt[i]){
int v=to[i];
if(!dfn[v]) dfs(v),low[u]=min(low[u],low[v]);
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
scc++;
do{
ins[stk[tp]]=false;
at[stk[tp]]=scc;
}while(stk[tp--]!=u);
}
}
void solve(vector<edge> &e,int l,int r){
if(l==r){
FOR(i,0,(int)e.size()-1) tim[e[i].id]=l;
return;
}
int mid=(l+r)>>1;
FOR(i,0,(int)e.size()-1) if(e[i].id<=mid){
int u=e[i].u,v=e[i].v;
if(!id[u]) id[u]=++cnt;
if(!id[v]) id[v]=++cnt;
add(id[u],id[v]);
}
FOR(i,1,cnt) if(!dfn[i]) dfs(i);
vector<edge> e1,e2;
FOR(i,0,(int)e.size()-1){
int u=e[i].u,v=e[i].v;
if(!id[u]) id[u]=++cnt,at[id[u]]=++scc;
if(!id[v]) id[v]=++cnt,at[id[v]]=++scc;
if(at[id[u]]==at[id[v]] && e[i].id<=mid) e1.PB(e[i]);
else e2.PB((edge){at[id[u]],at[id[v]],e[i].id});
}
FOR(i,1,cnt) head[i]=dfn[i]=low[i]=at[i]=0;
FOR(i,1,el) to[i]=nxt[i]=0;
FOR(i,0,(int)e.size()-1){
int u=e[i].u,v=e[i].v;
id[u]=id[v]=0;
}
cnt=el=scc=dfc=0;
solve(e1,l,mid);
solve(e2,mid+1,r);
}
inline int getfa(int x){
return x==fa[x]?x:fa[x]=getfa(fa[x]);
}
inline void unite(int x,int y){
x=getfa(x);y=getfa(y);
if(x==y) return;
fa[x]=y;
sum+=1ll*sz[x]*sz[y];
sz[y]+=sz[x];
}
int main(){
n=read();m=read();
vector<edge> e,ee;
FOR(i,1,m){
int u=read(),v=read();
e.PB((edge){u,v,i});
}
ee=e;
solve(e,1,m+1);
sort(ee.begin(),ee.end());
FOR(i,1,n) fa[i]=i,sz[i]=1;
int cur=0;
FOR(i,1,m){
while(cur<(int)ee.size() && tim[ee[cur].id]<=i){
unite(ee[cur].u,ee[cur].v);
cur++;
}
printf("%lld\n",sum);
}
}
Details
Test #1:
score: 100
Accepted
time: 227ms
memory: 33844kb
Test #2:
score: 0
Accepted
time: 234ms
memory: 33764kb
Test #3:
score: 0
Accepted
time: 245ms
memory: 33380kb
Test #4:
score: 0
Accepted
time: 243ms
memory: 37416kb
Test #5:
score: 0
Accepted
time: 220ms
memory: 33852kb
Test #6:
score: 0
Accepted
time: 245ms
memory: 34564kb
Test #7:
score: 0
Accepted
time: 226ms
memory: 34752kb
Test #8:
score: 0
Accepted
time: 281ms
memory: 33924kb
Test #9:
score: 0
Accepted
time: 232ms
memory: 38260kb
Test #10:
score: 0
Accepted
time: 218ms
memory: 33664kb
Test #11:
score: 0
Accepted
time: 236ms
memory: 33616kb
Test #12:
score: 0
Accepted
time: 217ms
memory: 33440kb
Test #13:
score: 0
Accepted
time: 217ms
memory: 38108kb
Test #14:
score: 0
Accepted
time: 219ms
memory: 38432kb
Test #15:
score: 0
Accepted
time: 137ms
memory: 22840kb
Test #16:
score: 0
Accepted
time: 230ms
memory: 27884kb
Test #17:
score: 0
Accepted
time: 199ms
memory: 27764kb
Test #18:
score: 0
Accepted
time: 210ms
memory: 28016kb
Test #19:
score: 0
Accepted
time: 197ms
memory: 34244kb
Test #20:
score: 0
Accepted
time: 234ms
memory: 35396kb
Test #21:
score: 0
Accepted
time: 226ms
memory: 36692kb
Test #22:
score: 0
Accepted
time: 244ms
memory: 35712kb
Test #23:
score: 0
Accepted
time: 273ms
memory: 36004kb
Test #24:
score: 0
Accepted
time: 247ms
memory: 35264kb
Test #25:
score: 0
Accepted
time: 241ms
memory: 35908kb
Test #26:
score: 0
Accepted
time: 228ms
memory: 34852kb
Test #27:
score: 0
Accepted
time: 253ms
memory: 33416kb