답안 #46882

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
46882 2018-04-24T13:57:37 Z sorry_Benq Pipes (CEOI15_pipes) C++17
60 / 100
1246 ms 19564 KB
/**
* Sources: various
*/
 
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
 
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
 
const int MOD = 1000000007;
template<typename T>
ostream& operator<< (ostream& out, const pair<T, T>& v) {
    out << "{" << v.first << ", " << v.second << "}";
    return out;
}
 
template<typename T>
ostream& operator<< (ostream& out, const vector<T>& v) {
    out << "[";
    size_t last = v.size() - 1;
    for(size_t i = 0; i < v.size(); ++i) {
        out << v[i];
        if (i != last) 
            out << ", ";
    }
    out << "]";
    return out;
}
 
template<typename T>
ostream& operator<< (ostream& out, const set<T>& v) {
    out << "[";
    auto pen = v.end();
    pen--;
    for (auto it = v.begin(); it != v.end(); it++){
		out << *it;
		if (it != pen){
			out << ", ";
		}
	}
	out << "]";
    return out;
}
 
template<typename T>
ostream& operator<< (ostream& out, const Tree<T>& v) {
    out << "[";
    auto pen = v.end();
    pen--;
    for (auto it = v.begin(); it != v.end(); it++){
		out << *it;
		if (it != pen){
			out << ", ";
		}
	}
	out << "]";
    return out;
}
 
template<int SZ> struct DSU {
    int par[SZ];
    DSU() {
        F0R(i,SZ) par[i] = i;
    }
    
    int get(int x) { // path compression
    	if (par[x] != x) par[x] = get(par[x]);
    	return par[x];
    }
    
    bool unite(int x, int y) { // union-by-rank
    	x = get(x), y = get(y);
    	if (x == y) return 0;
    	par[y] = x;
    	return 1;
    }
};
 
DSU<100005> one_connected;
DSU<100005> two_connected; 
vector<int> adj2[100005];
vector<int> adj3[100005];
int par1[100005];
int par2[100005];
bool vis[100005];
int n; // number of nodes

bool visited[100005];
int tin[100005];
int fup[100005];
int timer;

void dfs(int v, int p = -1) {
    visited[v] = true;
    tin[v] = fup[v] = timer++;
    for (int to : adj2[v]) {
        if (to == p) continue;
        if (visited[to]) {
            fup[v] = min(fup[v], tin[to]);
        } else {
            dfs(to, v);
            fup[v] = min(fup[v], fup[to]);
            if (fup[to] > tin[v]){
				int cnt = 0;
				if ((par1[to] == v) || (par1[v] == to)) cnt++;
				if ((par2[to] == v) || (par2[v] == to)) cnt++;
				if (cnt == 1){
					cout << to << ' ' << v << endl;
				}
			}
                
        }
    }
    for (int to : adj3[v]) {
        if (to == p) continue;
        if (visited[to]) {
            fup[v] = min(fup[v], tin[to]);
        } else {
            dfs(to, v);
            fup[v] = min(fup[v], fup[to]);
            if (fup[to] > tin[v]){
				int cnt = 0;
				if ((par1[to] == v) || (par1[v] == to)) cnt++;
				if ((par2[to] == v) || (par2[v] == to)) cnt++;
				if (cnt == 1){
					cout << to << ' ' << v << endl;
				}
			}
                
        }
    }
}

void find_bridges() {
    timer = 1;
    for (int i = 0; i < n; ++i) {
        if (!visited[i])
            dfs(i);
    }
}
void DFSParent1(int v, int p = -1){
	vis[v] = true;
	for (int j: adj2[v]){
		if (j != p){
			DFSParent1(j, v);
			par1[j] = v;
		}
	}
}

void DFSParent2(int v, int p = -1){
	vis[v] = true;
	for (int j: adj3[v]){
		if (j != p){
			DFSParent2(j, v);
			par2[j] = v;
		}
	}
}

 
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	int m; cin >> n >> m;
	int u, v;
	for (int i = 0; i < m; i++){
		cin >> u >> v;
		if (!one_connected.unite(u, v)){
			if (!two_connected.unite(u, v)){
				continue;
			}
			else{
				adj2[u].pb(v);
				adj2[v].pb(u);
			}
		}
		else{
			adj3[u].pb(v);
			adj3[v].pb(u);
		}
	}
	for (int i = 1; i <= n; i++){
		if (!vis[i]){
			DFSParent1(i);
		}
	}
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; i++){
		if (!vis[i]){
			DFSParent2(i);
		}
	}
	
	find_bridges();
	return 0;
}
 
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 5988 KB Output is correct
2 Correct 7 ms 5888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 6528 KB Output is correct
2 Correct 12 ms 6272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 6392 KB Output is correct
2 Correct 100 ms 6216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 173 ms 7288 KB Output is correct
2 Correct 199 ms 6724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 298 ms 9208 KB Output is correct
2 Correct 254 ms 8696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 435 ms 15668 KB Output is correct
2 Correct 391 ms 12076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 652 ms 17104 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 869 ms 19564 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1034 ms 19560 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1246 ms 18868 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -