QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#189312 | #2214. Link Cut Digraph | Crysfly | AC ✓ | 299ms | 75292kb | C++20 | 2.3kb | 2023-09-27 10:20:40 | 2023-09-27 10:20:40 |
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 1000005
#define inf 0x3f3f3f3f
int n,m;
int fa[maxn],sz[maxn];
ll now;
int gf(int x){
while(x^fa[x])x=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("graph.in","r",stdin);
//freopen("graph.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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 273ms
memory: 70568kb
Test #2:
score: 0
Accepted
time: 280ms
memory: 69044kb
Test #3:
score: 0
Accepted
time: 278ms
memory: 69276kb
Test #4:
score: 0
Accepted
time: 265ms
memory: 73196kb
Test #5:
score: 0
Accepted
time: 281ms
memory: 69052kb
Test #6:
score: 0
Accepted
time: 287ms
memory: 75292kb
Test #7:
score: 0
Accepted
time: 292ms
memory: 70532kb
Test #8:
score: 0
Accepted
time: 289ms
memory: 71084kb
Test #9:
score: 0
Accepted
time: 192ms
memory: 72824kb
Test #10:
score: 0
Accepted
time: 279ms
memory: 71240kb
Test #11:
score: 0
Accepted
time: 263ms
memory: 66928kb
Test #12:
score: 0
Accepted
time: 299ms
memory: 70552kb
Test #13:
score: 0
Accepted
time: 195ms
memory: 72700kb
Test #14:
score: 0
Accepted
time: 201ms
memory: 74728kb
Test #15:
score: 0
Accepted
time: 65ms
memory: 56416kb
Test #16:
score: 0
Accepted
time: 242ms
memory: 58888kb
Test #17:
score: 0
Accepted
time: 250ms
memory: 59376kb
Test #18:
score: 0
Accepted
time: 250ms
memory: 59604kb
Test #19:
score: 0
Accepted
time: 292ms
memory: 73144kb
Test #20:
score: 0
Accepted
time: 265ms
memory: 70848kb
Test #21:
score: 0
Accepted
time: 253ms
memory: 66680kb
Test #22:
score: 0
Accepted
time: 254ms
memory: 70788kb
Test #23:
score: 0
Accepted
time: 263ms
memory: 72684kb
Test #24:
score: 0
Accepted
time: 266ms
memory: 66660kb
Test #25:
score: 0
Accepted
time: 251ms
memory: 68728kb
Test #26:
score: 0
Accepted
time: 248ms
memory: 66428kb
Test #27:
score: 0
Accepted
time: 259ms
memory: 67496kb