답안 #1100769

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100769 2024-10-14T15:54:45 Z lmToT27 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
648 ms 65608 KB
#include <bits/stdc++.h>
#define all(dataStructure) dataStructure.begin(),dataStructure.end()
#define ll long long

using namespace std;
namespace std {
	template <typename T, int D>
	struct _vector : public vector <_vector <T, D - 1>> {
		static_assert(D >= 1, "Dimension must be positive!");
		template <typename... Args>
		_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
	};
	// _vector <int, 3> a(n, m, k);: int a[n][m][k].
	// _vector <int, 3> a(n, m, k, x);: int a[n][m][k] initialized with x.
	template <typename T>
	struct _vector <T, 1> : public vector <T> {
		_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
	};
}

const int MAX = 1e5 + 3;
const ll MOD[] = {1000000007, 998244353};

int n, m;
int pa[MAX];
ll sz[MAX];
set <int> followers[MAX], in[MAX], out[MAX];
queue <pair <int, int>> toJoin;
ll ans;

int getSize(int i) {
	return followers[i].size() + in[i].size() + out[i].size();
}

void insertOneWay(int u, int v) {
	out[u].insert(v);
	in[v].insert(u);
	if (out[v].count(u)) {
		toJoin.push({u, v});
	}
}

int findpa(int u) {
	return u == pa[u] ? u : pa[u] = findpa(pa[u]);
}

void join(int u, int v) {
	u = findpa(u);
	v = findpa(v);
	if (u == v) return;
	if (getSize(u) < getSize(v)) swap(u, v);
	
	ans += 1ll * sz[u] * followers[v].size() + 1ll * sz[v] * followers[u].size();
	
	pa[v] = u;
	sz[u] += sz[v];
	
	for (int i : followers[v]) {
		if (followers[u].count(i)) ans -= sz[u];
		else followers[u].insert(i);
	}

	in[u].erase(v);
	in[v].erase(u);
	out[u].erase(v);
	out[v].erase(u);

	for (int i : in[v]) {
		out[i].erase(v);
		insertOneWay(i, u);
	}
	
	for (int i : out[v]) {
		in[i].erase(v);
		insertOneWay(u, i);
	}
}

void Solve() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		pa[i] = i;
		sz[i] = 1;
		followers[i].insert(i);
	}
	int u, v;
	while (m--) {
		cin >> u >> v;
		v = findpa(v);
		if (findpa(u) != v && !followers[v].count(u)) {
			followers[v].insert(u);
			ans += sz[v];
			u = findpa(u);
			insertOneWay(u, v);
			while (toJoin.size()) {
				auto [u, v] = toJoin.front();
				join(u, v);
				toJoin.pop();				
			}
		}
		cout << ans << '\n';
	}
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	#define TASK ""
	if (fopen(TASK".INP", "r")) {
		freopen(TASK".INP", "r", stdin);
		freopen(TASK".OUT", "w", stdout);
	}
	
	if (fopen("TASK.INP", "r")) {
		freopen("TASK.INP", "r", stdin);
		freopen("TASK.OUT", "w", stdout);
	}
	
	/* int TEST = 1; cin >> TEST; while (TEST--) */ Solve();

	cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
	return 0;
}

Compilation message

