QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#151635#3556. Making Friends on Joitter is FunOvO_Zuo0 4ms25700kbC++143.1kb2023-08-27 11:19:502023-08-27 11:19:52

Judging History

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

  • [2023-08-27 11:19:52]
  • 评测
  • 测评结果:0
  • 用时:4ms
  • 内存:25700kb
  • [2023-08-27 11:19:50]
  • 提交

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<<" "<<deg[x][y]<<" "<<deg[y][x]<<" "<<siz[x]<<" "<<siz[y]<<endl;
	int dx=0,dy=0;
	if(deg[x].count(y)) dx=deg[x][y],ans-=deg[x][y]*siz[y],deg[x].erase(y),deg_in[y].erase(x);
	if(deg[y].count(x)) dy=deg[y][x],ans-=deg[y][x]*siz[x],deg[y].erase(x),deg_in[x].erase(y);
	ans+=2*siz[x]*siz[y];
	/*
	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[(*it)].find(x)==edg[(*it)].end()) ++cnta;
		else ++cntb;
	//cout<<cnta<<" "<<cntb<<" "<<dx<<" "<<dy<<endl;
	ans+=(cnta-dx)*siz[x]+siz[y]*(edg_in[x].size()-cntb-dy);
	//cout<<ans<<" "<<cnta-dx<<" "<<siz[x]<<" "<<edg_in[x].size()<<" "<<edg_in[x].size()-cntb-dy<<" "<<siz[y]<<endl;//" "<<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[(*it)].find(x)==edg[(*it)].end()){
			if(get_fa((*it))!=x) edg[(*it)].insert(x),edg_in[x].insert((*it));
		}
		else deg[get_fa((*it))][x]--,deg_in[x][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[x].count(it.fi)) tpos.push_back(it.fi);
	}
	edg_in[y].clear();
	pos[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[xx].find(y)==edg[xx].end())
	{
		edg_in[y].insert(xx);
		//cout<<xx<<" "<<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团 的边
则将两团合并
*/

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 1
Accepted
time: 2ms
memory: 25608kb

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: 4ms
memory: 24936kb

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: 2ms
memory: 25088kb

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: 1ms
memory: 25304kb

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: 0
Accepted
time: 2ms
memory: 25700kb

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
52
52
59
60
60
60
60
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90

result:

ok 90 lines

Test #6:

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

input:

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

output:

1
2
3
4
5
6
7
8
9
11
12
13
14
17
20
22
24
26
26
28
29
35
35
49
49
55
55
55
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90

result:

ok 90 lines

Test #7:

score: -1
Wrong Answer
time: 1ms
memory: 24796kb

input:

50 2450
21 49
31 13
28 21
35 9
40 19
8 18
8 41
13 46
5 2
46 9
30 46
37 36
4 19
23 33
28 39
2 23
38 28
13 26
46 44
29 27
35 15
10 23
49 33
2 6
16 22
2 10
29 15
18 5
15 40
46 29
18 47
31 5
9 45
18 29
15 27
40 27
12 43
14 22
8 31
50 29
16 4
35 43
36 40
16 34
28 14
30 13
9 40
44 47
33 36
10 29
26 33
8 1...

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
99
100
101
102
103
106
109
110
113
114
115
116
117
118
119
120
121
12...

result:

wrong answer 140th lines differ - expected: '1705', found: '1663'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%