QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#353769#3556. Making Friends on Joitter is Funoscaryang0 0ms27784kbC++202.1kb2024-03-14 16:07:372024-03-14 16:07:37

Judging History

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

  • [2024-03-14 16:07:37]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:27784kb
  • [2024-03-14 16:07:37]
  • 提交

answer

#include<bits/stdc++.h>

#define ll long long
#define vc vector
#define pb emplace_back
#define pii pair<int, int>
#define mkp make_pair
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define lep(i, a, b) for(int i = (a); i >= (b); --i)

using namespace std;

inline int read() {
	int x = 0, w = 0; char ch = getchar(); while(!isdigit(ch)) w |= (ch == '-'), ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return w ? -x : x; 
}

const int N = 1e5 + 5;

int n, m, anc[N], sz[N], in[N], out[N], cnt[N];
ll ans;
queue<pii> Q;
unordered_map<int, int> G1[N], G2[N], G3[N], G4[N];

inline int get(int x) { return anc[x] == x ? x : anc[x] = get(anc[x]); }

inline void merge(int x, int y) {
	x = get(x); y = get(y);
	if(x == y) return ;
	
	// check new merge
	if(in[x] + out[x] < in[y] + out[y]) swap(x, y);
	for(auto [z, v] : G3[y]) if(v && G4[x].count(z)) Q.push(mkp(x, y));
	for(auto [z, v] : G4[y]) if(v && G3[x].count(z)) Q.push(mkp(x, y));
	
	// merge
	if(in[x] + out[x] + cnt[x] < in[y] + out[y] + cnt[y]) swap(x, y);
	ans -= 1ll * cnt[x] * sz[x] + 1ll * cnt[y] * sz[y];
	ans += 2ll * sz[x] * sz[y];
	anc[y] = x; sz[x] += sz[y]; cnt[x] -= G3[y][x];
	for(auto [z, v] : G2[y]) if(v && get(z) != x) {
		cnt[x] += !G2[x][z]; 
		G2[x][z] = 1; G1[z][y] = 0; G1[z][x] = 1;
	}
	ans += 1ll * cnt[x] * sz[x];
	for(auto [z, v] : G3[y]) if(v && get(z) != x) {
		out[x] += !G3[x][z]; G3[x][z] += v; G4[z][x] = 1;
	}
	for(auto [z, v] : G4[y]) if(v && get(z) != x) {
		in[x] += !G4[x][z]; G3[z][x] += G3[z][y]; G4[x][z] = 1; 
	}
}

signed main() {
	n = read(); m = read();
	rep(i, 1, n) anc[i] = i, sz[i] = 1;
	
	for(int t = 1, u, v, U, V; t <= m; printf("%lld\n", ans), t++) {
		U = get(u = read()); V = get(v = read()); 
		if(U == V || G1[u][V]) continue;
		
		G1[u][V] = G2[V][u] = 1; ++cnt[V];
		G3[U][V]++; ans += sz[V]; G4[V][U] = 1;
		if(G3[U][V] == 1) ++out[U], ++in[V];
		
		if(G3[V][U]) Q.push(mkp(U, V));
		while(!Q.empty()) {
			auto [x, y] = Q.front(); Q.pop();
			merge(x, y);
		}
	}
	return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 27784kb

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
15
15
15
15
15
15
15
15
15
15
15

result:

wrong answer 10th lines differ - expected: '20', found: '15'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%