답안 #269689

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
269689 2020-08-17T08:33:05 Z 임성재(#5102) 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
17 / 100
5000 ms 44828 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define em emplace
#define eb emplace_back
#define mp make_pair

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF = 1e18;
const int inf = 1e9;
const ll Mod = 1e9 + 7;

int n, m;
int a, b;
int p[100010];
ll sz[100010];
set<int> in[100010], out[100010];
int rnk[100010];
ll ans;
vector<pii> q;

int Find(int a) {
	return p[a] = a == p[a] ? a : Find(p[a]);
}

void Union(int a, int b) {
	a = Find(a);
	b = Find(b);

	if(a == b) return;

	if(rnk[a] < rnk[b]) swap(a, b);
	if(rnk[a] == rnk[b]) rnk[a]++;

	ans -= sz[a] * (sz[a]-1) + sz[b] * (sz[b] - 1);
	ans -= sz[a] * in[a].size();
	ans -= sz[b] * in[b].size();

	p[b] = a;
	sz[a] += sz[b];
	sz[b] = 0;

	ans += sz[a] * (sz[a] - 1);

	for(auto i : out[b]) {
		if(Find(i) == a) continue;

		out[a].insert(Find(i));
		if(out[Find(i)].find(a) != out[Find(i)].end()) {
			q.eb(a, Find(i));
		}
	}

	for(auto i : in[b]) {
		if(Find(i) == a) continue;

		in[a].insert(i);
		out[Find(i)].insert(a);

		if(out[a].find(Find(i)) != out[a].end()) {
			q.eb(a, Find(i));
		}
	}

	vector<int> v;

	for(auto i : in[a]) {
		if(Find(i) == a) v.eb(i);
	}

	for(auto i : v) in[a].erase(i);

	ans += sz[a] * in[a].size();
}

int main() {
	fast;

	cin >> n >> m;

	for(int i=1; i<=n; i++)
		p[i] = i, sz[i] = 1;

	for(int i=1; i<=m; i++) {
		cin >> a >> b;

		if(Find(a) == Find(b) || in[Find(b)].find(a) != in[Find(b)].end()) {
			cout << ans << "\n";
			continue;
		}

		in[Find(b)].insert(a);
		out[Find(a)].insert(Find(b));

		ans += sz[Find(b)];

		if(out[Find(b)].find(Find(a)) != out[Find(b)].end()) {
			q.eb(a, b);
		}

		while(q.size()) {
			auto x = q.back();
			q.pop_back();

			Union(x.fi, x.se);
		}

		cout << ans << "\n";
	}

}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 8 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 7 ms 9856 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 8 ms 9728 KB Output is correct
12 Correct 8 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 9 ms 9728 KB Output is correct
18 Correct 7 ms 9728 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 9728 KB Output is correct
21 Correct 7 ms 9856 KB Output is correct
22 Correct 7 ms 9728 KB Output is correct
23 Correct 7 ms 9728 KB Output is correct
24 Correct 7 ms 9728 KB Output is correct
25 Correct 9 ms 9728 KB Output is correct
26 Correct 8 ms 9728 KB Output is correct
27 Correct 8 ms 9728 KB Output is correct
28 Correct 6 ms 9728 KB Output is correct
29 Correct 7 ms 9728 KB Output is correct
30 Correct 6 ms 9728 KB Output is correct
31 Correct 7 ms 9728 KB Output is correct
32 Correct 7 ms 9760 KB Output is correct
33 Correct 8 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 8 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 7 ms 9856 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 8 ms 9728 KB Output is correct
12 Correct 8 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 9 ms 9728 KB Output is correct
18 Correct 7 ms 9728 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 9728 KB Output is correct
21 Correct 7 ms 9856 KB Output is correct
22 Correct 7 ms 9728 KB Output is correct
23 Correct 7 ms 9728 KB Output is correct
24 Correct 7 ms 9728 KB Output is correct
25 Correct 9 ms 9728 KB Output is correct
26 Correct 8 ms 9728 KB Output is correct
27 Correct 8 ms 9728 KB Output is correct
28 Correct 6 ms 9728 KB Output is correct
29 Correct 7 ms 9728 KB Output is correct
30 Correct 6 ms 9728 KB Output is correct
31 Correct 7 ms 9728 KB Output is correct
32 Correct 7 ms 9760 KB Output is correct
33 Correct 8 ms 9728 KB Output is correct
34 Correct 10 ms 9856 KB Output is correct
35 Correct 95 ms 15352 KB Output is correct
36 Correct 148 ms 17720 KB Output is correct
37 Correct 137 ms 17524 KB Output is correct
38 Correct 134 ms 17276 KB Output is correct
39 Correct 10 ms 10112 KB Output is correct
40 Correct 11 ms 10240 KB Output is correct
41 Correct 10 ms 10240 KB Output is correct
42 Correct 9 ms 10112 KB Output is correct
43 Correct 20 ms 10112 KB Output is correct
44 Correct 19 ms 10112 KB Output is correct
45 Correct 10 ms 10112 KB Output is correct
46 Correct 9 ms 10112 KB Output is correct
47 Correct 10 ms 10240 KB Output is correct
48 Correct 11 ms 10240 KB Output is correct
49 Correct 23 ms 10880 KB Output is correct
50 Correct 136 ms 17752 KB Output is correct
51 Correct 23 ms 10360 KB Output is correct
52 Correct 123 ms 16348 KB Output is correct
53 Correct 23 ms 10752 KB Output is correct
54 Correct 130 ms 17016 KB Output is correct
55 Correct 20 ms 10368 KB Output is correct
56 Correct 20 ms 10368 KB Output is correct
57 Correct 18 ms 10368 KB Output is correct
58 Correct 18 ms 10360 KB Output is correct
59 Correct 9 ms 10112 KB Output is correct
60 Correct 93 ms 15132 KB Output is correct
61 Correct 13 ms 10368 KB Output is correct
62 Correct 133 ms 17140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 8 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 7 ms 9856 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 8 ms 9728 KB Output is correct
12 Correct 8 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 9 ms 9728 KB Output is correct
18 Correct 7 ms 9728 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 9728 KB Output is correct
21 Correct 7 ms 9856 KB Output is correct
22 Correct 7 ms 9728 KB Output is correct
23 Correct 7 ms 9728 KB Output is correct
24 Correct 7 ms 9728 KB Output is correct
25 Correct 9 ms 9728 KB Output is correct
26 Correct 8 ms 9728 KB Output is correct
27 Correct 8 ms 9728 KB Output is correct
28 Correct 6 ms 9728 KB Output is correct
29 Correct 7 ms 9728 KB Output is correct
30 Correct 6 ms 9728 KB Output is correct
31 Correct 7 ms 9728 KB Output is correct
32 Correct 7 ms 9760 KB Output is correct
33 Correct 8 ms 9728 KB Output is correct
34 Correct 10 ms 9856 KB Output is correct
35 Correct 95 ms 15352 KB Output is correct
36 Correct 148 ms 17720 KB Output is correct
37 Correct 137 ms 17524 KB Output is correct
38 Correct 134 ms 17276 KB Output is correct
39 Correct 10 ms 10112 KB Output is correct
40 Correct 11 ms 10240 KB Output is correct
41 Correct 10 ms 10240 KB Output is correct
42 Correct 9 ms 10112 KB Output is correct
43 Correct 20 ms 10112 KB Output is correct
44 Correct 19 ms 10112 KB Output is correct
45 Correct 10 ms 10112 KB Output is correct
46 Correct 9 ms 10112 KB Output is correct
47 Correct 10 ms 10240 KB Output is correct
48 Correct 11 ms 10240 KB Output is correct
49 Correct 23 ms 10880 KB Output is correct
50 Correct 136 ms 17752 KB Output is correct
51 Correct 23 ms 10360 KB Output is correct
52 Correct 123 ms 16348 KB Output is correct
53 Correct 23 ms 10752 KB Output is correct
54 Correct 130 ms 17016 KB Output is correct
55 Correct 20 ms 10368 KB Output is correct
56 Correct 20 ms 10368 KB Output is correct
57 Correct 18 ms 10368 KB Output is correct
58 Correct 18 ms 10360 KB Output is correct
59 Correct 9 ms 10112 KB Output is correct
60 Correct 93 ms 15132 KB Output is correct
61 Correct 13 ms 10368 KB Output is correct
62 Correct 133 ms 17140 KB Output is correct
63 Correct 467 ms 44644 KB Output is correct
64 Correct 462 ms 44692 KB Output is correct
65 Correct 492 ms 44828 KB Output is correct
66 Correct 260 ms 29432 KB Output is correct
67 Correct 373 ms 34680 KB Output is correct
68 Correct 255 ms 29468 KB Output is correct
69 Execution timed out 5048 ms 18180 KB Time limit exceeded
70 Halted 0 ms 0 KB -