Submission #291520

# Submission time Handle Problem Language Result Execution time Memory
291520 2020-09-05T12:59:33 Z davooddkareshki Pipes (CEOI15_pipes) C++17
100 / 100
1255 ms 8180 KB
#include<bits/stdc++.h>
using namespace std;

//#define int long long
#define F first
#define S second
#define pii pair<int,int>
#define mpr make_pair

typedef long long ll;

const int maxn = 1e5+1;
//const int mod = 1e9+7;
//const int inf = 1e9+10;

int n, m, q;

struct dsu
{
    vector<int> par;
    void DO()
    {
        for(int i = 0; i <= n; i++) par.push_back(-1);
    }

    int get_par(int v)
    {
        if(par[v] < 0) return v;
        return par[v] = get_par(par[v]);
    }

    void merg(int u, int v)
    {
        u = get_par(u);
        v = get_par(v);

        if(u == v) return;

        if(par[u] < par[v]) swap(u,v);
        par[v] += par[u];
        par[u] = v;
    }

    bool ask(int u, int v) {return (get_par(u) == get_par(v));}
} G, T;

/// G --> kole graph
/// T --> molefe haye 2hamband yali graph

vector<pair<int,pii>> g[maxn];
bitset<maxn> mark; int la[maxn];

void pfs(int v, int p = 0)
{
    mark[v] = 1;
    for(auto e : g[v])
    {
        int u = e.F;
        if(mark[u]) {la[v]++; la[u]--;}
    }
    for(auto e : g[v])
    {
        int u = e.F;
        if(!mark[u]) pfs(u,v);
    }
    la[p]++; la[v]--;
}

void dfs(int v, pii id = {-1,-1})
{
    mark[v] = 1;
    for(auto e : g[v])
    {
        int u = e.F; pii ID = e.S;
        if(!mark[u])
        {
            dfs(u,ID);
            la[v] += la[u];
        }
    }

    if(la[v] == 0 && id.F >= 0)
        cout<< id.F <<" "<< id.S <<"\n";
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    cin>> n >> m;
    G.DO(); T.DO();

    vector<pii> edge;
    for(int i = 1, u, v; i <= m; i++)
    {
        cin>> u >> v;

        if(!G.ask(u,v))
        {
            G.merg(u,v);
            edge.push_back({u,v});
        }
        else
            T.merg(u,v);
    }
    G.par.clear();

    while(edge.size())
    {
        pii e = edge.back();
        edge.pop_back();

        int u = T.get_par(e.F), v = T.get_par(e.S);
        g[u].push_back({v,e});
        g[v].push_back({u,e});
    }

    for(int i = 1; i <= n; i++)
        if(!mark[i]) pfs(i);
    for(int i = 1; i <= n; i++) mark[i] = 0;
    for(int i = 1; i <= n; i++)
        if(!mark[i]) dfs(i);
}
/*
10 11
1 7
1 8
1 6
2 8
6 7
5 8
2 5
2 3
2 4
3 4
10 9
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3072 KB Output is correct
2 Correct 5 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 2936 KB Output is correct
2 Correct 106 ms 2832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 3432 KB Output is correct
2 Correct 206 ms 3320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 301 ms 4144 KB Output is correct
2 Correct 243 ms 4272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 364 ms 7792 KB Output is correct
2 Correct 320 ms 6692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 579 ms 7916 KB Output is correct
2 Correct 559 ms 7688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 750 ms 8044 KB Output is correct
2 Correct 707 ms 7908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 961 ms 8132 KB Output is correct
2 Correct 935 ms 7888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1255 ms 8172 KB Output is correct
2 Correct 1162 ms 8180 KB Output is correct