QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#19974 | #2214. Link Cut Digraph | guobo# | AC ✓ | 253ms | 44888kb | C++11 | 2.6kb | 2022-02-14 14:41:50 | 2022-05-08 01:44:40 |
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: 230ms
memory: 38420kb
Test #2:
score: 0
Accepted
time: 253ms
memory: 39392kb
Test #3:
score: 0
Accepted
time: 211ms
memory: 38140kb
Test #4:
score: 0
Accepted
time: 230ms
memory: 43848kb
Test #5:
score: 0
Accepted
time: 233ms
memory: 38996kb
Test #6:
score: 0
Accepted
time: 230ms
memory: 38172kb
Test #7:
score: 0
Accepted
time: 235ms
memory: 37664kb
Test #8:
score: 0
Accepted
time: 237ms
memory: 38504kb
Test #9:
score: 0
Accepted
time: 199ms
memory: 43628kb
Test #10:
score: 0
Accepted
time: 230ms
memory: 39948kb
Test #11:
score: 0
Accepted
time: 232ms
memory: 40112kb
Test #12:
score: 0
Accepted
time: 203ms
memory: 39484kb
Test #13:
score: 0
Accepted
time: 186ms
memory: 44888kb
Test #14:
score: 0
Accepted
time: 209ms
memory: 43376kb
Test #15:
score: 0
Accepted
time: 138ms
memory: 28176kb
Test #16:
score: 0
Accepted
time: 216ms
memory: 32096kb
Test #17:
score: 0
Accepted
time: 209ms
memory: 31812kb
Test #18:
score: 0
Accepted
time: 206ms
memory: 31816kb
Test #19:
score: 0
Accepted
time: 218ms
memory: 39200kb
Test #20:
score: 0
Accepted
time: 224ms
memory: 41908kb
Test #21:
score: 0
Accepted
time: 213ms
memory: 41032kb
Test #22:
score: 0
Accepted
time: 210ms
memory: 40800kb
Test #23:
score: 0
Accepted
time: 236ms
memory: 42652kb
Test #24:
score: 0
Accepted
time: 238ms
memory: 40552kb
Test #25:
score: 0
Accepted
time: 227ms
memory: 39652kb
Test #26:
score: 0
Accepted
time: 222ms
memory: 39072kb
Test #27:
score: 0
Accepted
time: 219ms
memory: 37540kb