Submission #46445

# Submission time Handle Problem Language Result Execution time Memory
46445 2018-04-20T20:31:23 Z sorry_Benq Pipes (CEOI15_pipes) C++17
20 / 100
1230 ms 14852 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], sz[SZ];
    DSU() {
        F0R(i,SZ) par[i] = i, sz[i] = 1;
    }
    
    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;
    	if (sz[x] < sz[y]) swap(x,y);
    	sz[x] += sz[y], par[y] = x;
    	return 1;
    }
};

DSU<100005> one_connected;
DSU<100005> two_connected; 
vector<int> adj[100005];
int timer = 1;
int ti[100005]; int fup[100005];
bool vis[100005];

void DFS(int v, int p = -1){
	if (vis[v]) return;
	vis[v] = true;
	ti[v] = fup[v] = timer++;
	//cout << v << ' ' << timer << endl;
	for (int j: adj[v]){
		if (j == p) continue;
		if (vis[j]){
			assert(ti[j] != -1);
			fup[v] = min(ti[j], fup[v]);
		}
		else{
			DFS(j, v);
			fup[v] = min(fup[j], fup[v]);
			if (fup[j] > ti[v]){
				cout << v << ' ' << j << endl;
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	int n, m; cin >> n >> m;
	int u, v;
	for (int i = 1; i <= n; i++){
		ti[i] = -1; fup[i] = -1;
	}
	for (int i = 0; i < m; i++){
		cin >> u >> v;
		if (!one_connected.unite(u, v)){
			if (!two_connected.unite(u, v)){
				continue;
			}
			else{
				adj[u].pb(v);
				adj[v].pb(u);
			}
		}
		else{
			adj[u].pb(v);
			adj[v].pb(u);
		}
	}
	for (int i = 1; i <= n; i++){
		if (!vis[i]){
			DFS(i);
		}
	}
	return 0;
}

// read!read!read!read!read!read!read!
// ll vs. int!
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4352 KB Output is correct
2 Incorrect 6 ms 4224 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4760 KB Output is correct
2 Incorrect 9 ms 4480 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 99 ms 4620 KB Output is correct
2 Incorrect 94 ms 4480 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Incorrect 163 ms 5292 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 276 ms 6864 KB Output is correct
2 Correct 236 ms 6388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 405 ms 11780 KB Output is correct
2 Incorrect 370 ms 8308 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 608 ms 12924 KB Output is correct
2 Incorrect 555 ms 10000 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 842 ms 14836 KB Output is correct
2 Incorrect 751 ms 10096 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 1032 ms 14852 KB Output is correct
2 Incorrect 927 ms 10004 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 1230 ms 14348 KB Output is correct
2 Correct 1107 ms 11416 KB Output is correct