Submission #592008

# Submission time Handle Problem Language Result Execution time Memory
592008 2022-07-08T11:25:11 Z andrei_boaca Pipes (CEOI15_pipes) C++14
100 / 100
4491 ms 15356 KB
#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> pii;
vector<vector<int>> muchii;
int comp[100005],myroot[100005],dp[19][100005],val[100005],par[100005],niv[100005],lg[100005];
int n,m;
int what;
int LCA(int a,int b)
{
    if(niv[a]<niv[b])
        swap(a,b);
    int dif=niv[a]-niv[b];
    int loga=log2(dif);
    for(int i=0;i<=loga;i++)
        if((dif>>i)&1)
            a=dp[i][a];
    if(a==b)
        return a;
    loga=log2(niv[a]);
    for(int i=loga;i>=0;i--)
        if(dp[i][a]!=0&&dp[i][b]!=0)
            if(dp[i][a]!=dp[i][b])
            {
                a=dp[i][a];
                b=dp[i][b];
            }
    return par[a];
}
void dfscalc(int nod)
{
    for(int q:muchii[nod])
    {
        int i=abs(q);
        if(i!=par[nod])
        {
            dfscalc(i);
            val[nod]+=val[i];
        }
    }
    if(val[nod])
    {
        int a=min(nod,par[nod]);
        int b=max(nod,par[nod]);
        for(int &i:muchii[b])
            if(i==a&&i>0)
            {
                i=-i;
                break;
            }
    }
    comp[nod]=what;
}
void dfsbuild(int nod)
{
    val[nod]=0;
    dp[0][nod]=par[nod];
    for(int i=1;i<=17;i++)
        dp[i][nod]=dp[i-1][dp[i-1][nod]];
    for(int q:muchii[nod])
    {
        int i=abs(q);
        if(i!=par[nod])
        {
            par[i]=nod;
            niv[i]=niv[nod]+1;
            dfsbuild(i);
        }
    }
}
void dsumerge(int a, int b)
{
    int c1=comp[a],c2=comp[b];
    if(lg[c1]<lg[c2])
    {
        swap(a,b);
        swap(c1,c2);
    }
    what=c1;
    dfscalc(myroot[c2]);
    par[b]=a;
    lg[c1]+=lg[c2];
    lg[c2]=0;
    niv[b]=niv[a]+1;
    muchii[a].push_back(b);
    muchii[b].push_back(a);
    dfsbuild(b);
}
int main()
{
    cin>>n>>m;
    muchii.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        niv[i]=1;
        lg[i]=1;
        comp[i]=i;
        myroot[i]=i;
        par[i]=0;
        val[i]=0;
    }
    int cnt=0;
    for(int z=1;z<=m;z++)
    {
        int a,b;
        cin>>a>>b;
        if(comp[a]!=comp[b])
        {
            cnt++;
            dsumerge(a,b);
        }
        else
        {
            int lca=LCA(a,b);
            val[a]++;
            val[b]++;
            val[lca]-=2;
        }
    }
    for(int i=1;i<=n;i++)
        if(lg[i])
            dfscalc(myroot[i]);
    for(int i=1;i<=n;i++)
        for(int j:muchii[i])
            if(j<i&&j>0)
                cout<<i<<' '<<j<<'\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 980 KB Output is correct
2 Correct 8 ms 1044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 347 ms 852 KB Output is correct
2 Correct 311 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 581 ms 1828 KB Output is correct
2 Correct 682 ms 1816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1034 ms 3912 KB Output is correct
2 Correct 867 ms 4928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1412 ms 10768 KB Output is correct
2 Correct 1237 ms 10884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2280 ms 12340 KB Output is correct
2 Correct 2178 ms 12272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3072 ms 15092 KB Output is correct
2 Correct 2784 ms 15124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3903 ms 15088 KB Output is correct
2 Correct 3531 ms 15288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4491 ms 14344 KB Output is correct
2 Correct 4322 ms 15356 KB Output is correct