Submission #245579

# Submission time Handle Problem Language Result Execution time Memory
245579 2020-07-06T19:56:58 Z kostia244 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
4081 ms 116192 KB
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,ssse3,popcnt")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
struct edge {
	int f, t, ri, r = 0;
	edge(int f, int t, int ri) : f(f), t(t), ri(ri) {}
};
struct dsu {
	ll ans = 0, T = 69;
	vector<int> r, p, w, tbd;
	vector<vector<int>> g, gr;
	map<pair<int, int>, int> ctc, rtc;
	vector<edge> ve;
	queue<int> mrg;
	int have(int i, int j) { return ctc[{i, j}]; }
	void add(int i, int j) { ctc[{i, j}]++; }
	void rem(int i, int j) { ctc[{i, j}]--; }
	void iadd(int i, int j) { rtc[{i, j}]++; }
	void irem(int i, int j) { rtc[{i, j}]--; }
	int ihave(int i, int j) { return rtc[{i, j}]; }
	dsu(int n) : r(n+1, 1), p(n+1), g(n+1), gr(n+1), w(n+1), tbd(n+1) {iota(all(p), 0);}
	int par(int i) {
		return p[i] == i ? i : p[i] = par(p[i]);
	}
	int unite(int i, int j) {
		//cout << i << " " << j << endl;
		if(r[i] < r[j]) swap(i, j);
		//cout << i << " " << j << " | " << r[i] << " " << r[j] << " " << w[i] << " " << w[j] << endl;
		ans -= r[i]*1ll*(r[i]+w[i]-1) + r[j]*1ll*(r[j]+w[j]-1);
		//cout << ans << endl;
		w[i] += w[j];
		r[i] += r[j];
		p[j] = i;
		for(int U = 0; U < 2; U++)
		for(auto &x : (U?gr:g)[j]) {
			auto &e = ve[x];
			if(e.r) continue;
			rem(e.f, e.t);
			irem(e.ri, e.t);
			if(U) e.t = i;
			else e.f = i;
			int p = U?e.f:e.t;assert(p == (e.f^e.t^i));
			if(tbd[p] != T && have(e.t, e.f)) {
				mrg.push(p);
				tbd[p] = T;
			}
			if(ihave(e.ri, e.t) || e.f == e.t) {e.r = 1, w[i]--; continue;}//fucking useless
			(U?gr:g)[i].push_back(x);
			add(e.f, e.t);
			iadd(e.ri, e.t);
		}
		//cout << r[i] << " " << w[i] << endl;
		ans += r[i]*1ll*(r[i]+w[i]-1);
		return i;
	}
	void adde(int i, int j) {
		++T;
		int ri = i;
		//cout << i << " " << j << " ";
		i = par(i), j = par(j);
		//cout << i << " " << j << "\n";
		if(i == j) return;
		if(ihave(ri, j)) return;
		w[j]++, ans += r[j];
		ve.emplace_back(i, j, ri);
		g[i].push_back(ve.size()-1);
		gr[j].push_back(ve.size()-1);
		add(i, j);
		iadd(ri, j);
		if(have(j, i)) {
			int cur = i;
			tbd[i] = tbd[j] = T;
			mrg.push(j);
			while(!mrg.empty()) {
				cur = unite(cur, mrg.front());
				mrg.pop();
			}
		}
	}
};
int main() {
	cin.tie(0)->sync_with_stdio(0);
	int n, m;
	cin >> n >> m;
	dsu d(n);
	for(int f, t, i = 0; i < m; i++) {
		cin >> f >> t;
		d.adde(f, t);
		cout << d.ans << endl;
	}
}

Compilation message

joitter2.cpp: In constructor 'dsu::dsu(int)':
joitter2.cpp:14:25: warning: 'dsu::gr' will be initialized after [-Wreorder]
  vector<vector<int>> g, gr;
                         ^~
joitter2.cpp:13:20: warning:   'std::vector<int> dsu::w' [-Wreorder]
  vector<int> r, p, w, tbd;
                    ^
joitter2.cpp:24:2: warning:   when initialized here [-Wreorder]
  dsu(int n) : r(n+1, 1), p(n+1), g(n+1), gr(n+1), w(n+1), tbd(n+1) {iota(all(p), 0);}
  ^~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 10 ms 384 KB Output is correct
