QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#547415#2214. Link Cut DigraphIceturkyAC ✓156ms100592kbC++173.3kb2024-09-04 21:32:362024-09-04 21:32:36

Judging History

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

  • [2024-09-04 21:32:36]
  • 评测
  • 测评结果:AC
  • 用时:156ms
  • 内存:100592kb
  • [2024-09-04 21:32:36]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<queue>
#define int long long
#define ll long long
#define ls (x<<1)
#define rs ((x<<1)|1)
#define mid ((l+r)>>1)
#define pc(x) putchar(x)
#define inf ((ll)(1e18+7))
#define lowbit(x) (x&(-x))
#define pb push_back
#define pir pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(),x.end()

using namespace std;

inline ll read()
{
	ll w,f=1;char c;
	while((c=getchar())>'9'||c<'0')
		if(c=='-')f=-1;
	w=c-'0';
	while((c=getchar())>='0'&&c<='9')
		w=w*10+c-'0';
	return w*f;
}

int printt[50],never_use;

inline void print(ll x)
{
	if(x==0)
		pc(48);
	if(x<0)
		pc('-'),x=-x;
	while(x)
		printt[++never_use]=x%10,x/=10;
	while(never_use)
		pc(printt[never_use--]+48);
}

const int N=5e5+5,M=5e5+5; 

pir EE[M];
struct Edge{
	int u,v,id;
}E[M];
vector<pir> tim[M];

struct edge{
	int v,nxt;
}e[M];
int head[N],tott;
void add(int u,int v){e[++tott]={v,head[u]},head[u]=tott;}

int dfn[N],low[N],dfncnt;
int stk[N],tot;
int col[N],cnt;
void tarjan(int u){
//	cout<<" "<<u<<" "<<dfn[u]<<endl;
	low[u]=dfn[u]=++dfncnt;
	stk[++tot]=u;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].v;
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}else if(!col[v])
			low[u]=min(low[u],dfn[v]);
	}
	if(low[u]==dfn[u]){
		cnt++;
		int x;
		do{
			col[x=stk[tot]]=cnt;
			stk[tot--]=0;
		}while(x!=u);
	}
}

void calc(int l,int r,int L,int R){
	if(l==r){
		for(int i=L;i<=R;i++)
			tim[l].pb(EE[E[i].id]);
		return;
	}
	tott=0;
	dfncnt=0;
	cnt=0;
	tot=0;
//	cout<<l<<" "<<r<<" "<<L<<" "<<R<<endl;
	for(int i=L;i<=R;i++){
//		cout<<E[i].u<<" "<<E[i].v<<" "<<E[i].id<<endl;
		head[E[i].u]=head[E[i].v]=0;
		dfn[E[i].u]=dfn[E[i].v]=0;
		low[E[i].u]=low[E[i].v]=0;
		col[E[i].u]=col[E[i].v]=0;
	}
	for(int i=L;i<=R;i++)
		if(E[i].id<=mid)
			add(E[i].u,E[i].v);
	for(int i=L;i<=R;i++){
		if(!dfn[E[i].u])
			tarjan(E[i].u);
//		if(!dfn[E[i].v])
//			tarjan(E[i].v);
	}
	vector<Edge> E1,E2;
	int RR=R;
	for(int i=L;i<=R;i++){
//		cout<<" "<<E[i].u<<" "<<E[i].v<<" "<<E[i].id<<endl;
//		cout<<" "<<col[E[i].u]<<" "<<col[E[i].v]<<endl;
		if(col[E[i].u]!=col[E[i].v])
			E2.pb({col[E[i].u],col[E[i].v],E[i].id});
		else if(E[i].id<=mid)
			E1.pb(E[i]);
		else
			RR--;
	}
	int len=E1.size();
	for(int i=0;i<E1.size();i++)
		E[i+L]=E1[i];
	for(int i=0;i<E2.size();i++)
		E[i+len+L]=E2[i];
	calc(l,mid,L,L+len-1);
	calc(mid+1,r,L+len,RR);
}

struct DSU{
	int fa[N],sum[N];
	void init(int n){
		for(int i=1;i<=n;i++)
			fa[i]=i,sum[i]=1;
	}
	int fd(int x){
		return fa[x]==x?x:fa[x]=fd(fa[x]);
	}
}D;

signed main(){
	int n=read(),m=read();
	for(int i=1;i<=m;i++)
		E[i]={read(),read(),i},EE[i]={E[i].u,E[i].v};
	calc(1,m+1,1,m);
	D.init(n);
	int ans=0;
	for(int i=1;i<=m;i++){
		for(pir j:tim[i]){
			int u=j.fi,v=j.se;
//			cout<<u<<" "<<v<<endl;
			u=D.fd(u),v=D.fd(v);
			if(u==v)
				continue;
			ans-=D.sum[u]*(D.sum[u]-1)/2;
			ans-=D.sum[v]*(D.sum[v]-1)/2;
			if(D.sum[u]<D.sum[v])
				swap(u,v);
			D.fa[v]=u;
			D.sum[u]+=D.sum[v]; 
			ans+=D.sum[u]*(D.sum[u]-1)/2;
		}print(ans),pc('\n');
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 151ms
memory: 87584kb

Test #2:

score: 0
Accepted
time: 150ms
memory: 84620kb

Test #3:

score: 0
Accepted
time: 152ms
memory: 91680kb

Test #4:

score: 0
Accepted
time: 144ms
memory: 100592kb

Test #5:

score: 0
Accepted
time: 143ms
memory: 84336kb

Test #6:

score: 0
Accepted
time: 145ms
memory: 85460kb

Test #7:

score: 0
Accepted
time: 144ms
memory: 82736kb

Test #8:

score: 0
Accepted
time: 149ms
memory: 86132kb

Test #9:

score: 0
Accepted
time: 119ms
memory: 100296kb

Test #10:

score: 0
Accepted
time: 156ms
memory: 86016kb

Test #11:

score: 0
Accepted
time: 150ms
memory: 88828kb

Test #12:

score: 0
Accepted
time: 146ms
memory: 86560kb

Test #13:

score: 0
Accepted
time: 120ms
memory: 90692kb

Test #14:

score: 0
Accepted
time: 111ms
memory: 99432kb

Test #15:

score: 0
Accepted
time: 37ms
memory: 58476kb

Test #16:

score: 0
Accepted
time: 123ms
memory: 73072kb

Test #17:

score: 0
Accepted
time: 130ms
memory: 74008kb

Test #18:

score: 0
Accepted
time: 127ms
memory: 74356kb

Test #19:

score: 0
Accepted
time: 145ms
memory: 90804kb

Test #20:

score: 0
Accepted
time: 145ms
memory: 98016kb

Test #21:

score: 0
Accepted
time: 147ms
memory: 91352kb

Test #22:

score: 0
Accepted
time: 142ms
memory: 95824kb

Test #23:

score: 0
Accepted
time: 125ms
memory: 92776kb

Test #24:

score: 0
Accepted
time: 130ms
memory: 90960kb

Test #25:

score: 0
Accepted
time: 136ms
memory: 86988kb

Test #26:

score: 0
Accepted
time: 132ms
memory: 87428kb

Test #27:

score: 0
Accepted
time: 132ms
memory: 91220kb