QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#61477#2214. Link Cut Digraphteam12345WA 333ms39828kbC++172.3kb2022-11-13 09:25:052022-11-13 09:25:06

Judging History

This is the latest submission verdict.

  • [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:25:06]
  • Judged
  • Verdict: WA
  • Time: 333ms
  • Memory: 39828kb
  • [2022-11-13 09:25:05]
  • Submitted

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