1916F - Group Division - CodeForces Solution


constructive algorithms dfs and similar graphs

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define fr first
#define sc second

long long n[2],m,n2,nn,ttl,dh[4069],fh[4069],pr[4069],ca[4069],sq[4069],zs;
vector<pair<long long,long long>> al[4069];
bitset<4069> vtd,vtd2,spc;
vector<long long> al3[4069];
multiset<long long> ms[4069];
bool r0;

void dbd(long long x,long long b4)
{
	long long i,sz=al[x].size(),l,p,mn=dh[x];
	
	vtd[x]=1;
	vtd2[x]=1;
	fh[x]=dh[x];
	for(i=0;i<sz;i++)
	{
		l=al[x][i].fr;
		p=al[x][i].sc;
		if(p==b4)
		{
			continue;
		}
		if(!vtd[l])
		{
			dh[l]=dh[x]+1;
			pr[l]=x;
			dbd(l,p);
			fh[x]=min(fh[x],fh[l]);
			al3[x].push_back(l);
			ms[x].insert(fh[l]);
		}
		else if(vtd2[l])
		{
			fh[x]=min(fh[x],dh[l]);
			al3[l].push_back(x);
			mn=min(mn,dh[l]);
		}
	}
	vtd2[x]=0;
	ms[x].insert(mn);
}

bool cmp(long long x,long long y)
{
	return dh[x]>dh[y];
}

void dtrv(long long x)
{
	long long i,sz=al3[x].size(),l;
	
	vtd[x]=1;
	ttl++;
	spc[x]=1;
	if(ttl>=n[0])
	{
		r0=1;
		return;
	}
	nn++;
	ca[nn]=x;
	sort(al3[x].begin(),al3[x].end(),cmp);
	for(i=0;i<sz;i++)
	{
		l=al3[x][i];
		if(!vtd[l]&&(fh[l]>=dh[x]||dh[l]==dh[x]+1))
		{
			dtrv(l);
			if(r0)
			{
				return;
			}
		}
	}
	nn--;
	if(pr[x])
	{
		ms[pr[x]].erase(ms[pr[x]].find(fh[x]));
		if(!vtd[pr[x]]&&(!nn||*ms[pr[x]].begin()>dh[ca[nn]]))
		{
			dtrv(pr[x]);
		}
	}
}

int main()
{
	long long t,rr,i,ii,k,l;
	
	scanf("%lld",&t);
	for(rr=0;rr<t;rr++)
	{
		scanf("%lld%lld%lld",n,n+1,&m);
		n2=n[0]+n[1];
		for(i=1;i<=n2;i++)
		{
			al[i].clear();
			vtd[i]=0;
			vtd2[i]=0;
			al3[i].clear();
			ms[i].clear();
			spc[i]=0;
		}
		for(i=1;i<=m;i++)
		{
			scanf("%lld%lld",&k,&l);
			al[k].push_back({l,i});
			al[l].push_back({k,i});
		}
		dh[1]=0;
		pr[1]=0;
		dbd(1,-1);
		for(i=1;i<=n2;i++)
		{
			vtd[i]=0;
		}
		ttl=0;
		nn=0;
		r0=0;
		dtrv(al3[1][al3[1].size()-1]);
		for(ii=0;ii<2;ii++)
		{
			zs=0;
			for(i=1;i<=n2;i++)
			{
				if(spc[i]==!ii)
				{
					zs++;
					sq[zs]=i;
				}
			}
			for(i=1;i<=zs;i++)
			{
				printf("%lld%c",sq[i]," \n"[i==zs]);
			}
		}
	}
}


Comments

Submit
0 Comments
More Questions

HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits