답안 #445340

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
445340 2021-07-17T14:12:32 Z ics0503 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1711 ms 115888 KB
#include<stdio.h>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
set<long long >edge[121212], redge[121212], ar[121212], a[121212], aar[121212];
vector<long long >L[121212];
long long par[121212], sz[121212], rsz[121212], ck[121212];
long long find(long long x) {
	if (par[x] == x)return x;
	return par[x] = find(par[x]);
}

long long ans = 0;
set<int>LL;
void un(int s, int e) {
	long long ps = find(s), pe = find(e);
	if (ps == pe)return;
	if (sz[ps] < sz[pe]) { swap(s, e), swap(ps, pe); }
	ans -= rsz[ps] * (rsz[ps] - 1);
	ans -= rsz[pe] * (rsz[pe] - 1);
	for (auto x : L[pe]) {
		int ff = 1;
		for (long long nxt : edge[x]) {
			long long pnxt = find(nxt);
			if (pnxt == ps) {
				if(ff)ans -= rsz[ps];
				ff = 0;
			}
			else {
				if (pnxt != pe) {
					aar[pnxt].erase(pe);
					aar[pnxt].insert(ps);
				}
			}
		}
		for (long long nxt : redge[x]) {
			long long pnxt = find(nxt);
			if (pnxt == ps) {
				if (ck[nxt] == 0) {
					ans -= rsz[pe];
					ck[nxt] = 1;
				}
			}
			else {
				if (pnxt != pe) {
					a[pnxt].erase(pe);
					a[pnxt].insert(ps);
				}
			}
		}
		L[ps].push_back(x);
		if (ar[ps].find(x) != ar[ps].end()) ar[ps].erase(x);
	}
	for (auto x : L[pe]) {
		for (long long nxt : redge[x]) {
			ck[nxt] = 0;
		}
	}
	ans += rsz[pe] * ar[ps].size();
	for (auto x : a[pe]) if (aar[ps].find(x) != aar[ps].end())LL.insert(x);
	for (auto x : aar[pe]) if (a[ps].find(x) != a[ps].end())LL.insert(x);
	for (auto x : ar[pe]) {
		if (ar[ps].find(x) != ar[ps].end())ans -= rsz[pe];
		else if (find(x) != ps) {
			ans += rsz[ps];
			ar[ps].insert(x);
		}
	}
	for (auto x : a[pe])a[ps].insert(x);
	for (auto x : aar[pe]) aar[ps].insert(x);
	a[ps].erase(ps); aar[ps].erase(ps);
	par[pe] = ps;
	rsz[ps] += rsz[pe];
	sz[ps] += sz[pe];
	ans += rsz[ps] * (rsz[ps] - 1);
	if (LL.size() > 0) {
		auto now = *LL.begin();
		LL.erase(now);
		un(ps, now);
	}
}
int main() {
	long long n, m; scanf("%lld%lld", &n, &m);
	long long i, j;
	for (i = 1; i <= n; i++)par[i] = i, sz[i] = rsz[i] = 1, L[i].push_back(i);
	for (i = 0; i < m; i++) {
		long long s, e; scanf("%lld%lld", &s, &e);
		long long ps = find(s), pe = find(e);
		if (ps == pe) { printf("%lld\n", ans); continue; }
		if (a[pe].find(ps) != a[pe].end()) {
			un(s, e);
		}
		else {
			edge[s].insert(e); redge[e].insert(s);
			a[ps].insert(pe);
			aar[pe].insert(ps);
			if (ar[pe].find(s) == ar[pe].end()) {
				ar[pe].insert(s);
				sz[ps]++; sz[pe]++;
				ans += rsz[pe];
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:85:15: warning: unused variable 'j' [-Wunused-variable]
   85 |  long long i, j;
      |               ^
joitter2.cpp:84:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |  long long n, m; scanf("%lld%lld", &n, &m);
      |                  ~~~~~^~~~~~~~~~~~~~~~~~~~
joitter2.cpp:88:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |   long long s, e; scanf("%lld%lld", &s, &e);
      |                   ~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 31564 KB Output is correct
2 Correct 20 ms 31564 KB Output is correct
3 Correct 20 ms 31620 KB Output is correct
4 Correct 20 ms 31564 KB Output is correct
5 Correct 20 ms 31564 KB Output is correct
6 Correct 20 ms 31540 KB Output is correct
7 Correct 21 ms 31564 KB Output is correct
8 Correct 21 ms 31672 KB Output is correct
9 Correct 20 ms 31564 KB Output is correct
10 Correct 20 ms 31564 KB Output is correct
11 Correct 20 ms 31624 KB Output is correct
12 Correct 20 ms 31576 KB Output is correct
13 Correct 20 ms 31620 KB Output is correct
14 Correct 19 ms 31564 KB Output is correct
15 Correct 20 ms 31640 KB Output is correct
16 Correct 20 ms 31600 KB Output is correct
17 Correct 20 ms 31564 KB Output is correct
18 Correct 20 ms 31564 KB Output is correct
19 Correct 19 ms 31540 KB Output is correct
20 Correct 19 ms 31660 KB Output is correct
21 Correct 23 ms 31624 KB Output is correct
22 Correct 20 ms 31560 KB Output is correct
23 Correct 20 ms 31564 KB Output is correct
24 Correct 22 ms 31564 KB Output is correct
25 Correct 20 ms 31560 KB Output is correct
26 Correct 19 ms 31656 KB Output is correct
27 Correct 20 ms 31564 KB Output is correct
28 Correct 20 ms 31636 KB Output is correct
29 Correct 20 ms 31552 KB Output is correct
30 Correct 20 ms 31564 KB Output is correct
31 Correct 20 ms 31596 KB Output is correct
32 Correct 20 ms 31568 KB Output is correct
33 Correct 21 ms 31664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 31564 KB Output is correct
2 Correct 20 ms 31564 KB Output is correct
3 Correct 20 ms 31620 KB Output is correct
4 Correct 20 ms 31564 KB Output is correct
5 Correct 20 ms 31564 KB Output is correct
6 Correct 20 ms 31540 KB Output is correct
7 Correct 21 ms 31564 KB Output is correct
8 Correct 21 ms 31672 KB Output is correct
9 Correct 20 ms 31564 KB Output is correct
10 Correct 20 ms 31564 KB Output is correct
11 Correct 20 ms 31624 KB Output is correct
12 Correct 20 ms 31576 KB Output is correct
13 Correct 20 ms 31620 KB Output is correct
14 Correct 19 ms 31564 KB Output is correct
15 Correct 20 ms 31640 KB Output is correct
16 Correct 20 ms 31600 KB Output is correct
17 Correct 20 ms 31564 KB Output is correct
18 Correct 20 ms 31564 KB Output is correct
19 Correct 19 ms 31540 KB Output is correct
20 Correct 19 ms 31660 KB Output is correct
21 Correct 23 ms 31624 KB Output is correct
22 Correct 20 ms 31560 KB Output is correct
23 Correct 20 ms 31564 KB Output is correct
24 Correct 22 ms 31564 KB Output is correct
25 Correct 20 ms 31560 KB Output is correct
26 Correct 19 ms 31656 KB Output is correct
27 Correct 20 ms 31564 KB Output is correct
28 Correct 20 ms 31636 KB Output is correct
29 Correct 20 ms 31552 KB Output is correct
30 Correct 20 ms 31564 KB Output is correct
31 Correct 20 ms 31596 KB Output is correct
32 Correct 20 ms 31568 KB Output is correct
33 Correct 21 ms 31664 KB Output is correct
34 Correct 24 ms 31664 KB Output is correct
35 Correct 141 ms 38376 KB Output is correct
36 Correct 187 ms 42800 KB Output is correct
37 Correct 183 ms 43016 KB Output is correct
38 Correct 181 ms 42436 KB Output is correct
39 Correct 24 ms 32232 KB Output is correct
40 Correct 26 ms 32588 KB Output is correct
41 Correct 28 ms 32588 KB Output is correct
42 Correct 26 ms 32204 KB Output is correct
43 Correct 24 ms 32220 KB Output is correct
44 Correct 25 ms 32204 KB Output is correct
45 Correct 24 ms 32292 KB Output is correct
46 Correct 24 ms 32276 KB Output is correct
47 Correct 26 ms 32476 KB Output is correct
48 Correct 25 ms 32452 KB Output is correct
49 Correct 39 ms 33660 KB Output is correct
50 Correct 189 ms 42992 KB Output is correct
51 Correct 36 ms 32788 KB Output is correct
52 Correct 153 ms 40236 KB Output is correct
53 Correct 35 ms 33524 KB Output is correct
54 Correct 169 ms 41640 KB Output is correct
55 Correct 29 ms 33228 KB Output is correct
56 Correct 30 ms 33228 KB Output is correct
57 Correct 30 ms 33156 KB Output is correct
58 Correct 29 ms 33260 KB Output is correct
59 Correct 26 ms 32636 KB Output is correct
60 Correct 130 ms 37588 KB Output is correct
61 Correct 29 ms 32460 KB Output is correct
62 Correct 170 ms 42052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 31564 KB Output is correct
2 Correct 20 ms 31564 KB Output is correct
3 Correct 20 ms 31620 KB Output is correct
4 Correct 20 ms 31564 KB Output is correct
5 Correct 20 ms 31564 KB Output is correct
6 Correct 20 ms 31540 KB Output is correct
7 Correct 21 ms 31564 KB Output is correct
8 Correct 21 ms 31672 KB Output is correct
9 Correct 20 ms 31564 KB Output is correct
10 Correct 20 ms 31564 KB Output is correct
11 Correct 20 ms 31624 KB Output is correct
12 Correct 20 ms 31576 KB Output is correct
13 Correct 20 ms 31620 KB Output is correct
14 Correct 19 ms 31564 KB Output is correct
15 Correct 20 ms 31640 KB Output is correct
16 Correct 20 ms 31600 KB Output is correct
17 Correct 20 ms 31564 KB Output is correct
18 Correct 20 ms 31564 KB Output is correct
19 Correct 19 ms 31540 KB Output is correct
20 Correct 19 ms 31660 KB Output is correct
21 Correct 23 ms 31624 KB Output is correct
22 Correct 20 ms 31560 KB Output is correct
23 Correct 20 ms 31564 KB Output is correct
24 Correct 22 ms 31564 KB Output is correct
25 Correct 20 ms 31560 KB Output is correct
26 Correct 19 ms 31656 KB Output is correct
27 Correct 20 ms 31564 KB Output is correct
28 Correct 20 ms 31636 KB Output is correct
29 Correct 20 ms 31552 KB Output is correct
30 Correct 20 ms 31564 KB Output is correct
31 Correct 20 ms 31596 KB Output is correct
32 Correct 20 ms 31568 KB Output is correct
33 Correct 21 ms 31664 KB Output is correct
34 Correct 24 ms 31664 KB Output is correct
35 Correct 141 ms 38376 KB Output is correct
36 Correct 187 ms 42800 KB Output is correct
37 Correct 183 ms 43016 KB Output is correct
38 Correct 181 ms 42436 KB Output is correct
39 Correct 24 ms 32232 KB Output is correct
40 Correct 26 ms 32588 KB Output is correct
41 Correct 28 ms 32588 KB Output is correct
42 Correct 26 ms 32204 KB Output is correct
43 Correct 24 ms 32220 KB Output is correct
44 Correct 25 ms 32204 KB Output is correct
45 Correct 24 ms 32292 KB Output is correct
46 Correct 24 ms 32276 KB Output is correct
47 Correct 26 ms 32476 KB Output is correct
48 Correct 25 ms 32452 KB Output is correct
49 Correct 39 ms 33660 KB Output is correct
50 Correct 189 ms 42992 KB Output is correct
51 Correct 36 ms 32788 KB Output is correct
52 Correct 153 ms 40236 KB Output is correct
53 Correct 35 ms 33524 KB Output is correct
54 Correct 169 ms 41640 KB Output is correct
55 Correct 29 ms 33228 KB Output is correct
56 Correct 30 ms 33228 KB Output is correct
57 Correct 30 ms 33156 KB Output is correct
58 Correct 29 ms 33260 KB Output is correct
59 Correct 26 ms 32636 KB Output is correct
60 Correct 130 ms 37588 KB Output is correct
61 Correct 29 ms 32460 KB Output is correct
62 Correct 170 ms 42052 KB Output is correct
63 Correct 818 ms 112992 KB Output is correct
64 Correct 817 ms 113116 KB Output is correct
65 Correct 821 ms 112964 KB Output is correct
66 Correct 429 ms 66568 KB Output is correct
67 Correct 968 ms 95960 KB Output is correct
68 Correct 422 ms 65720 KB Output is correct
69 Correct 591 ms 63540 KB Output is correct
70 Correct 442 ms 66516 KB Output is correct
71 Correct 424 ms 66560 KB Output is correct
72 Correct 796 ms 76984 KB Output is correct
73 Correct 833 ms 78900 KB Output is correct
74 Correct 1711 ms 111468 KB Output is correct
75 Correct 982 ms 77436 KB Output is correct
76 Correct 1223 ms 95884 KB Output is correct
77 Correct 1218 ms 96340 KB Output is correct
78 Correct 452 ms 71152 KB Output is correct
79 Correct 751 ms 82104 KB Output is correct
80 Correct 479 ms 70060 KB Output is correct
81 Correct 782 ms 78008 KB Output is correct
82 Correct 1193 ms 115764 KB Output is correct
83 Correct 1157 ms 115888 KB Output is correct
84 Correct 1081 ms 115768 KB Output is correct
85 Correct 1063 ms 115784 KB Output is correct
86 Correct 734 ms 100652 KB Output is correct
87 Correct 779 ms 102880 KB Output is correct
88 Correct 824 ms 78008 KB Output is correct
89 Correct 1183 ms 91472 KB Output is correct