Submission #976789

# Submission time Handle Problem Language Result Execution time Memory
976789 2024-05-07T06:26:25 Z 8pete8 Pipes (CEOI15_pipes) C++17
60 / 100
963 ms 48860 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;
int lg=0;
vector<vector<int>>up;
vector<vector<int>>adj;
vector<int>h,par;//parent of comp(highest node)
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;
vector<pii>comp;
void bfs(int cur,int p){
	queue<pii>q;
	q.push({cur,p});
	while(!q.empty()){
		int cc=q.front().f,cp=q.front().s;
		q.pop();
		if(vis[cc])continue;
		vis[cc]=1;
		comp.pb({h[cc],cc});
		for(auto i:adj[cc]){
			if(i==cp)continue;
			h[i]=h[cc]+1;
			up[i][0]=cc;
			q.push({i,cc});
		}
	}
}
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(){
	fastio
	int n,m;cin>>n>>m;
	lg=log2(n)+1;
	up.resize(n+1);
	for(int i=0;i<=n;i++)up[i].resize(lg+1,0);
	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);
	}
	for(int head=1;head<=n;head++)if(!vis[head]){
		bfs(head,-1);
		sort(rall(comp));
		for(int i=1;i<=lg;i++)up[head][i]=head;
		for(int i=1;i<=lg;i++)for(auto j:comp)up[j.s][i]=up[up[j.s][i-1]][i-1];
		for(auto j:comp){
			int x=t2.find(j.s);
			if(par[x])par[x]=lca(par[x],j.s);
			else par[x]=j.s;
		}
		for(auto j:comp)if(h[par[t2.find(j.s)]]<h[j.s])t2.merg(j.s,up[j.s][0],1);
		getans(head,-1);
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 1112 KB Output is correct
2 Correct 14 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 860 KB Output is correct
2 Correct 78 ms 964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 2092 KB Output is correct
2 Correct 141 ms 2076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 192 ms 4816 KB Output is correct
2 Correct 624 ms 5836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 273 ms 13440 KB Output is correct
2 Correct 370 ms 13580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 423 ms 15416 KB Output is correct
2 Runtime error 963 ms 48860 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 566 ms 18632 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 671 ms 18636 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 831 ms 17880 KB Memory limit exceeded
2 Halted 0 ms 0 KB -