답안 #861386

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
861386 2023-10-16T04:54:53 Z Faisal_Saqib Pipes (CEOI15_pipes) C++17
50 / 100
840 ms 55792 KB
#include <iostream>
#include <algorithm>
#include <climits>
#include <cmath>
#include <map>
#include <set>
#include <bitset>
#include <iomanip>
#include <vector>

using namespace std;

const int MAXN=1e5+1;
const int LG=17;
int pa[MAXN][LG];
int *par,*h,*Up,*par1;
vector<int> ma[MAXN];
int get1(int& a)
{
	return ((par1[a]==a)?a:par1[a]=get1(par1[a]));
}
void join1(int a,int b)
{
	a=get1(a);
	b=get1(b);
	if(a!=b)
		par1[a]=b;
}
int get(int& a)
{
	return ((par[a]==a)?a:par[a]=get(par[a]));
}
bool join(int a,int b)
{
	a=get(a);
	b=get(b);
	if(a!=b)
	{
		par[a]=b;
		return 1;
	}
	return 0;
}
void dfs(int& x,int& p)
{
	// cout<<x<<' '<<p<<endl;
	pa[x][0]=p;
	for(int j=1;j<LG;j++)
		pa[x][j]=pa[pa[x][j-1]][j-1];
	h[x]=h[p]+1;
	for(auto y:ma[x])
		if(y!=p)
			dfs(y,x);
}
int lca(int x,int y)
{
	if(h[x]>h[y])
		swap(x,y);
	for(int j=LG-1;j>=0;j--)
		if(h[x]<=h[pa[y][j]])
			y=pa[y][j];
	if(x==y)
		return x;
	for(int j=LG-1;j>=0;j--)
		if(pa[x][j]!=pa[y][j])
		{
			x=pa[x][j];
			y=pa[y][j];
		}
	return pa[x][0];
}
void solve(int& x,int& p)
{
	for(auto y:ma[x])
	{
		if(y!=p)
		{
			solve(y,x);
		}
	}
	for(auto y:ma[x])
	{
		if(y!=p)
		{
			if(h[Up[x]]>h[Up[y]])
			{
				Up[x]=Up[y];
			}
			if(Up[y]==y)
			{
				cout<<x<<' '<<y<<endl;
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
    int n,m;
    cin>>n>>m;
	par=new int[n+1];
	par1=new int[n+1];
	h=new int[n+1];
	Up=new int[n+1];
    for(int i=1;i<=n;i++)
    	par[i]=par1[i]=Up[i]=i;
    for(int i=0;i<m;i++)
    {
    	int x,y;
    	cin>>x>>y;
    	if(join(x,y))
    	{
    		// cout<<"Tree Edge "<<x<<' '<<y<<endl;
    		ma[x].push_back(y);
    		ma[y].push_back(x);
    	}
    	else
    	{
    		// cout<<"Cycle Edge "<<x<<' '<<y<<endl;
    		join1(x,y);
    	}
    }
    // cout<<"Tree Input:\n";
    for(int i=1;i<=n;i++)
    	if(pa[i][0]==0)
		    dfs(i,i);
	// cout<<"Cycle Input:"<<endl;
    for(int y=1;y<=n;y++)
    {
    	int x=get1(y);
    	// cout<<y<<' '<<x<<endl;
    	int c=lca(x,y);
		if(h[Up[x]]>h[c])
		{
			Up[x]=c;
		}
		if(h[Up[y]]>h[c])
		{
			Up[y]=c; 
		}
    }
    // cout<<"Bridge Edges:\n";
    for(int i=1;i<=n;i++)
    	if(pa[i][0]==i)
		    solve(i,i);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4952 KB Output is correct
2 Correct 4 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 4700 KB Output is correct
2 Correct 66 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 4956 KB Output is correct
2 Correct 135 ms 5208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 189 ms 5724 KB Output is correct
2 Correct 160 ms 8036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 260 ms 12112 KB Output is correct
2 Runtime error 259 ms 21256 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 401 ms 12576 KB Output is correct
2 Runtime error 395 ms 29508 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 527 ms 13856 KB Output is correct
2 Runtime error 505 ms 35056 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 649 ms 14152 KB Output is correct
2 Runtime error 639 ms 41288 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 840 ms 13636 KB Output is correct
2 Runtime error 776 ms 55792 KB Memory limit exceeded
3 Halted 0 ms 0 KB -