답안 #391439

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
391439 2021-04-18T18:18:52 Z ali_tavakoli Pipes (CEOI15_pipes) C++14
100 / 100
1345 ms 15452 KB
//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back
#define F first
#define S second
//#pragma GCC optimize("Ofast")

const int maxn = 1e5 + 5, maxlog = 17;

int n, m, pr[maxn], x, y, par[maxn][maxlog], h[maxn], cnt[maxn], cnt2[maxn], pr2[maxn];
vector<int> adj[maxn];
vector<pair<int, int> > edge;
bool vis[maxn];

int getpar2(int v)
{
    if(pr2[v] == 0)
        return pr2[v] = v;
    
    return (v == pr2[v] ? v : pr2[v] = getpar2(pr2[v]));
}

bool un2(int v, int u)
{
    v = getpar2(v);
    u = getpar2(u);
    if(u != v)
    {
        pr2[u] = v;
        return 1;
    }
    return 0;
}

int getpar(int v)
{
    if(pr[v] == 0)
        return pr[v] = v;
    
    return (v == pr[v] ? v : pr[v] = getpar(pr[v]));
}

bool un(int v, int u)
{
    v = getpar(v);
    u = getpar(u);
    if(u != v)
    {
        pr[u] = v;
        return 1;
    }
    return 0;
}


void dfs(int v, int p = 0, int hi = 1)
{
    vis[v] = 1;
    par[v][0] = p;
    h[v] = hi;
    for(int i = 1; i < maxlog; i++)
        par[v][i] = par[par[v][i - 1]][i - 1];
    
    for(auto u : adj[v])
        if(u != p)
            dfs(u, v, hi + 1);
}

int lca(int v, int u)
{
    if(h[v] < h[u])
        swap(v, u);
    
    for(int i = maxlog - 1; i >= 0; i--)
        if(h[par[v][i]] >= h[u])
            v = par[v][i];
    
    for(int i = maxlog - 1; i >= 0; i--)
        if(par[v][i] != par[u][i])
            v = par[v][i], u = par[u][i];
    
    return (v == u ? v : par[v][0]);
}

void dfs2(int v, int p = 0)
{
    vis[v] = 1;
    cnt2[v] = cnt[v];
    for(auto u : adj[v])
        if(u != p)
            dfs2(u, v), cnt2[v] += cnt2[u];
}

int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> m;

    for(int i = 0; i < m; i++)
    {
        cin >> x >> y;
        if(un(x, y))
        {
            adj[x].pb(y);
            adj[y].pb(x);
            //bs[i] = 1;
        }
        else if(un2(x, y))
            edge.pb({x, y});
    }

    for(int i = 1; i <= n; i++)
        if(!vis[i])
            dfs(i);

    //cout << lca(1, 2) << '\n';
    //cin.seekg(0);

    //cin >> n >> m;

    for(auto e : edge)
    {
        x = e.F, y = e.S;
        //cin >> x >> y;
        //cout << x << " " << y << '\n';
        //cout << x << " " << y << '\n';
        
        //cout << x << " " << y << " " << lca(x, y) << '\n';
        cnt[x] ++;
        cnt[y] ++;
        cnt[lca(x, y)] -= 2;
    }

    //return 0;
    memset(vis, 0, maxn);

    for(int i = 1; i <= n; i++)
        if(!vis[i])
            dfs2(i);

    vector<pair<int, int> > ans;

    for(int i = 2; i <= n; i++)
        if(cnt2[i] == 0&& par[i][0] != 0)
            cout << i << " " << par[i][0] << '\n';
    
    return 0;
}

/*
10 11
1 7
1 8
1 6
2 8
6 7
5 8
2 5
2 3
2 4
3 4
10 9
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 2 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 3284 KB Output is correct
2 Correct 7 ms 3276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 3240 KB Output is correct
2 Correct 113 ms 3456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 196 ms 3980 KB Output is correct
2 Correct 226 ms 4364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 323 ms 5720 KB Output is correct
2 Correct 295 ms 6660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 461 ms 11476 KB Output is correct
2 Correct 463 ms 11500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 775 ms 12748 KB Output is correct
2 Correct 676 ms 12484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 963 ms 15096 KB Output is correct
2 Correct 885 ms 15320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1179 ms 15244 KB Output is correct
2 Correct 1093 ms 15452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1345 ms 14548 KB Output is correct
2 Correct 1325 ms 15024 KB Output is correct