1903F - Babysitting - CodeForces Solution


2-sat binary search data structures graphs trees *2500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
const ll ILL=2167167167167167167;
const int INF=2100000000;
const int mod=998244353;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a,bool upp=0){if(a) cout<<(upp?"YES\n":"Yes\n"); else cout<<(upp?"NO\n":"No\n");}
template<class T> void vec_out(vector<T> &p,int ty=0){
if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}

namespace po167{
struct SCC{
	std::vector<std::vector<int>> G;
	std::vector<std::vector<int>> G_rev;
	int size;
	SCC(int n):G(n),G_rev(n),size(n){}
	void clear(){
		for(auto &x:G) x.clear();
		for(auto &x:G_rev) x.clear();
	}
	void reserve(int ind,size_t n_){
		G[ind].reserve(n_);
		G_rev[ind].reserve(n_);
	}
	int add_ver(){
		G.push_back({});
		G_rev.push_back({});
		size++;
		return size-1;
	}
	void add_edge(int from,int to){
		assert(0<=to&&to<size);
		assert(0<=from&&from<size);
		G[from].push_back(to);
		G_rev[to].push_back(from);
	}

	void dfs1(std::vector<int> &order,std::vector<int> &seen,int ind,int &now){
		if(seen[ind]!=0) return;
		seen[ind]=-1;
		for(auto x:G[ind]){
			dfs1(order,seen,x,now);
		}
		order[now]=ind;
		now++;
	}
	void dfs2(std::vector<int> &seen,int ind,int rank){
		if(seen[ind]!=-1) return;
		seen[ind]=rank;
		for(auto x:G_rev[ind]){
			dfs2(seen,x,rank);
		}
	}
	std::vector<std::vector<int>> tp_sort(){
		std::vector<int> seen(size,0),order(size);
		int now=0;
		for(int i=0;i<size;i++) dfs1(order,seen,i,now);
		now=0;
		for(int i=size-1;i>=0;i--){
			if(seen[order[i]]==-1){
				dfs2(seen,order[i],now);
				now++;
			}
		}
		std::vector<std::vector<int>> ans(now);
		for(int i=0;i<size;i++){
			ans[seen[i]].push_back(i);
		}
		return ans;
	}
	std::vector<int> for_two_sat(){
		std::vector<int> seen(size,0),order(size);
		int now=0;
		for(int i=0;i<size;i++) dfs1(order,seen,i,now);
		now=0;
		for(int i=size-1;i>=0;i--){
			if(seen[order[i]]==-1){
				dfs2(seen,order[i],now);
				now++;
			}
		}
		return seen;
	}
};

struct two_SAT{
	po167::SCC graph;
	std::vector<bool> ans;
	bool exists=false;
	int size;
	two_SAT(int n):graph(2*n),ans(n),size(n){}
	void add_clause(int i,bool f,int j,bool g){
		graph.add_edge(i*2+(!f),j*2+(g));
		graph.add_edge(j*2+(!g),i*2+(f));
	}
	void clear(){
		graph.clear();
		exists=false;
	}
	void one_true_in_group(std::vector<std::pair<int,int>> &p){
		int ind;
		for(int i=0;i<(int)(p.size())-1;i++){
			ind=graph.add_ver();
			graph.add_ver();
			ind/=2;
			add_clause(ind,1,p[i].first,p[i].second^1);
			add_clause(ind,0,p[i+1].first,p[i+1].second^1);
			if(i){
				add_clause(ind-1,0,ind,1);
			}
			ans.push_back(false);
			size++;
		}
	}
	bool build(){
		auto rank=graph.for_two_sat();
		exists=true;
		for(int i=0;i<size;i++){
			if(rank[i*2]==rank[i*2+1]){
				exists=false;
				break;
			}
			ans[i]=(rank[i*2]<rank[i*2+1]);
		}
		return exists;
	}
};
}
using po167::SCC;
using po167::two_SAT;


void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	cin>>t;
	rep(i,0,t) solve();
}

void solve(){
	int N,M;
	cin>>N>>M;
	vector<int> A(M),B(M);
	rep(i,0,M) cin>>A[i]>>B[i],A[i]--,B[i]--;
	int L=1,R=N+1;
	int size=1,d=0;
	while(size<N) d++,size*=2;
	vector<int> C(size);
	rep(i,0,M){
		C[A[i]]++;C[B[i]]++;
	}
	two_SAT T(size*2);
	rep(i,0,d){
		rep(j,1<<i,1<<(i+1)){
			T.graph.reserve(j,(j-(1<<i)?(size>>i):0)+3);
		}
	}
	rep(i,0,size){
		T.graph.reserve(i+size,(i<N?C[i]+d*2+6:2));
	}
	while(R-L>1){
		int mid=(L+R)/2;
		rep(i,0,M){
			T.add_clause(A[i]+size,1,B[i]+size,1);
		}
		rep(i,1,size){
			T.add_clause(i,1,i*2,0);
			T.add_clause(i,1,i*2+1,0);
		}
		rep(i,0,N){
			int l=i+1,r=min(N,i+mid);
			l+=size,r+=size;
			while(r-l){
				if(l&1){
					T.add_clause(i+size,0,l++,0);
				}
				if(r&1){
					T.add_clause(i+size,0,--r,0);
				}
				l>>=1;r>>=1;
			}
		}
		if(T.build()) L=mid;
		else R=mid;
		T.clear();
	}
	cout<<L<<"\n";
}


Comments

Submit
0 Comments
More Questions

553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating