답안 #691903

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
691903 2023-02-01T00:21:29 Z NK_ 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
2310 ms 232764 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define sz(x) int(x.size())

using ll = long long;

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int N, M; cin >> N >> M;

	vector<int> ID(N);
	vector<set<int>> G(N), F(N), FG(N), GF(N);

	ll ans = 0;

	int cur = 0;
	vector<array<int, 2>> MERGE; 

	auto merge = [&](int u, int v) {
		int gu = ID[u], gv = ID[v];
		if (gu == gv) return;

		// cout << "MERGING: " << gu << " " << gv << nl;

		// cout << "BEF: " << nl;

		// cout << "GU: ";
		// for(auto x : G[gu]) cout << x << " ";
		// cout << nl;

		// cout << "FU: ";
		// for(auto x : F[gu]) cout << x << " ";
		// cout << nl;

		// cout << "FGU: ";
		// for(auto x : FG[gu]) cout << x << " ";
		// cout << nl;

		// cout << "GF: ";
		// for(auto x : GF[gu]) cout << x << " ";
		// cout << nl;

		// cout << "GV: ";
		// for(auto x : G[gv]) cout << x << " ";
		// cout << nl;

		// cout << "FV: ";
		// for(auto x : F[gv]) cout << x << " ";
		// cout << nl;

		// cout << "FGV: ";
		// for(auto x : FG[gv]) cout << x << " ";
		// cout << nl;

		// cout << "GF: ";
		// for(auto x : GF[gv]) cout << x << " ";
		// cout << nl;

		ans -= (sz(F[gu]) - 1) * 1LL * sz(G[gu]);
		ans -= (sz(F[gv]) - 1) * 1LL * sz(G[gv]);

		// CHECK FOR OTHER MERGES;

		set<int> add;

		if (sz(G[gu]) < sz(G[gv])) swap(gu, gv);
		for(const auto &x : G[gv]) {
			ID[x] = gu;
			G[gu].insert(x);
		}	

		if (sz(F[gu]) < sz(F[gv])) F[gu].swap(F[gv]);
		for(const auto &x : F[gv]) F[gu].insert(x);

		// if (sz(FG[gu]) < sz(FG[gv])) FG[gu].swap(FG[gv]);
		
		for(const auto &x : GF[gv]) {
			// cout << "SAW: " << x << nl;
			if (GF[x].count(gu)) add.insert(x);
			if (GF[x].count(gv)) add.insert(x);
			GF[gu].insert(x); FG[x].insert(gu);
		}
		

		for(const auto &x : FG[gv]) {
			// cout << "SAW: " << x << nl;
			if (FG[x].count(gu)) add.insert(x);
			if (FG[x].count(gv)) add.insert(x);
			FG[gu].insert(x); GF[x].insert(gu);
		}

		int gx = ID[u];
		ans += (sz(F[gx]) - 1) * 1LL * sz(G[gx]);

		// cout << "AFT: " << gx << nl;

		// cout << "G: ";
		// for(auto x : G[gx]) cout << x << " ";
		// cout << nl;

		// cout << "F: ";
		// for(auto x : F[gx]) cout << x << " ";
		// cout << nl;

		// cout << "FG: ";
		// for(auto x : FG[gx]) cout << x << " ";
		// cout << nl;

		// cout << "GF: ";
		// for(auto x : GF[gx]) cout << x << " ";
		// cout << nl;

		for(const auto &x : add) {
			if (x == gu || x == gv) continue;
			// cout << "ADDED: " << ID[x] << nl;
			MERGE.push_back({x, gx});
		}
	};

	for(int i = 0; i < N; i++) {
		ID[i] = i;
		G[i] = F[i] = {i};
	}

	for(int i = 0; i < M; i++) {
		int u, v; cin >> u >> v; --u, --v; // u follows v
		int gu = ID[u], gv = ID[v];

		if (gu == gv) cout << ans << nl; // Already same group
		else if (F[gv].count(u)) cout << ans << nl; // Already fan of group
		else if (FG[gv].count(gu)) { // Merge groups + fans
			
			MERGE.push_back({gv, gu});
			while(cur < sz(MERGE)) {
				merge(MERGE[cur][0], MERGE[cur][1]); cur++;
			}

			cout << ans << nl;
		} else { 
			F[gv].insert(u);	
			FG[gu].insert(gv);
			GF[gv].insert(gu);

			ans += sz(G[gv]);
			cout << ans << nl;
		}
	}
	
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 328 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 328 KB Output is correct
32 Correct 1 ms 320 KB Output is correct
33 Correct 2 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 328 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 328 KB Output is correct
32 Correct 1 ms 320 KB Output is correct
33 Correct 2 ms 328 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 80 ms 6876 KB Output is correct
36 Correct 124 ms 10168 KB Output is correct
37 Correct 114 ms 10316 KB Output is correct
38 Correct 123 ms 9908 KB Output is correct
39 Correct 4 ms 1356 KB Output is correct
40 Correct 15 ms 3284 KB Output is correct
41 Correct 10 ms 3028 KB Output is correct
42 Correct 4 ms 1360 KB Output is correct
43 Correct 4 ms 1364 KB Output is correct
44 Correct 4 ms 1364 KB Output is correct
45 Correct 4 ms 1360 KB Output is correct
46 Correct 4 ms 1360 KB Output is correct
47 Correct 8 ms 2320 KB Output is correct
48 Correct 8 ms 2252 KB Output is correct
49 Correct 17 ms 3020 KB Output is correct
50 Correct 141 ms 10428 KB Output is correct
51 Correct 8 ms 1848 KB Output is correct
52 Correct 87 ms 8448 KB Output is correct
53 Correct 18 ms 2900 KB Output is correct
54 Correct 111 ms 9412 KB Output is correct
55 Correct 9 ms 2132 KB Output is correct
56 Correct 8 ms 2004 KB Output is correct
57 Correct 8 ms 2116 KB Output is correct
58 Correct 8 ms 2068 KB Output is correct
59 Correct 11 ms 3532 KB Output is correct
60 Correct 81 ms 8548 KB Output is correct
61 Correct 8 ms 2132 KB Output is correct
62 Correct 108 ms 9668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 328 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 328 KB Output is correct
32 Correct 1 ms 320 KB Output is correct
33 Correct 2 ms 328 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 80 ms 6876 KB Output is correct
36 Correct 124 ms 10168 KB Output is correct
37 Correct 114 ms 10316 KB Output is correct
38 Correct 123 ms 9908 KB Output is correct
39 Correct 4 ms 1356 KB Output is correct
40 Correct 15 ms 3284 KB Output is correct
41 Correct 10 ms 3028 KB Output is correct
42 Correct 4 ms 1360 KB Output is correct
43 Correct 4 ms 1364 KB Output is correct
44 Correct 4 ms 1364 KB Output is correct
45 Correct 4 ms 1360 KB Output is correct
46 Correct 4 ms 1360 KB Output is correct
47 Correct 8 ms 2320 KB Output is correct
48 Correct 8 ms 2252 KB Output is correct
49 Correct 17 ms 3020 KB Output is correct
50 Correct 141 ms 10428 KB Output is correct
51 Correct 8 ms 1848 KB Output is correct
52 Correct 87 ms 8448 KB Output is correct
53 Correct 18 ms 2900 KB Output is correct
54 Correct 111 ms 9412 KB Output is correct
55 Correct 9 ms 2132 KB Output is correct
56 Correct 8 ms 2004 KB Output is correct
57 Correct 8 ms 2116 KB Output is correct
58 Correct 8 ms 2068 KB Output is correct
59 Correct 11 ms 3532 KB Output is correct
60 Correct 81 ms 8548 KB Output is correct
61 Correct 8 ms 2132 KB Output is correct
62 Correct 108 ms 9668 KB Output is correct
63 Correct 521 ms 76528 KB Output is correct
64 Correct 546 ms 76628 KB Output is correct
65 Correct 510 ms 76532 KB Output is correct
66 Correct 367 ms 57652 KB Output is correct
67 Correct 1568 ms 190776 KB Output is correct
68 Correct 322 ms 57504 KB Output is correct
69 Correct 549 ms 55340 KB Output is correct
70 Correct 385 ms 57496 KB Output is correct
71 Correct 406 ms 57596 KB Output is correct
72 Correct 1081 ms 104872 KB Output is correct
73 Correct 1188 ms 112856 KB Output is correct
74 Correct 2310 ms 154260 KB Output is correct
75 Correct 781 ms 67524 KB Output is correct
76 Correct 1390 ms 105172 KB Output is correct
77 Correct 1388 ms 105556 KB Output is correct
78 Correct 360 ms 63436 KB Output is correct
79 Correct 623 ms 74996 KB Output is correct
80 Correct 380 ms 59692 KB Output is correct
81 Correct 581 ms 67716 KB Output is correct
82 Correct 1203 ms 91104 KB Output is correct
83 Correct 1300 ms 91184 KB Output is correct
84 Correct 999 ms 91092 KB Output is correct
85 Correct 993 ms 91192 KB Output is correct
86 Correct 1386 ms 230444 KB Output is correct
87 Correct 1441 ms 232764 KB Output is correct
88 Correct 1144 ms 109172 KB Output is correct
89 Correct 1373 ms 102752 KB Output is correct