Submission #314945

# Submission time Handle Problem Language Result Execution time Memory
314945 2020-10-21T16:44:21 Z mohamedsobhi777 Pipes (CEOI15_pipes) C++14
100 / 100
1390 ms 13176 KB
#include <bits/stdc++.h>

#define I inline void
#define S struct
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;
using ll = long long;
using ld = long double;

const int N = 1e5 + 7, mod = 1e9 + 7;
const ll inf = 2e18;

// How interesting!

int n, m, t;
vector<int> adj[N];
int low[N], tim[N];

void dfs(int x, int p)
{
        low[x] = tim[x] = ++t;
        int ban = 0;
        for (auto u : adj[x])
        {
                if (u == p && !ban++)
                        continue;
                if (low[u])
                {
                        low[x] = min(low[x], tim[u]);
                }
                else
                {
                        dfs(u, x);
                        if (low[u] > tim[x])
                        {
                                cout << x << " " << u << "\n";
                        }
                        low[x] = min(low[x], low[u]);
                }
        }
}

struct dsu
{
        int fat[N];
        dsu()
        {
                for (int i = 0; i < N; ++i)
                        fat[i] = i;
        }
        int find(int x) { return fat[x] = (x == fat[x] ? x : find(fat[x])); }

        void link(int u, int v)
        {
                u = find(u);
                v = find(v);
                if (u != v)
                {
                        fat[u] = v;
                }
        }

        bool same(int u, int v)
        {
                return find(u) == find(v);
        }
} d1, d2;
int u, v;

int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        //freopen("in.in", "r", stdin);
        cin >> n >> m;
        for (int i = 0; i < m; ++i)
        {
                cin >> u >> v;
                if (!d1.same(u, v))
                {
                        d1.link(u, v);
                }
                else if (!d2.same(u, v))
                {
                        d2.link(u, v);
                }
                else
                        continue;
                adj[u].push_back(v);
                adj[v].push_back(u);
        }
        for (int i = 1; i <= n; ++i)
        {
                if (!tim[i])
                        dfs(i, i);
        }

        return 0;
}

/*
                - bounds sir (segtree = 4N, eulerTour = 2N, ...)
                - a variable defined twice?
                - will overflow?
                - is it a good complexity?
                - don't mess up indices (0-indexed vs 1-indexed)
                - reset everything between testcases. 
        */
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3968 KB Output is correct
2 Correct 6 ms 3712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 3832 KB Output is correct
2 Correct 115 ms 3664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 4368 KB Output is correct
2 Correct 220 ms 4088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 323 ms 5752 KB Output is correct
2 Correct 273 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 474 ms 10232 KB Output is correct
2 Correct 400 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 704 ms 11256 KB Output is correct
2 Correct 660 ms 8824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 930 ms 13020 KB Output is correct
2 Correct 885 ms 9080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1149 ms 13176 KB Output is correct
2 Correct 1090 ms 8956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1390 ms 12664 KB Output is correct
2 Correct 1295 ms 10232 KB Output is correct