Submission #668746

# Submission time Handle Problem Language Result Execution time Memory
668746 2022-12-04T20:59:31 Z AmirAli_H1 Pipes (CEOI15_pipes) C++17
80 / 100
901 ms 31576 KB
// In the name of Allah

#include <bits/stdc++.h>
using namespace std;

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

#define all(x)		(x).begin(),(x).end()
#define len(x)		((ll) (x).size())
#define F		first
#define S		second
#define pb		push_back
#define sep             ' '
#define endl            '\n'
#define Mp		make_pair
#define debug(x)	cerr << #x << ": " <<  x << endl;
#define kill(x)		cout << x << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define file_io(x,y)	freopen(x, "r", stdin); freopen(y, "w", stdout);

int n, m;
const int maxn = 1e5 + 5;
int *p, *sz;
int p1[maxn], sz1[maxn];
int p2[maxn], sz2[maxn];
vector<int> adj[maxn];
bitset<maxn> mark;
int h[maxn], mn[maxn];

void dfs(int v, int p = -1) {
	mark[v] = 1; mn[v] = h[v];
	int cnt = 0;
	for (int u : adj[v]) {
		if (!mark[u]) {
			h[u] = h[v] + 1;
			dfs(u, v);
			mn[v] = min(mn[v], mn[u]);
		}
		else {
			if (u != p) mn[v] = min(mn[v], h[u]);
			else cnt++;
		}
	}
	if (cnt >= 2) mn[v] = min(mn[v], h[p]);
	
	if (mn[v] >= h[v] && p != -1) {
		cout << min(p, v) + 1 << sep << max(p, v) + 1 << endl;
	}
}

int get(int a) {
	return ((p[a] == a) ? a : p[a] = get(p[a]));
}

void merge(int a, int b) {
	a = get(a); b = get(b);
	if (a == b) return ;
	if (sz[a] > sz[b]) swap(a, b);
	p[a] = b; sz[b] += sz[a]; sz[a] = 0;
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	cin >> n >> m;
	
	iota(p1, p1 + n, 0); fill(sz1, sz1 + n, 1);
	iota(p2, p2 + n, 0); fill(sz2, sz2 + n, 1);
	
	for (int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v; u--; v--;
		bool flag = 0;
		p = p1; sz = sz1;
		if (!flag && get(u) != get(v)) {
			flag = 1;
			merge(u, v);
			adj[u].pb(v); adj[v].pb(u);
		}
		p = p2; sz = sz2;
		if (!flag && get(u) != get(v)) {
			flag = 1;
			merge(u, v);
			adj[u].pb(v); adj[v].pb(u);
		}
	}
	
	for (int i = 0; i < n; i++) {
		if (!mark[i]) dfs(i);
	}
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2688 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3156 KB Output is correct
2 Correct 4 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 3020 KB Output is correct
2 Correct 80 ms 2876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 3776 KB Output is correct
2 Correct 152 ms 3328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 5536 KB Output is correct
2 Correct 195 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 313 ms 11124 KB Output is correct
2 Correct 267 ms 7612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 472 ms 12424 KB Output is correct
2 Correct 459 ms 9544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 624 ms 14744 KB Output is correct
2 Runtime error 674 ms 24904 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 735 ms 14760 KB Output is correct
2 Runtime error 823 ms 31576 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 901 ms 14084 KB Output is correct
2 Correct 893 ms 11296 KB Output is correct