답안 #709610

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
709610 2023-03-14T02:22:42 Z GusterGoose27 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
9 ms 16724 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1e5;
const int MAXM = 3e5;
vector<int> contained[MAXN];
vector<int> out_edges[MAXN];
vector<int> in_edges[MAXN];
int uf[MAXN];
set<int> inc[MAXN]; // specific nodes
set<int> out[MAXN]; // this is just general ccs
ll ans = 0;
int n, m;

void undo(int i) {
	ans -= (ll)contained[i].size()*inc[i].size();
	ans -= (ll)contained[i].size()*((int)contained[i].size()-1);
}

void make(int i) {
	ans += (ll)contained[i].size()*inc[i].size();
	ans += (ll)contained[i].size()*((int)contained[i].size()-1);
}

void merge(int a, int b) {
	a = uf[a];
	b = uf[b];
	if (a == b) return;
	if (contained[a].size() < contained[b].size()) swap(a, b);
	undo(a);
	undo(b);
	out[a].erase(b);
	out[b].erase(a);
	for (int v: contained[b]) {
		inc[a].erase(v);
		uf[v] = a;
		contained[a].push_back(v);
	}
	for (int w: inc[b]) {
		if (uf[w] != a) {
			inc[a].insert(w);
			out[uf[w]].insert(a);
			out[uf[w]].erase(b);
		}
	}
	for (int w: out[b]) {
		out[a].insert(w);
	}
	out[b].clear();
	inc[b].clear();
	contained[b].clear();
	make(a);
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n >> m;
	iota(uf, uf+n, 0);
	for (int i = 0; i < n; i++) contained[i].push_back(i);
	for (int i = 0; i < m; i++) {
		int x, y; cin >> x >> y;
		x--; y--;
		if (uf[x] != uf[y]) {
			if (out[uf[y]].find(uf[x]) != out[uf[y]].end()) {
				merge(x, y);
			}
			else {
				undo(uf[y]);
				inc[uf[y]].insert(x);
				make(uf[y]);
				out[uf[x]].insert(uf[y]);
			}
		}
		cout << ans << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Incorrect 8 ms 16724 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Incorrect 8 ms 16724 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Incorrect 8 ms 16724 KB Output isn't correct
4 Halted 0 ms 0 KB -