QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#151619#3556. Making Friends on Joitter is FunOvO_Zuo0 4ms25644kbC++142.9kb2023-08-27 10:21:492023-08-27 10:21:50

Judging History

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

  • [2023-08-27 10:21:50]
  • 评测
  • 测评结果:0
  • 用时:4ms
  • 内存:25644kb
  • [2023-08-27 10:21:49]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
const int N=1e5+5;
int n,m;
set<int> edg[N],edg_in[N];
map<int,int> deg[N],deg_in[N];
vector<int> pos[N],tpos;
ll siz[N],fa[N];
ll ans=0;
int get_fa(int x){ return x==fa[x]?x:fa[x]=get_fa(fa[x]);}
void merge(int xx,int yy)
{	
	int x=get_fa(xx),y=get_fa(yy);
	if(x==y) return;
	if(siz[x]<siz[y]) swap(x,y);
	///cout<<x<<" "<<y<<endl;
	int dx=deg[x][y],dy=deg[y][x];
	if(dx) deg[x].erase(y),deg_in[y].erase(x),ans-=dx*siz[y];
	if(dy) deg[y].erase(x),deg_in[x].erase(y),ans-=dy*siz[x];
	ans+=2*siz[x]*siz[y];
	//cout<<ans<<endl;
	/*
	for(auto it=edg_in[x].begin();it!=edg_in[x].end();it++)
		if(edg_in[y].find((*it))==edg_in[y].end()) ans+=siz[y];
	for(auto it=edg_in[y].begin();it!=edg_in[y].end();it++)
		if(edg_in[x].find((*it))==edg_in[x].end()) ans+=siz[x];
	*/
	int cnta=0,cntb=0;
	for(auto it=edg_in[y].begin();it!=edg_in[y].end();it++)
		if(edg_in[x].find((*it))==edg_in[x].end()) ++cnta;
		else ++cntb;
	ans+=(cnta-dx)*siz[x]+siz[y]*(edg_in[x].size()-cntb-dy);
	//cout<<ans<<" "<<siz[x]<<" "<<siz[y]<<" "<<cnta<<" "<<cntb<<" "<<dx<<" "<<dy<<" "<<edg_in[x].size()<<endl;
	for(auto it=edg_in[y].begin();it!=edg_in[y].end();it++)
	{
		edg[(*it)].erase(y);
		if(edg_in[x].find((*it))==edg_in[x].end()){
			if(get_fa((*it))!=x) edg[(*it)].insert(x),edg_in[x].insert((*it));
		}
		else deg[get_fa((*it))][y]--,deg_in[y][get_fa((*it))]--;
	}
	for(int t:pos[y])
	{
		if(edg[t].count(x)) edg[t].erase(x),edg_in[x].erase(t);
		pos[x].push_back(t);
	}
	tpos.clear();
	for(auto it:deg[y])
	{
		deg[x][it.fi]+=it.se,deg_in[it.fi][x]+=it.se;
		deg_in[it.fi].erase(y);
		if(deg[it.fi].count(x)) tpos.push_back(it.fi);
	}
	for(auto it:deg_in[y])
	{
		deg_in[x][it.fi]+=it.se,deg[it.fi][x]+=it.se;
		deg[it.fi].erase(y);
		if(deg_in[it.fi].count(x)) tpos.push_back(it.fi);
	}
	edg_in[y].clear();
	pos[y].clear();
	edg[y].clear();
	deg[y].clear();
	deg_in[y].clear();
	fa[y]=x;
	siz[x]+=siz[y];	
	for(int t:tpos) merge(x,t);
}
void Merge(int xx,int yy)
{
	int x=get_fa(xx),y=get_fa(yy);
	if(deg[y].count(x)) merge(xx,yy);
	else if(edg[y].find(x)==edg[y].end())
	{
		if(edg[xx].find(y)!=edg[xx].end()) return;
		edg_in[y].insert(xx);
		//cout<<x<<" "<<y<<" "<<edg_in[y].size()<<endl;
		edg[xx].insert(y);
		deg_in[y][x]++;
		deg[x][y]++;
		ans+=siz[y];
		return;
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	int i,x,y;
	for(i=1;i<=n;i++) siz[i]=1,fa[i]=i,pos[i].push_back(i);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		if(get_fa(x)!=get_fa(y)) Merge(x,y);
		printf("%lld\n",ans);
	}
	return 0;
}
/*
将一个极大团视作一个整体
当加入一条边时,有影响的只有两端所处的团
因为操作次数只有 3e5 次,所以团之间连边的数量也是这个量级
使用set维护该团的出边,加入边 a->b 后发现存在 b团->a团 的边
则将两团合并
*/

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 1
Accepted
time: 3ms
memory: 25184kb

input:

5 20
4 2
1 5
2 3
2 5
3 2
3 1
4 5
1 2
5 2
1 4
4 1
3 4
5 1
2 4
2 1
4 3
1 3
5 4
3 5
5 3

output:

1
2
3
4
6
7
8
12
16
20
20
20
20
20
20
20
20
20
20
20

result:

ok 20 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 25644kb

input:

5 20
4 5
1 2
2 1
4 3
3 2
5 2
1 5
4 1
2 3
1 3
1 4
4 2
5 1
3 5
2 5
3 1
2 4
3 4
5 4
5 3

output:

1
2
3
4
6
8
13
13
16
16
20
20
20
20
20
20
20
20
20
20

result:

ok 20 lines

Test #3:

score: 0
Accepted
time: 1ms
memory: 25628kb

input:

5 20
3 1
5 1
3 4
5 2
1 2
5 4
3 5
2 4
1 3
2 5
4 5
4 3
4 2
2 3
3 2
5 3
2 1
1 5
4 1
1 4

output:

1
2
3
4
5
6
7
8
11
15
20
20
20
20
20
20
20
20
20
20

result:

ok 20 lines

Test #4:

score: 0
Accepted
time: 0ms
memory: 24816kb

input:

10 10
9 1
10 6
4 10
1 8
4 9
7 8
2 4
6 5
10 1
1 7

output:

1
2
3
4
5
6
7
8
9
10

result:

ok 10 lines

Test #5:

score: -1
Wrong Answer
time: 4ms
memory: 24756kb

input:

10 90
10 6
7 8
8 4
9 3
2 8
9 2
1 10
1 8
8 9
5 6
2 10
4 3
7 2
10 2
10 5
3 7
1 9
8 7
1 2
9 1
7 6
9 7
10 3
8 3
6 3
7 5
8 2
6 1
4 9
7 1
4 2
6 8
7 3
9 8
5 1
10 4
6 4
1 4
6 7
3 1
9 10
3 2
1 7
2 5
10 1
2 7
2 1
5 8
7 9
2 4
6 9
3 8
10 7
8 5
5 4
8 6
5 10
3 5
5 7
8 1
4 7
4 1
10 8
9 5
4 8
6 10
1 6
2 9
1 5
6 5
3...

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
17
18
19
20
55
55
63
64
64
64
64
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96
96

result:

wrong answer 18th lines differ - expected: '52', found: '55'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%