Submission #1193319

#TimeUsernameProblemLanguageResultExecution timeMemory
1193319CrabCNHPipes (CEOI15_pipes)C++20
0 / 100
550 ms7828 KiB
#include <bits/stdc++.h>

#define task   "BriantheCrab"

#define pii    pair <int, int>
#define fi     first
#define se     second
#define szf    sizeof
#define sz(s)  (int)((s).size())

using namespace std;

template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}

const int maxN = 1e5 + 5;
const int maxM = 6e6 + 1;
const int mod = 1e9 + 7;

mt19937_64 rnd (chrono::steady_clock::now ().time_since_epoch ().count ());

int n, m;
int par[maxN], sz[maxN];
int val[maxN];
vector <int> adj[maxN];

void dfs (int u, int p) {
    for (auto v : adj[u]) {
        if (v == p) {
            continue;
        }
        dfs (v, u);
        val[u] += val[v];
        if (val[v] == 0) {
            cout << min (u, v) << ' ' << max (u, v) << '\n';
        }
    }
}

void init () {
    for (int i = 1; i <= n; i ++) {
        sz[i] = 1;
        par[i] = i;
    }
}

int getRoot (int u) {
    if (par[u] == u) {
        return u;
    }
    return (par[u] = getRoot (par[u]));
}

void merge (int u, int v) {
    u = getRoot (u);
    v = getRoot (v);
    if (u == v) {
        return;
    }
    if (sz[u] < sz[v]) {
        swap (u, v);
    }
    par[v] = u;
    sz[u] += sz[v];
    adj[u].push_back (v);
    adj[v].push_back (u);
} 

void solve () {
    cin >> n >> m;
    init ();
    for (int i = 1; i <= m; i ++) {
        int u, v;
        cin >> u >> v;
        if (getRoot (u) == getRoot (v)) {
            int w = rnd ();
            val[u] += w;
            val[v] -= w;
        }
        else {
            merge (u, v);
        }
    }
    for (int i = 1; i <= n; i ++) {
        if (par[i] == i) {
            dfs (i, i);
        }
    }
    return;
}

signed main () {
    cin.tie (nullptr) -> sync_with_stdio (false);
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int t = 1;
    //cin >> t;
    while (t --) {
        solve ();
    } 
    return 0;
}
// thfdgb

Compilation message (stderr)

pipes.cpp: In function 'int main()':
pipes.cpp:95:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pipes.cpp:96:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...