답안 #852286

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
852286 2023-09-21T14:16:58 Z mat_jur 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
17 / 100
5000 ms 76844 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else 
#define debug(X) ;
#endif
#define ll long long
#define all(v) (v).begin(), (v).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define ROF(i,r,l) for(int i=(r);i>=(l);--i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back

class UF {
public:
	int N;
	vector<int> e;
	vector<set<int>> to;
	vector<set<int>> c;
	vector<set<int>> rev;
	queue<pair<int, int>> to_merge;
	ll res;
	UF(int n) {
		N = n;
		e.resize(N, -1);
		to.resize(N);
		c.resize(N);
		rev.resize(N);
		REP(i, N) c[i].insert(i);
		res = 0;
	}
	int get(int a) {return e[a]<0?a:e[a] = get(e[a]);}
	int sz(int a) {return -e[get(a)];}
	int all_sz(int a) {return ssize(rev[a])+ssize(to[a])+ssize(c[a]);}
	void addEdge(int a, int b) {
		a = get(a); b = get(b);
		if (a == b) return;
		to[a].insert(b);
		rev[b].insert(a);
		if (to[b].count(a)) to_merge.push(mp(a, b));
	}
	void merge(int a, int b) {
		cerr << "MERGE: " << a << ' ' << b << '\n';
		a = get(a); b = get(b);
		if (a == b) return;
		res += (ll)sz(a)*(ll)ssize(c[b])+(ll)sz(b)*(ll)ssize(c[a]);
		if (all_sz(a) > all_sz(b)) swap(a, b);
		for (int x : c[a]) {
			if (c[b].count(x)) res -= sz(a)+sz(b);
			else c[b].insert(x);
		}
		to[a].erase(b); to[b].erase(a);
		rev[b].erase(a); rev[a].erase(b);
		for (int x : to[a]) {
			rev[x].erase(a);
			addEdge(b, x);
		}
		for (int x : rev[a]) {
			to[x].erase(a);
			addEdge(x, b);
		}

		e[b] += e[a];
		e[a] = b;
	}
	void add(int a, int b) {
		cerr << a << ' ' << b << ' ' << get(a) << ' ' << get(b) << '\n';
		b = get(b);
		if (get(a) != b && !c[b].count(a)){
			c[b].insert(a);
			res += sz(b);
			a = get(a);
			addEdge(a, b);
			while (to_merge.size() > 0) {
				pair<int, int> p = to_merge.front();
				to_merge.pop();
				merge(p.fi, p.se);
			}
		}
	}
};

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

	int n, m;
	cin >> n >> m;
	UF u(n);
	REP(i, m) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		cerr << "NEW Q \n";
		u.add(a, b);	
		cout << u.res << '\n';
		debug(u.e);
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 2 ms 344 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 27 ms 348 KB Output is correct
8 Correct 27 ms 348 KB Output is correct
9 Correct 38 ms 344 KB Output is correct
10 Correct 3 ms 344 KB Output is correct
11 Correct 2 ms 344 KB Output is correct
12 Correct 3 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Correct 3 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 2 ms 344 KB Output is correct
20 Correct 6 ms 344 KB Output is correct
21 Correct 29 ms 344 KB Output is correct
22 Correct 4 ms 344 KB Output is correct
23 Correct 16 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 36 ms 344 KB Output is correct
26 Correct 2 ms 344 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 3 ms 344 KB Output is correct
29 Correct 3 ms 344 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 30 ms 344 KB Output is correct
32 Correct 2 ms 344 KB Output is correct
33 Correct 30 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 2 ms 344 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 27 ms 348 KB Output is correct
8 Correct 27 ms 348 KB Output is correct
9 Correct 38 ms 344 KB Output is correct
10 Correct 3 ms 344 KB Output is correct
11 Correct 2 ms 344 KB Output is correct
12 Correct 3 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Correct 3 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 2 ms 344 KB Output is correct
20 Correct 6 ms 344 KB Output is correct
21 Correct 29 ms 344 KB Output is correct
22 Correct 4 ms 344 KB Output is correct
23 Correct 16 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 36 ms 344 KB Output is correct
26 Correct 2 ms 344 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 3 ms 344 KB Output is correct
29 Correct 3 ms 344 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 30 ms 344 KB Output is correct
32 Correct 2 ms 344 KB Output is correct
33 Correct 30 ms 344 KB Output is correct
34 Correct 148 ms 592 KB Output is correct
35 Correct 3942 ms 10960 KB Output is correct
36 Correct 4046 ms 14384 KB Output is correct
37 Correct 3823 ms 14952 KB Output is correct
38 Correct 3902 ms 14448 KB Output is correct
39 Correct 59 ms 1104 KB Output is correct
40 Correct 82 ms 1432 KB Output is correct
41 Correct 61 ms 1356 KB Output is correct
42 Correct 64 ms 988 KB Output is correct
43 Correct 81 ms 1104 KB Output is correct
44 Correct 83 ms 1104 KB Output is correct
45 Correct 72 ms 1104 KB Output is correct
46 Correct 78 ms 1140 KB Output is correct
47 Correct 79 ms 1360 KB Output is correct
48 Correct 83 ms 1140 KB Output is correct
49 Correct 187 ms 2192 KB Output is correct
50 Correct 3575 ms 15372 KB Output is correct
51 Correct 124 ms 1540 KB Output is correct
52 Correct 3305 ms 13124 KB Output is correct
53 Correct 137 ms 1876 KB Output is correct
54 Correct 3368 ms 13600 KB Output is correct
55 Correct 62 ms 1900 KB Output is correct
56 Correct 59 ms 1584 KB Output is correct
57 Correct 59 ms 1420 KB Output is correct
58 Correct 58 ms 1360 KB Output is correct
59 Correct 62 ms 1360 KB Output is correct
60 Correct 3289 ms 11624 KB Output is correct
61 Correct 64 ms 1360 KB Output is correct
62 Correct 3348 ms 13944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 2 ms 344 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 27 ms 348 KB Output is correct
8 Correct 27 ms 348 KB Output is correct
9 Correct 38 ms 344 KB Output is correct
10 Correct 3 ms 344 KB Output is correct
11 Correct 2 ms 344 KB Output is correct
12 Correct 3 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Correct 3 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 2 ms 344 KB Output is correct
20 Correct 6 ms 344 KB Output is correct
21 Correct 29 ms 344 KB Output is correct
22 Correct 4 ms 344 KB Output is correct
23 Correct 16 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 36 ms 344 KB Output is correct
26 Correct 2 ms 344 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 3 ms 344 KB Output is correct
29 Correct 3 ms 344 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 30 ms 344 KB Output is correct
32 Correct 2 ms 344 KB Output is correct
33 Correct 30 ms 344 KB Output is correct
34 Correct 148 ms 592 KB Output is correct
35 Correct 3942 ms 10960 KB Output is correct
36 Correct 4046 ms 14384 KB Output is correct
37 Correct 3823 ms 14952 KB Output is correct
38 Correct 3902 ms 14448 KB Output is correct
39 Correct 59 ms 1104 KB Output is correct
40 Correct 82 ms 1432 KB Output is correct
41 Correct 61 ms 1356 KB Output is correct
42 Correct 64 ms 988 KB Output is correct
43 Correct 81 ms 1104 KB Output is correct
44 Correct 83 ms 1104 KB Output is correct
45 Correct 72 ms 1104 KB Output is correct
46 Correct 78 ms 1140 KB Output is correct
47 Correct 79 ms 1360 KB Output is correct
48 Correct 83 ms 1140 KB Output is correct
49 Correct 187 ms 2192 KB Output is correct
50 Correct 3575 ms 15372 KB Output is correct
51 Correct 124 ms 1540 KB Output is correct
52 Correct 3305 ms 13124 KB Output is correct
53 Correct 137 ms 1876 KB Output is correct
54 Correct 3368 ms 13600 KB Output is correct
55 Correct 62 ms 1900 KB Output is correct
56 Correct 59 ms 1584 KB Output is correct
57 Correct 59 ms 1420 KB Output is correct
58 Correct 58 ms 1360 KB Output is correct
59 Correct 62 ms 1360 KB Output is correct
60 Correct 3289 ms 11624 KB Output is correct
61 Correct 64 ms 1360 KB Output is correct
62 Correct 3348 ms 13944 KB Output is correct
63 Correct 3794 ms 73272 KB Output is correct
64 Correct 3798 ms 73052 KB Output is correct
65 Correct 3744 ms 73320 KB Output is correct
66 Correct 2891 ms 39448 KB Output is correct
67 Correct 3104 ms 65956 KB Output is correct
68 Correct 2913 ms 39092 KB Output is correct
69 Correct 3062 ms 40272 KB Output is correct
70 Correct 2915 ms 39232 KB Output is correct
71 Correct 2935 ms 39180 KB Output is correct
72 Correct 3136 ms 50064 KB Output is correct
73 Correct 3123 ms 51780 KB Output is correct
74 Execution timed out 5029 ms 76844 KB Time limit exceeded
75 Halted 0 ms 0 KB -