Submission #97536

# Submission time Handle Problem Language Result Execution time Memory
97536 2019-02-16T21:36:55 Z jasony123123 Pipes (CEOI15_pipes) C++11
100 / 100
1198 ms 15668 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <array>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define mt make_tuple
#define mp make_pair
#define v vector
#define sf scanf
#define pf printf
#define dvar(x) cout << #x << " := " << x << "\n"
#define darr(x,n) FOR(i,0,n) cout << #x << "[" << i << "]" << " := " << x[i] << "\n"

typedef long long ll;
typedef long double ld;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }

void io() {
#ifdef LOCAL_PROJECT 
	freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#else
#endif
	ios_base::sync_with_stdio(false); cin.tie(NULL);
}

const ll INF = 1e14;
/***********************CEOI 2015 D1 Pipes*****************************************/

template<int NSZ, int ESZ> struct BCC {
	int NN, MM; // Nodes, Edges

	void init(int n, int m) {
		NN = n, MM = m;
	}

	v<pii> adj[NSZ];
	int edge[ESZ][2];

	void addEdge(int a, int b, int id) {
		edge[id][0] = a, edge[id][1] = b;
		adj[a].push_back({ b,id }), adj[b].push_back({ a,id });
	}

	bool bridge[ESZ];
	int lo[NSZ], ind[NSZ], t = 0;

	void tarjan(int cur, int par = -1) {
		ind[cur] = lo[cur] = t++;
		for (pii yi : adj[cur]) {
			int nex = yi.first, id = yi.second;
			if (id == par) continue;
			if (ind[nex] == -1) {
				tarjan(nex, id);
				lo[cur] = min(lo[cur], lo[nex]);
				if (lo[nex]>ind[cur]) bridge[id] = 1;
			}
			else lo[cur] = min(lo[cur], ind[nex]);
		}
	}

	void findBridges() {
		memset(ind, -1, sizeof ind);
		memset(lo, 0, sizeof lo);
		memset(bridge, 0, sizeof bridge);
		FOR(i, 0, NN) if (ind[i] == -1)
			tarjan(i);
	}
};

template <int SZ> struct UnionFind {
	int par[SZ], compsz[SZ];
	UnionFind() {
		FOR(i, 0, SZ) par[i] = i, compsz[i] = 1;
	}

	int find(int x) {
		if (par[x] != x) par[x] = find(par[x]);
		return par[x];
	}

	bool unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y) return false;

		if (compsz[x] < compsz[y]) swap(x, y); // now x is the larger component
		compsz[x] += compsz[y];
		par[y] = x;
		return true;
	}
};

int n, m, esz = 0;
UnionFind<100001> uf1, uf2;
BCC<100001, 200001> bcc;

int main() {
	io();
	cin >> n >> m;

	FOR(i, 0, m) {
		int a, b; cin >> a >> b;
		if (uf1.unite(a, b) || uf2.unite(a, b)) {
			bcc.addEdge(a, b, esz++);
		//	cout << a << " " << b << " - \n";
		}
	}

	bcc.init(n + 1, esz);
	bcc.findBridges();
	FOR(i, 0, esz) if (bcc.bridge[i]) {
		cout << bcc.edge[i][0] << " " << bcc.edge[i][1] << "\n";
	}

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5248 KB Output is correct
2 Correct 5 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 5632 KB Output is correct
2 Correct 9 ms 5632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 5624 KB Output is correct
2 Correct 96 ms 5516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 6216 KB Output is correct
2 Correct 191 ms 5936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 285 ms 7648 KB Output is correct
2 Correct 245 ms 7312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 419 ms 12548 KB Output is correct
2 Correct 381 ms 10824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 618 ms 13608 KB Output is correct
2 Correct 594 ms 10736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 822 ms 15668 KB Output is correct
2 Correct 793 ms 13228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1027 ms 15668 KB Output is correct
2 Correct 944 ms 13132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1198 ms 15036 KB Output is correct
2 Correct 1099 ms 12368 KB Output is correct