1634E - Fair Share - CodeForces Solution


constructive algorithms data structures dfs and similar graph matchings graphs *2400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> PII;
#define ft first
#define sd second
const int N=4e5+10;
const int mod=1e9+7;
int n,m;
int idx,deg[N];
vector<PII>g[N];
vector<int>a[N];
map<int,int>mp;
void dfs(int u,int x){
	while(deg[u]){
		deg[u]--;
		int v=g[u][deg[u]].ft;
		if(a[min(u,v)][g[u][deg[u]].sd]!=-1)continue;
		a[min(u,v)][g[u][deg[u]].sd]=x;
		dfs(v,x^1);
	}
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);
	cin>>n;
	idx=n;
	for(int i=1;i<=n;i++){
		cin>>m;
		a[i].resize(m+1,-1);
		for(int j=1;j<=m;j++){
			int x;cin>>x;
			if(mp[x]==0)mp[x]=++idx;
			x=mp[x];
			g[i].push_back(make_pair(x,j));
			g[x].push_back(make_pair(i,j));
			deg[i]++;
			deg[x]++;
		}
	}
	for(int i=1;i<=idx;i++){
		if(deg[i]%2){
			cout<<"NO\n";
			return 0;
		}
	}
	for(int i=1;i<=idx;i++)dfs(i,0);
	cout<<"YES\n";
	for(int i=1;i<=n;i++){
		for(int j=1;j<a[i].size();j++){
			if(a[i][j]==0)cout<<'L';
			else cout<<'R';
		}
		cout<<'\n';
	}
}


Comments

Submit
0 Comments
More Questions

1157E - Minimum Array
1661D - Progressions Covering
262A - Roma and Lucky Numbers
1634B - Fortune Telling
1358A - Park Lighting
253C - Text Editor
365B - The Fibonacci Segment
75A - Life Without Zeros
1519A - Red and Blue Beans
466A - Cheap Travel
659E - New Reform
1385B - Restore the Permutation by Merger
706A - Beru-taxi
686A - Free Ice Cream
1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs
1257A - Two Rival Students
1415A - Prison Break
1271A - Suits
259B - Little Elephant and Magic Square