QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#61477 | #2214. Link Cut Digraph | team12345 | WA | 333ms | 39828kb | C++17 | 2.3kb | 2022-11-13 09:25:05 | 2022-11-13 09:25:06 |
Judging History
answer
// what is matter? never mind.
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 300005
#define inf 0x3f3f3f3f
int n,m;
int fa[maxn],sz[maxn];
ll now;
int gf(int x){
while(x^fa[x])x=fa[x]=fa[fa[x]];
return x;
}
pii stk[maxn]; int tim;
void upd(int u,int o){
if(o==1) now+=1ll*sz[u]*(sz[u]-1)/2;
else now-=1ll*sz[u]*(sz[u]-1)/2;
}
void merge(int u,int v){
u=gf(u),v=gf(v);if(u==v)return;
if(sz[u]<sz[v])swap(u,v);
stk[++tim]=mkp(u,v);
upd(u,-1),upd(v,-1),sz[u]+=sz[v],fa[v]=u,upd(u,1);
}
void bk(){
int u=stk[tim].fi,v=stk[tim].se; --tim;
upd(u,-1),sz[u]-=sz[v],fa[v]=v,upd(u,1),upd(v,1);
}
int dfn[maxn],low[maxn],ins[maxn],idx;
int st[maxn],tp;
vi e[maxn];
void tar(int u){
dfn[u]=low[u]=++idx;
st[++tp]=u,ins[u]=1;
for(int v:e[u]){
if(!dfn[v])tar(v),low[u]=min(low[u],low[v]);
else if(ins[v])low[u]=min(low[u],dfn[v]);
}
if(dfn[u]!=low[u])return;
int v=0; do{
ins[st[tp]]=0;
if(v) merge(v,st[tp]);
v=st[tp];
}while(st[tp--]!=u);
}
ll res[maxn];
struct node{
int u,v,t;
};
vi o;
void solve(int l,int r,vector<node>&p)
{
int mid=l+r>>1;
o.clear();
for(auto i:p)
if(i.t<=mid) o.pb(gf(i.u)),o.pb(gf(i.v));
for(int i:o)
dfn[i]=low[i]=ins[i]=0,e[i].clear();
idx=tp=0;
for(auto i:p)
if(i.t<=mid){
int u=gf(i.u),v=gf(i.v);
e[u].pb(v);
}
int nowt=tim;
for(int i:o)if(!dfn[i])tar(i);
if(l==r){
res[l]=now;
while(nowt!=tim)bk();
return;
}
vector<node>lp,rp;
for(auto i:p)
if(gf(i.u)==gf(i.v)){
if(i.t<=mid)lp.pb(i);
}else rp.pb(i);
p.resize(0);
solve(mid+1,r,rp);
while(nowt!=tim)bk();
solve(l,mid,lp);
}
signed main()
{
// freopen("ex_graph3.in","r",stdin);
// freopen("my.out","w",stdout);
n=read(),m=read();
For(i,1,n)fa[i]=i,sz[i]=1;
vector<node>p;
For(i,1,m){
int u=read(),v=read();
p.pb((node){u,v,i});
}
solve(1,m,p);
For(i,1,m)printf("%lld\n",res[i]);
return 0;
}
Details
Test #1:
score: 0
Wrong Answer
time: 333ms
memory: 39828kb