Submission #379407

# Submission time Handle Problem Language Result Execution time Memory
379407 2021-03-18T07:09:48 Z Return_0 Pipes (CEOI15_pipes) C++17
0 / 100
1231 ms 44980 KB
#pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;

#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()

cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;

template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
	out<< '['; for (int i = -1; ++i < ll(a.size());) out<< a[i] << (i + 1 < ll(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;

struct bridge_online {
	vector <ll> par, dsu_2ecc, dsu_cc, dsu_cc_size;
	ll bridges, lca_iteration;
	vector <ll> last_visit;
	set <pll> st;

	void init(ll n) {
		par.resize(n);
		dsu_2ecc.resize(n);
		dsu_cc.resize(n);
		dsu_cc_size.resize(n);
		lca_iteration = 0;
		last_visit.assign(n, 0);
		for (ll i = 0; i < n; ++i) {
			dsu_2ecc[i] = i;
			dsu_cc[i] = i;
			dsu_cc_size[i] = 1;
			par[i] = -1;
		}
		bridges = 0;
	}

	ll find_2ecc (ll v) {
		if (v == -1) return -1;
		return dsu_2ecc[v] == v ? v : dsu_2ecc[v] = find_2ecc(dsu_2ecc[v]);
	}

	ll find_cc(ll v) {
		v = find_2ecc(v);
		return dsu_cc[v] == v ? v : dsu_cc[v] = find_cc(dsu_cc[v]);
	}

	void make_root(ll v) {
		v = find_2ecc(v);
		ll root = v;
		ll child = -1;
		while (v != -1) {
			ll p = find_2ecc(par[v]);
			par[v] = child;
			dsu_cc[v] = root;
			child = v;
			v = p;
		}
		dsu_cc_size[root] = dsu_cc_size[child];
	}

	void merge_path (ll a, ll b) {
		++lca_iteration;
		vector<ll> path_a, path_b;
		ll lca = -1;
		while (lca == -1) {
			if (a != -1) {
				a = find_2ecc(a);
				path_a.push_back(a);
				if (last_visit[a] == lca_iteration){
					lca = a;
					break;
				}
				last_visit[a] = lca_iteration;
				a = par[a];
			}
			if (b != -1) {
				b = find_2ecc(b);
				path_b.push_back(b);
				if (last_visit[b] == lca_iteration){
					lca = b;
					break;
				}
				last_visit[b] = lca_iteration;
				b = par[b];
			}
		}

		for (ll i = 0; i < SZ(path_a); i++) {
			dsu_2ecc[path_a[i]] = lca;
			if (path_a[i] == lca) break;
			--bridges;
			if (path_a[i] < path_a[i + 1]) st.erase({path_a[i], path_a[i + 1]});
			else st.erase({path_a[i + 1], path_a[i]});
		}
		for (ll i = 0; i < SZ(path_b); i++) {
			dsu_2ecc[path_b[i]] = lca;
			if (path_b[i] == lca) break;
			--bridges;
			if (path_b[i] < path_b[i + 1]) st.erase({path_b[i], path_b[i + 1]});
			else st.erase({path_b[i + 1], path_b[i]});
		}
	}

	void add_edge(ll a, ll b) {
		a = find_2ecc(a);
		b = find_2ecc(b);
		if (a == b) return;

		ll ca = find_cc(a);
		ll cb = find_cc(b);

		if (ca != cb) {
			++bridges;
			if (a < b) st.insert({a, b});
			else st.insert({b, a});
			if (dsu_cc_size[ca] > dsu_cc_size[cb]) {
				swap(a, b);
				swap(ca, cb);
			}
			make_root(a);
			par[a] = dsu_cc[a] = b;
			dsu_cc_size[cb] += dsu_cc_size[a];
		} else {
			merge_path(a, b);
		}
	}
} ds;

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

	ll n, m;
	cin>> n >> m;

	ds.init(n);
	for (ll i = 0, v, u; i < m; i++) {
		cin>> v >> u;
		ds.add_edge(--v, --u);
	}

	for (auto &x : ds.st) cout<< x.fr + 1 << ' ' << x.sc + 1 << '\n';

	cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

	return 0;
}
/*

*/
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 748 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 110 ms 6124 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 197 ms 10476 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 307 ms 17644 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 427 ms 7248 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 661 ms 7708 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 865 ms 20260 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1123 ms 31852 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1231 ms 44980 KB Memory limit exceeded
2 Halted 0 ms 0 KB -