joitter2.cpp: In function 'int32_t main()':
joitter2.cpp:111:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |   freopen(TASK".INP", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:112:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |   freopen(TASK".OUT", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:116:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |   freopen("TASK.INP", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:117:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |   freopen("TASK.OUT", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14928 KB Output is correct
2 Correct 3 ms 14928 KB Output is correct
3 Correct 3 ms 14928 KB Output is correct
4 Correct 3 ms 14928 KB Output is correct
5 Correct 3 ms 14928 KB Output is correct
6 Correct 3 ms 14928 KB Output is correct
7 Correct 4 ms 14928 KB Output is correct
8 Correct 3 ms 14928 KB Output is correct
9 Correct 3 ms 14928 KB Output is correct
10 Correct 2 ms 14928 KB Output is correct
11 Correct 3 ms 14928 KB Output is correct
12 Correct 3 ms 14928 KB Output is correct
13 Correct 2 ms 14928 KB Output is correct
14 Correct 3 ms 15096 KB Output is correct
15 Correct 3 ms 14928 KB Output is correct
16 Correct 3 ms 14928 KB Output is correct
17 Correct 3 ms 14928 KB Output is correct
18 Correct 3 ms 14928 KB Output is correct
19 Correct 3 ms 14928 KB Output is correct
20 Correct 3 ms 14928 KB Output is correct
21 Correct 4 ms 14928 KB Output is correct
22 Correct 3 ms 14928 KB Output is correct
23 Correct 3 ms 15028 KB Output is correct
24 Correct 3 ms 14928 KB Output is correct
25 Correct 3 ms 14920 KB Output is correct
26 Correct 3 ms 14928 KB Output is correct
27 Correct 3 ms 14928 KB Output is correct
28 Correct 3 ms 14928 KB Output is correct
29 Correct 3 ms 15096 KB Output is correct
30 Correct 3 ms 14928 KB Output is correct
31 Correct 3 ms 14928 KB Output is correct
32 Correct 3 ms 14928 KB Output is correct
33 Correct 3 ms 14928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14928 KB Output is correct
2 Correct 3 ms 14928 KB Output is correct
3 Correct 3 ms 14928 KB Output is correct
4 Correct 3 ms 14928 KB Output is correct
5 Correct 3 ms 14928 KB Output is correct
6 Correct 3 ms 14928 KB Output is correct
7 Correct 4 ms 14928 KB Output is correct
8 Correct 3 ms 14928 KB Output is correct
9 Correct 3 ms 14928 KB Output is correct
10 Correct 2 ms 14928 KB Output is correct
11 Correct 3 ms 14928 KB Output is correct
12 Correct 3 ms 14928 KB Output is correct
13 Correct 2 ms 14928 KB Output is correct
14 Correct 3 ms 15096 KB Output is correct
15 Correct 3 ms 14928 KB Output is correct
16 Correct 3 ms 14928 KB Output is correct
17 Correct 3 ms 14928 KB Output is correct
18 Correct 3 ms 14928 KB Output is correct
19 Correct 3 ms 14928 KB Output is correct
20 Correct 3 ms 14928 KB Output is correct
21 Correct 4 ms 14928 KB Output is correct
22 Correct 3 ms 14928 KB Output is correct
23 Correct 3 ms 15028 KB Output is correct
24 Correct 3 ms 14928 KB Output is correct
25 Correct 3 ms 14920 KB Output is correct
26 Correct 3 ms 14928 KB Output is correct
27 Correct 3 ms 14928 KB Output is correct
28 Correct 3 ms 14928 KB Output is correct
29 Correct 3 ms 15096 KB Output is correct
30 Correct 3 ms 14928 KB Output is correct
31 Correct 3 ms 14928 KB Output is correct
32 Correct 3 ms 14928 KB Output is correct
33 Correct 3 ms 14928 KB Output is correct
34 Correct 4 ms 14928 KB Output is correct
35 Correct 50 ms 18572 KB Output is correct
36 Correct 78 ms 21076 KB Output is correct
37 Correct 68 ms 21076 KB Output is correct
38 Correct 65 ms 20552 KB Output is correct
39 Correct 4 ms 15184 KB Output is correct
40 Correct 5 ms 15552 KB Output is correct
41 Correct 6 ms 15608 KB Output is correct
42 Correct 4 ms 15300 KB Output is correct
43 Correct 5 ms 15184 KB Output is correct
44 Correct 5 ms 15184 KB Output is correct
45 Correct 5 ms 15196 KB Output is correct
46 Correct 4 ms 15184 KB Output is correct
47 Correct 6 ms 15440 KB Output is correct
48 Correct 7 ms 15440 KB Output is correct
49 Correct 11 ms 16208 KB Output is correct
50 Correct 75 ms 21060 KB Output is correct
51 Correct 8 ms 15440 KB Output is correct
52 Correct 61 ms 19320 KB Output is correct
53 Correct 11 ms 15952 KB Output is correct
54 Correct 65 ms 20296 KB Output is correct
55 Correct 8 ms 15696 KB Output is correct
56 Correct 7 ms 15696 KB Output is correct
57 Correct 7 ms 15860 KB Output is correct
58 Correct 7 ms 15540 KB Output is correct
59 Correct 6 ms 15696 KB Output is correct
60 Correct 51 ms 17992 KB Output is correct
61 Correct 6 ms 15440 KB Output is correct
62 Correct 68 ms 20432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14928 KB Output is correct
2 Correct 3 ms 14928 KB Output is correct
3 Correct 3 ms 14928 KB Output is correct
4 Correct 3 ms 14928 KB Output is correct
5 Correct 3 ms 14928 KB Output is correct
6 Correct 3 ms 14928 KB Output is correct
7 Correct 4 ms 14928 KB Output is correct
8 Correct 3 ms 14928 KB Output is correct
9 Correct 3 ms 14928 KB Output is correct
10 Correct 2 ms 14928 KB Output is correct
11 Correct 3 ms 14928 KB Output is correct
12 Correct 3 ms 14928 KB Output is correct
13 Correct 2 ms 14928 KB Output is correct
14 Correct 3 ms 15096 KB Output is correct
15 Correct 3 ms 14928 KB Output is correct
16 Correct 3 ms 14928 KB Output is correct
17 Correct 3 ms 14928 KB Output is correct
18 Correct 3 ms 14928 KB Output is correct
19 Correct 3 ms 14928 KB Output is correct
20 Correct 3 ms 14928 KB Output is correct
21 Correct 4 ms 14928 KB Output is correct
22 Correct 3 ms 14928 KB Output is correct
23 Correct 3 ms 15028 KB Output is correct
24 Correct 3 ms 14928 KB Output is correct
25 Correct 3 ms 14920 KB Output is correct
26 Correct 3 ms 14928 KB Output is correct
27 Correct 3 ms 14928 KB Output is correct
28 Correct 3 ms 14928 KB Output is correct
29 Correct 3 ms 15096 KB Output is correct
30 Correct 3 ms 14928 KB Output is correct
31 Correct 3 ms 14928 KB Output is correct
32 Correct 3 ms 14928 KB Output is correct
33 Correct 3 ms 14928 KB Output is correct
34 Correct 4 ms 14928 KB Output is correct
35 Correct 50 ms 18572 KB Output is correct
36 Correct 78 ms 21076 KB Output is correct
37 Correct 68 ms 21076 KB Output is correct
38 Correct 65 ms 20552 KB Output is correct
39 Correct 4 ms 15184 KB Output is correct
40 Correct 5 ms 15552 KB Output is correct
41 Correct 6 ms 15608 KB Output is correct
42 Correct 4 ms 15300 KB Output is correct
43 Correct 5 ms 15184 KB Output is correct
44 Correct 5 ms 15184 KB Output is correct
45 Correct 5 ms 15196 KB Output is correct
46 Correct 4 ms 15184 KB Output is correct
47 Correct 6 ms 15440 KB Output is correct
48 Correct 7 ms 15440 KB Output is correct
49 Correct 11 ms 16208 KB Output is correct
50 Correct 75 ms 21060 KB Output is correct
51 Correct 8 ms 15440 KB Output is correct
52 Correct 61 ms 19320 KB Output is correct
53 Correct 11 ms 15952 KB Output is correct
54 Correct 65 ms 20296 KB Output is correct
55 Correct 8 ms 15696 KB Output is correct
56 Correct 7 ms 15696 KB Output is correct
57 Correct 7 ms 15860 KB Output is correct
58 Correct 7 ms 15540 KB Output is correct
59 Correct 6 ms 15696 KB Output is correct
60 Correct 51 ms 17992 KB Output is correct
61 Correct 6 ms 15440 KB Output is correct
62 Correct 68 ms 20432 KB Output is correct
63 Correct 432 ms 64584 KB Output is correct
64 Correct 356 ms 64600 KB Output is correct
65 Correct 370 ms 64584 KB Output is correct
66 Correct 140 ms 31824 KB Output is correct
67 Correct 282 ms 58704 KB Output is correct
68 Correct 135 ms 31816 KB Output is correct
69 Correct 261 ms 33016 KB Output is correct
70 Correct 187 ms 31816 KB Output is correct
71 Correct 185 ms 31816 KB Output is correct
72 Correct 267 ms 42848 KB Output is correct
73 Correct 273 ms 44360 KB Output is correct
74 Correct 648 ms 65352 KB Output is correct
75 Correct 380 ms 38472 KB Output is correct
76 Correct 487 ms 50616 KB Output is correct
77 Correct 472 ms 50760 KB Output is correct
78 Correct 137 ms 35912 KB Output is correct
79 Correct 241 ms 38728 KB Output is correct
80 Correct 137 ms 34632 KB Output is correct
81 Correct 248 ms 36680 KB Output is correct
82 Correct 479 ms 52424 KB Output is correct
83 Correct 583 ms 52476 KB Output is correct
84 Correct 452 ms 51016 KB Output is correct
85 Correct 433 ms 51016 KB Output is correct
86 Correct 242 ms 64584 KB Output is correct
87 Correct 266 ms 65608 KB Output is correct
88 Correct 279 ms 43592 KB Output is correct
89 Correct 455 ms 48636 KB Output is correct