Submission #431277

# Submission time Handle Problem Language Result Execution time Memory
431277 2021-06-17T10:40:51 Z Kevin_Zhang_TW Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
20 ms 19532 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// My bug list :
// integer overflow
// 0based, 1based forgotten
// index out of bound
// n, m, i, j typo
// some cases are missing
 
const int MAX_N = 100010;
 
ll ans;
 
int n, m, g[MAX_N], sz[MAX_N], cost[MAX_N];

ll cntin[MAX_N];
 
set<tuple<int,int,int>> in[MAX_N], out[MAX_N];
 
void init() {
	iota(g, g+n+1, 0);
	fill(sz, sz + n + 1, 1);
}
 
int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); }

void rm(int x, int u) {
	--cost[x], --cost[u];
	ans -= sz[ F(u) ];
	--cntin[ F(u) ];
	in[F(u)].erase({F(x),x,u});
	out[F(x)].erase({F(u),x,u});
}
void ad(int x, int u) {
	++cost[x], ++cost[u];
	ans += sz[ F(u) ];
	++cntin[ F(u) ];
	in[F(u)].insert({F(x),x,u});
	out[F(x)].insert({F(u),x,u});
}

void kick(int a, int b) {
	auto it = out[F(a)].lower_bound({F(b), -1, -1});
	vector<pair<int,int>> vec;
	while (it != end(out[F(a)]) && get<0>(*it) == F(b)) {
		auto [q, w, e] = *it;
		vec.pb(w, e);
		it = out[F(a)].erase(it);
	}
	for (auto [x, y] : vec) rm(x, y);
}

void add_edge(int a, int b) {
	if (F(a) == F(b)) return;
	// if a already connected to group(b), then nothing happen
	auto it = in[F(b)].lower_bound({F(a), a, -1});
	if (it != end(in[F(b)])) {
		auto [g, x, y] = *it;
		DE(g, x, y);
	}
	if (it != end(in[F(b)]) && get<0>(*it) == F(a) && get<1>(*it) == a) return;
	
	ad(a, b);
	it = out[F(b)].lower_bound({F(a), -1, -1});
	if (it == end(out[F(b)]) || get<0>(*it) != F(a)) return;

	a = F(a), b = F(b);

	if (cost[a] > cost[b] * 2) swap(a, b);
	assert(cost[a] == in[a].size() + out[a].size() && cost[b] == in[b].size() + out[b].size());

	kick(a, b), kick(b, a);

	vector<pair<int,int>> kl;
	for (auto [_, x, y] : in[a]) kl.pb(x, y);
	for (auto [_, x, y] : out[a]) kl.pb(x, y);

	in[a].clear(), out[a].clear();

	for (auto [x, y] : kl) rm(x, y);

	ans += 2ll * sz[a] * sz[b] + 1ll * cntin[b] * sz[a];

	sz[b] += sz[a], g[a] = b;

	for (auto [x, y] : kl)
		add_edge(x, y);
}
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n >> m;
	vector<pair<int,int>> alle(m);
	for (auto &[a, b] : alle) cin >> a >> b;
	vector<ll> res;

	init();
	for (auto [a, b] : alle) {
		add_edge(a, b);
		res.pb(ans);
	}
	for (auto i : res) cout << i << '\n';
}

Compilation message

joitter2.cpp: In function 'void add_edge(int, int)':
joitter2.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
joitter2.cpp:73:3: note: in expansion of macro 'DE'
   73 |   DE(g, x, y);
      |   ^~
joitter2.cpp:72:8: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
   72 |   auto [g, x, y] = *it;
      |        ^~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from joitter2.cpp:1:
joitter2.cpp:84:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |  assert(cost[a] == in[a].size() + out[a].size() && cost[b] == in[b].size() + out[b].size());
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:84:60: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |  assert(cost[a] == in[a].size() + out[a].size() && cost[b] == in[b].size() + out[b].size());
      |                                                    ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 20 ms 19532 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 20 ms 19532 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 20 ms 19532 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -