답안 #976773

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
976773 2024-05-07T06:00:57 Z 8pete8 Pipes (CEOI15_pipes) C++17
40 / 100
2483 ms 65536 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
#include <cstdlib> 
#include <cstdint>
using namespace std;
#define ll long long
#define f first
//#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
//#define int long long
//#define double long double
const int mxn=1e5,inf=1e9,minf=-1e9,lg=18;
vector<vector<int>>adj;
vector<int>h,par;//parent of comp(highest node)
int up[mxn+10][lg+1];
int lca(int a,int b){
	if(h[a]<h[b])swap(a,b);
	int k=h[a]-h[b];
	for(int i=0;i<=lg;i++)if(k&(1<<i))a=up[a][i];
	if(a==b)return a;
	for(int i=lg;i>=0;i--)if(up[a][i]!=up[b][i])a=up[a][i],b=up[b][i];
	return up[a][0];
}
struct dsu{
	vector<int>pa;
	void init(int n){
		pa.assign(n+1,0);
		for(int i=1;i<=n;i++)pa[i]=i;
	}
	int find(int u){return (pa[u]==u)?u:pa[u]=find(pa[u]);}
	bool merg(int a,int b,int mode){
		a=find(a),b=find(b);
		if(a==b)return 0;
		pa[a]=b;
		if(mode)par[b]=lca(par[b],par[a]);
		return 1;
	}
}t,t2;
bitset<mxn+10>vis;
void dfs(int cur,int p){
	vis[cur]=1;
	for(auto i:adj[cur])if(i!=p)h[i]=h[cur]+1,up[i][0]=cur,dfs(i,cur);
}
void dfs2(int cur,int p){
	for(auto i:adj[cur])if(i!=p)dfs2(i,cur);	
	if(h[par[t2.find(cur)]]<h[cur])t2.merg(cur,up[cur][0],1);
}
void getans(int cur,int p){
	for(auto i:adj[cur])if(i!=p){
		if(t2.find(cur)!=t2.find(i))cout<<cur<<" "<<i<<'\n';
		getans(i,cur);
	}
}
int32_t main(){
	int n,m;cin>>n>>m;
	adj.resize(n+1);
	int a,b;
	t.init(n);
	t2.init(n);
	h.assign(n+1,0);
	par.assign(n+1,0);
	for(int i=1;i<=n;i++)par[i]=i;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		if(t.merg(a,b,0)){
			adj[a].pb(b);
			adj[b].pb(a);
		}
		else t2.merg(a,b,0);
	}
	vector<int>head;
	for(int i=1;i<=n;i++)if(!vis[i])dfs(i,-1),head.pb(i);
	for(auto j:head)for(int i=1;i<=lg;i++)up[j][i]=j;
	for(int i=1;i<=lg;i++)for(int j=1;j<=n;j++)up[j][i]=up[up[j][i-1]][i-1];
	for(int i=1;i<=n;i++){
		int x=t2.find(i);
		if(par[x])par[x]=lca(par[x],i);
		else par[x]=i;
	}
	for(auto j:head)dfs2(j,-1);
	for(auto j:head)getans(j,-1);
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2904 KB Output is correct
2 Correct 7 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 210 ms 8336 KB Output is correct
2 Correct 204 ms 8016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 368 ms 13132 KB Output is correct
2 Correct 448 ms 14680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 591 ms 20260 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 768 ms 32360 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1200 ms 45652 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1580 ms 58824 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2071 ms 65536 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2483 ms 65536 KB Memory limit exceeded
2 Halted 0 ms 0 KB -