답안 #874394

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
874394 2023-11-16T20:46:16 Z Hamed_Ghaffari Pipes (CEOI15_pipes) C++17
50 / 100
777 ms 45652 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long       ll;
typedef pair<int, int>  pii;

#define FastIO          cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false);
#define FileIO          freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define F               first
#define S               second
#define max_heap(T)     priority_queue<T>
#define min_heap(T)     priority_queue<T, vector<T>, greater<T>>
#define fr(i,a,b)       for(int i=a;i<b;i++)
#define frr(i,a,b)      for(int i=a;i>b;i--)
#define frin(i,A)       for(auto &i : A)
#define frp(a,b,A)      for(auto [a, b] : A)
#define SZ(x)           (int)x.size()
#define all(A)          A.begin(), A.end()
#define mins(a,b)       a = min(a,b)
#define maxs(a,b)       a = max(a,b)
#define pb              push_back
#define kill(x)         cout << x << '\n', exit(0)
#define md(a)           (a%MOD+MOD)%MOD

const ll  INF  = 2e9;
const ll  MOD  = 1e9 + 7;
const int MAXN = 1e5 + 1;
const int LOG  = 30;

int n, m, par[MAXN][2], hi[MAXN], mn[MAXN], edge;
vector<pii> g[MAXN];

void DSU() {
	fr(i, 1, n+1)
		par[i][0] = par[i][1] = i;
}
int Find(int v, int x) { return v == par[v][x] ? v : par[v][x] = Find(par[v][x], x); }
bool Union(int u, int v) {
	int u0 = Find(u, 0), v0 = Find(v, 0);
	if(u0 == v0) {
		int u1 = Find(u, 1), v1 = Find(v, 1);
		if(u1 == v1)
			return 0;
		par[u1][1] = v1;
		edge++;
		return 1;
	}
	par[u0][0] = v0;
	edge++;
	return 1;
}

void dfs(int v, int p, int pid) {
	hi[v] = hi[p]+1;
	mn[v] = hi[v];
	frin(u, g[v]) {
		if(u.F == p && u.S == pid)
			continue;
		if(!hi[u.F])
			dfs(u.F, v, u.S);
		mins(mn[v], mn[u.F]);
	}
	if(v != p && mn[v] == hi[v])
		cout << v << ' ' << p << '\n';
}

void solve() {
	cin >> n >> m;
	DSU();
	int u, v;
	while(m--) {
		cin >> u >> v;
		if(Union(u, v))
			g[u].pb({v, edge}), g[v].pb({u, edge});
	}
	fr(i, 1, n+1)
		if(!hi[i])
			dfs(i, i, 0);
}

int32_t main() {
	FastIO
	int T = 1;
	// cin >> T;
	while(T--)
		solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3420 KB Output is correct
2 Correct 3 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 3056 KB Output is correct
2 Correct 64 ms 2904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 3924 KB Output is correct
2 Correct 135 ms 3460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 5716 KB Output is correct
2 Correct 159 ms 5200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 259 ms 12140 KB Output is correct
2 Runtime error 239 ms 17960 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 403 ms 13652 KB Output is correct
2 Runtime error 374 ms 26896 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 516 ms 15960 KB Output is correct
2 Runtime error 490 ms 32376 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 628 ms 15896 KB Output is correct
2 Runtime error 611 ms 38288 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 777 ms 15280 KB Output is correct
2 Runtime error 738 ms 45652 KB Memory limit exceeded
3 Halted 0 ms 0 KB -