8 Correct 11 ms 384 KB Output is correct
9 Correct 10 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 6 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 10 ms 384 KB Output is correct
22 Correct 6 ms 384 KB Output is correct
23 Correct 7 ms 384 KB Output is correct
24 Correct 6 ms 384 KB Output is correct
25 Correct 9 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 6 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 9 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 9 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 10 ms 384 KB Output is correct
8 Correct 11 ms 384 KB Output is correct
9 Correct 10 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 6 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 10 ms 384 KB Output is correct
22 Correct 6 ms 384 KB Output is correct
23 Correct 7 ms 384 KB Output is correct
24 Correct 6 ms 384 KB Output is correct
25 Correct 9 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 6 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 9 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 9 ms 384 KB Output is correct
34 Correct 26 ms 512 KB Output is correct
35 Correct 633 ms 7148 KB Output is correct
36 Correct 719 ms 11468 KB Output is correct
37 Correct 720 ms 11764 KB Output is correct
38 Correct 687 ms 10980 KB Output is correct
39 Correct 19 ms 1280 KB Output is correct
40 Correct 21 ms 1536 KB Output is correct
41 Correct 22 ms 1536 KB Output is correct
42 Correct 18 ms 1280 KB Output is correct
43 Correct 27 ms 1280 KB Output is correct
44 Correct 18 ms 1280 KB Output is correct
45 Correct 24 ms 1272 KB Output is correct
46 Correct 18 ms 1280 KB Output is correct
47 Correct 23 ms 1536 KB Output is correct
48 Correct 23 ms 1664 KB Output is correct
49 Correct 49 ms 2936 KB Output is correct
50 Correct 719 ms 11856 KB Output is correct
51 Correct 36 ms 1784 KB Output is correct
52 Correct 658 ms 9204 KB Output is correct
53 Correct 49 ms 2688 KB Output is correct
54 Correct 675 ms 10224 KB Output is correct
55 Correct 25 ms 1792 KB Output is correct
56 Correct 25 ms 1792 KB Output is correct
57 Correct 26 ms 1920 KB Output is correct
58 Correct 24 ms 1792 KB Output is correct
59 Correct 19 ms 1280 KB Output is correct
60 Correct 638 ms 6288 KB Output is correct
61 Correct 26 ms 1792 KB Output is correct
62 Correct 717 ms 10792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 10 ms 384 KB Output is correct
8 Correct 11 ms 384 KB Output is correct
9 Correct 10 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 6 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 10 ms 384 KB Output is correct
22 Correct 6 ms 384 KB Output is correct
23 Correct 7 ms 384 KB Output is correct
24 Correct 6 ms 384 KB Output is correct
25 Correct 9 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 6 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 9 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 9 ms 384 KB Output is correct
34 Correct 26 ms 512 KB Output is correct
35 Correct 633 ms 7148 KB Output is correct
36 Correct 719 ms 11468 KB Output is correct
37 Correct 720 ms 11764 KB Output is correct
38 Correct 687 ms 10980 KB Output is correct
39 Correct 19 ms 1280 KB Output is correct
40 Correct 21 ms 1536 KB Output is correct
41 Correct 22 ms 1536 KB Output is correct
42 Correct 18 ms 1280 KB Output is correct
43 Correct 27 ms 1280 KB Output is correct
44 Correct 18 ms 1280 KB Output is correct
45 Correct 24 ms 1272 KB Output is correct
46 Correct 18 ms 1280 KB Output is correct
47 Correct 23 ms 1536 KB Output is correct
48 Correct 23 ms 1664 KB Output is correct
49 Correct 49 ms 2936 KB Output is correct
50 Correct 719 ms 11856 KB Output is correct
51 Correct 36 ms 1784 KB Output is correct
52 Correct 658 ms 9204 KB Output is correct
53 Correct 49 ms 2688 KB Output is correct
54 Correct 675 ms 10224 KB Output is correct
55 Correct 25 ms 1792 KB Output is correct
56 Correct 25 ms 1792 KB Output is correct
57 Correct 26 ms 1920 KB Output is correct
58 Correct 24 ms 1792 KB Output is correct
59 Correct 19 ms 1280 KB Output is correct
60 Correct 638 ms 6288 KB Output is correct
61 Correct 26 ms 1792 KB Output is correct
62 Correct 717 ms 10792 KB Output is correct
63 Correct 2280 ms 80088 KB Output is correct
64 Correct 2204 ms 80088 KB Output is correct
65 Correct 2168 ms 80072 KB Output is correct
66 Correct 1119 ms 46360 KB Output is correct
67 Correct 1858 ms 61252 KB Output is correct
68 Correct 1045 ms 46472 KB Output is correct
69 Correct 1318 ms 46176 KB Output is correct
70 Correct 1116 ms 46536 KB Output is correct
71 Correct 1152 ms 46296 KB Output is correct
72 Correct 1922 ms 61028 KB Output is correct
73 Correct 2106 ms 61656 KB Output is correct
74 Correct 4081 ms 116192 KB Output is correct
75 Correct 2153 ms 57540 KB Output is correct
76 Correct 3044 ms 85956 KB Output is correct
77 Correct 3004 ms 85984 KB Output is correct
78 Correct 937 ms 44732 KB Output is correct
79 Correct 1668 ms 57056 KB Output is correct
80 Correct 901 ms 43032 KB Output is correct
81 Correct 1516 ms 52832 KB Output is correct
82 Correct 2668 ms 77236 KB Output is correct
83 Correct 2650 ms 77280 KB Output is correct
84 Correct 2302 ms 77204 KB Output is correct
85 Correct 2360 ms 77028 KB Output is correct
86 Correct 1307 ms 48480 KB Output is correct
87 Correct 1545 ms 50896 KB Output is correct
88 Correct 1951 ms 62264 KB Output is correct
89 Correct 3022 ms 83472 KB Output is correct