QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#527873 | #2214. Link Cut Digraph | XY_Eleven | AC ✓ | 446ms | 71244kb | C++23 | 6.2kb | 2024-08-22 21:14:56 | 2024-08-22 21:14:57 |
Judging History
answer
#include <bits/stdc++.h>
// #include <bits/extc++.h>
// using namespace __gnu_pbds;
using namespace std;
//#pragma GCC optimize(3)
#define DB double
#define LL long long
#define ULL unsigned long long
#define in128 __int128
#define cint const int
#define cLL const LL
#define For(z,e1,e2) for(int z=(e1);z<=(e2);z++)
#define Rof(z,e1,e2) for(int z=(e2);z>=(e1);z--)
#define For_(z,e1,e2) for(int z=(e1);z<(e2);z++)
#define Rof_(z,e1,e2) for(int z=(e2);z>(e1);z--)
#define inint(e) scanf("%d",&e)
#define inll(e) scanf("%lld",&e)
#define inpr(e1,e2) scanf("%d%d",&e1,&e2)
#define in3(e1,e2,e3) scanf("%d%d%d",&e1,&e2,&e3)
#define outint(e) printf("%d\n",e)
#define outint_(e) printf("%d%c",e," \n"[i==n])
#define outint2_(e,e1,e2) printf("%d%c",e," \n"[(e1)==(e2)])
#define outll(e) printf("%lld\n",e)
#define outll_(e) printf("%lld%c",e," \n"[i==n])
#define outll2_(e,e1,e2) printf("%lld%c",e," \n"[(e1)==(e2)])
#define exc(e) if(e) continue
#define stop(e) if(e) break
#define ret(e) if(e) return
#define ll(e) (1ll*(e))
#define pb push_back
#define ft first
#define sc second
#define pii pair<int,int>
#define pli pair<LL,int>
#define vct vector
#define clean(e) while(!e.empty()) e.pop()
#define all(ev) ev.begin(),ev.end()
#define sz(ev) ((int)ev.size())
#define debug(x) printf("%s=%d\n",#x,x)
#define x0 __xx00__
#define y1 __yy11__
#define ffo fflush(stdout)
cLL mod=998244353,G=404;
// cLL mod[2]={1686688681ll,1666888681ll},base[2]={166686661ll,188868881ll};
template <typename Type> void get_min(Type &w1,const Type w2) { if(w2<w1) w1=w2; } template <typename Type> void get_max(Type &w1,const Type w2) { if(w2>w1) w1=w2; }
template <typename Type> Type up_div(Type w1,Type w2) { return (w1/w2+(w1%w2?1:0)); }
template <typename Type> Type gcd(Type X_,Type Y_) { Type R_=X_%Y_; while(R_) { X_=Y_; Y_=R_; R_=X_%Y_; } return Y_; } template <typename Type> Type lcm(Type X_,Type Y_) { return (X_/gcd(X_,Y_)*Y_); }
template <typename Type> Type md(Type w1,const Type w2=mod) { w1%=w2; if(w1<0) w1+=w2; return w1; } template <typename Type> Type md_(Type w1,const Type w2=mod) { w1%=w2; if(w1<=0) w1+=w2; return w1; }
void ex_gcd(LL &X_,LL &Y_,LL A_,LL B_) { if(!B_) { X_=1ll; Y_=0ll; return ; } ex_gcd(Y_,X_,B_,A_%B_); X_=md(X_,B_); Y_=(1ll-X_*A_)/B_; } LL inv(LL A_,LL B_=mod) { LL X_=0ll,Y_=0ll; ex_gcd(X_,Y_,A_,B_); return X_; }
template <typename Type> void add(Type &w1,const Type w2,const Type M_=mod) { w1=md(w1+w2,M_); } void mul(LL &w1,cLL w2,cLL M_=mod) { w1=md(w1*md(w2,M_),M_); } template <typename Type> Type pw(Type X_,Type Y_,Type M_=mod) { Type S_=1; while(Y_) { if(Y_&1) mul(S_,X_,M_); Y_>>=1; mul(X_,X_,M_); } return S_; }
template <typename Type> Type bk(vector <Type> &V_) { auto T_=V_.back(); V_.pop_back(); return T_; } template <typename Type> Type tp(stack <Type> &V_) { auto T_=V_.top(); V_.pop(); return T_; } template <typename Type> Type frt(queue <Type> &V_) { auto T_=V_.front(); V_.pop(); return T_; }
template <typename Type> Type bg(set <Type> &V_) { auto T_=*V_.begin(); V_.erase(V_.begin()); return T_; } template <typename Type> Type bk(set <Type> &V_) { auto T_=*prev(V_.end()); V_.erase(*prev(V_.end())); return T_; }
mt19937 gen(time(NULL)); int rd() { return abs((int)gen()); }
int rnd(int l,int r) { return rd()%(r-l+1)+l; }
void main_init()
{
}
cint N=1.02e5,M=2.6e5;
LL ans[M],h[N];
void work(int l,int r,vct <int> siz,vct <pair<pii,int>> edge,LL sum)
{
int n=sz(siz)-1;
{
vct <bool> vis(n+1); For(i,0,n) vis[i]=false;
for(auto [p2,id]:edge)
vis[p2.ft]=vis[p2.sc]=true;
vct <int> fin(n+1);
int n2=0;
For(i,1,n)
if(vis[i])
{
fin[i]=++n2;
siz[n2]=siz[i];
}
else sum+=h[siz[i]];
n=n2; siz.resize(n+1);
for(auto &i:edge) i.ft.ft=fin[i.ft.ft],i.ft.sc=fin[i.ft.sc];
// printf("[%d,%d]:%lld\n",l,r,sum);
// For(i,1,n) outint_(siz[i]);
// for(auto [p2,id]:edge) printf("%d,%d\n",p2.ft,p2.sc);
if(l>r) return (void)(ans[l]=sum);
}
int mid=l+r>>1;
vct <int> co(n+1),siz2(1); siz2[0]=0;
{
vct <vct<int>> v(n+1);
for(auto [p2,id]:edge)
{
exc(id>mid);
auto [x,y]=p2;
// printf("%d&%d\n",x,y);
v[x].pb(y);
}
vct <int> dfn(n+1),low(n+1);
vct <bool> ins(n+1);
For(i,0,n) dfn[i]=low[i]=co[i]=0,ins[i]=false;
stack <int> st;
int id=0,tot=0;
auto dfs=[&](auto &&self,int p)->void
{
// printf("p=%d\n",p);
dfn[p]=low[p]=++id;
st.push(p),ins[p]=true;
for(auto i:v[p])
{
if(!dfn[i])
{
// printf("%d->%d\n",p,i);
self(self,i);
get_min(low[p],low[i]);
}
else if(ins[i])
get_min(low[p],dfn[i]);
}
ret(dfn[p]!=low[p]);
tot++; siz2.pb(0);
while(true)
{
int t=tp(st); ins[t]=false;
co[t]=tot; siz2[tot]+=siz[t];
stop(t==p);
}
};
For(i,1,n) if(!dfn[i]) dfs(dfs,i);
// For(i,1,n) outint_(co[i]);
}
if(l>=r) return work(l,l-1,siz2,{},sum);
vct <pair<pii,int>> edge1,edge2;
for(auto [p2,id]:edge)
if(co[p2.ft]==co[p2.sc]) edge1.pb({p2,id});
else edge2.pb({{co[p2.ft],co[p2.sc]},id});
work(l,mid,siz,edge1,sum);
work(mid+1,r,siz2,edge2,sum);
}
void main_solve()
{
int n,m; inpr(n,m);
For(i,1,n) h[i]=1ll*i*(i-1)/2ll;
vct <int> siz(n+1);
siz[0]=0; For(i,1,n) siz[i]=1;
vct <pair<pii,int> > edge;
For(i,1,m)
{
int x,y; inpr(x,y);
edge.pb({{x,y},i});
}
work(1,m,siz,edge,0ll);
For(i,1,m) outll(ans[i]);
}
int main()
{
// ios::sync_with_stdio(0); cin.tie(0);
// srand(time(NULL));
main_init();
// int _; inint(_); For(__,1,_) // T>1 ?
// printf("\n------------\n\n"),
main_solve();
return 0;
}
/*
4 7
3 4
4 3
1 2
4 1
1 4
3 2
2 4
*/
Details
Test #1:
score: 100
Accepted
time: 409ms
memory: 53684kb
Test #2:
score: 0
Accepted
time: 396ms
memory: 52872kb
Test #3:
score: 0
Accepted
time: 403ms
memory: 53332kb
Test #4:
score: 0
Accepted
time: 386ms
memory: 63932kb
Test #5:
score: 0
Accepted
time: 391ms
memory: 52384kb
Test #6:
score: 0
Accepted
time: 386ms
memory: 52280kb
Test #7:
score: 0
Accepted
time: 398ms
memory: 51916kb
Test #8:
score: 0
Accepted
time: 394ms
memory: 52632kb
Test #9:
score: 0
Accepted
time: 347ms
memory: 71244kb
Test #10:
score: 0
Accepted
time: 402ms
memory: 52724kb
Test #11:
score: 0
Accepted
time: 446ms
memory: 52080kb
Test #12:
score: 0
Accepted
time: 389ms
memory: 52968kb
Test #13:
score: 0
Accepted
time: 353ms
memory: 70444kb
Test #14:
score: 0
Accepted
time: 360ms
memory: 69776kb
Test #15:
score: 0
Accepted
time: 239ms
memory: 41096kb
Test #16:
score: 0
Accepted
time: 366ms
memory: 37256kb
Test #17:
score: 0
Accepted
time: 368ms
memory: 37948kb
Test #18:
score: 0
Accepted
time: 363ms
memory: 38460kb
Test #19:
score: 0
Accepted
time: 403ms
memory: 53884kb
Test #20:
score: 0
Accepted
time: 389ms
memory: 59744kb
Test #21:
score: 0
Accepted
time: 399ms
memory: 60184kb
Test #22:
score: 0
Accepted
time: 397ms
memory: 59196kb
Test #23:
score: 0
Accepted
time: 390ms
memory: 60768kb
Test #24:
score: 0
Accepted
time: 374ms
memory: 60280kb
Test #25:
score: 0
Accepted
time: 386ms
memory: 61364kb
Test #26:
score: 0
Accepted
time: 380ms
memory: 59684kb
Test #27:
score: 0
Accepted
time: 399ms
memory: 55256kb