QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#61478#2214. Link Cut Digraphteam12345AC ✓393ms41472kbC++172.3kb2022-11-13 09:26:042022-11-13 09:26:05

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-13 09:26:05]
  • 评测
  • 测评结果:AC
  • 用时:393ms
  • 内存:41472kb
  • [2022-11-13 09:26:04]
  • 提交

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];
	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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 357ms
memory: 38824kb

Test #2:

score: 0
Accepted
time: 369ms
memory: 39380kb

Test #3:

score: 0
Accepted
time: 374ms
memory: 39516kb

Test #4:

score: 0
Accepted
time: 319ms
memory: 40048kb

Test #5:

score: 0
Accepted
time: 373ms
memory: 40468kb

Test #6:

score: 0
Accepted
time: 363ms
memory: 39872kb

Test #7:

score: 0
Accepted
time: 361ms
memory: 39968kb

Test #8:

score: 0
Accepted
time: 364ms
memory: 41072kb

Test #9:

score: 0
Accepted
time: 266ms
memory: 41472kb

Test #10:

score: 0
Accepted
time: 393ms
memory: 41056kb

Test #11:

score: 0
Accepted
time: 355ms
memory: 40192kb

Test #12:

score: 0
Accepted
time: 367ms
memory: 40084kb

Test #13:

score: 0
Accepted
time: 271ms
memory: 41412kb

Test #14:

score: 0
Accepted
time: 272ms
memory: 40936kb

Test #15:

score: 0
Accepted
time: 105ms
memory: 24984kb

Test #16:

score: 0
Accepted
time: 302ms
memory: 32480kb

Test #17:

score: 0
Accepted
time: 301ms
memory: 32768kb

Test #18:

score: 0
Accepted
time: 315ms
memory: 33304kb

Test #19:

score: 0
Accepted
time: 372ms
memory: 41176kb

Test #20:

score: 0
Accepted
time: 352ms
memory: 40672kb

Test #21:

score: 0
Accepted
time: 358ms
memory: 40736kb

Test #22:

score: 0
Accepted
time: 345ms
memory: 39128kb

Test #23:

score: 0
Accepted
time: 338ms
memory: 39040kb

Test #24:

score: 0
Accepted
time: 340ms
memory: 39052kb

Test #25:

score: 0
Accepted
time: 334ms
memory: 39456kb

Test #26:

score: 0
Accepted
time: 326ms
memory: 39688kb

Test #27:

score: 0
Accepted
time: 330ms
memory: 37796kb