Submission #1126718

#TimeUsernameProblemLanguageResultExecution timeMemory
1126718DenisovPipes (CEOI15_pipes)C++20
100 / 100
3181 ms14984 KiB
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#include <bits/stdc++.h>

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

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

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif

const int max_n = 1e5 + 11, inf = 1000111222;

struct dsu {
public:
    int n;
    vector <int> p;

    inline void make_set (int v) {
        p[v] = v;
    }

    dsu (int n) : n(n) {
        p.resize(n);
        for (int i = 0; i < n; i++) {
            make_set(i);
        }
    }

    inline int get (int v) {
        if (p[v] == v) return v;
        return p[v] = get(p[v]); /// compressing path
    }

    inline bool unite (int a, int b) {
        a = get(a);
        b = get(b);
        if (a == b) return false;
        p[a] = b;
        return true;
    }
};



vector <int> edge[max_n];
pii ee[max_n];

const int C = 1e5 / 2;

int tin[max_n], tout[max_n], timer, n;
bool bad[max_n];

inline void dfs (int v, int prv = inf) {
    tin[v] = tout[v] = timer++;
    for (int id : edge[v]) {
        if (id < 0) {
            id = -id - 1;
            if (id == prv) {
                continue;
            }
            int to = v ^ ee[id].first ^ ee[id].second;
            if (tin[to]) {
                umin(tout[v], tin[to]);
                bad[id] = 1;
            }
            else {
                dfs(to, id);
                umin(tout[v], tout[to]);
                if (id < n && tout[to] <= tin[v]) {
                    bad[id] = 1;
                }
            }
        }
        else {
            int to = id;
            if (-to - 1 == prv) {
                continue;
            }
//        int to = v ^ ee[id].first ^ ee[id].second;
            if (tin[to]) {
                umin(tout[v], tin[to]);
//            bad[id] = 1;
            }
            else {
                dfs(to, -to - 1);
                umin(tout[v], tout[to]);
//            if (id < n && tout[to] <= tin[v]) {
//                bad[id] = 1;
//            }
            }
        }

    }
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int m;
    cin >> n >> m;
    dsu t(n);
    int num = 0;
    int id = 0;
    for (int i = 0, u, v; i < m; ) {
        int r = min(i + C, m);
        for (int j = 0; j < n; j++) {
            edge[j].clear();
        }
        for (int j = 0; j < id; j++) {
            edge[ee[j].first].pb(-(j + 1));
            edge[ee[j].second].pb(-(j + 1));
        }
        while (i < r) {
            cin >> u >> v;
            --u, --v;
            if (t.unite(u, v)) {
                edge[u].pb(-(id + 1));
                edge[v].pb(-(id + 1));
                ee[id++] = make_pair(u, v);
            }
            else {
                edge[u].pb(v);
                edge[v].pb(u);
            }
            ++i;
        }
        for (int j = 0; j < n; j++) tin[j] = 0;
        timer = 1;
        for (int j = 0; j < n; j++) {
            if (!tin[j]) {
                dfs(j);
            }
        }
    }
    for (int i = 0; i < id; i++) {
        if (!bad[i]) {
            cout << ee[i].first + 1 << ' ' << ee[i].second + 1 << '\n';
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...